通常狀況下傳輸和回傳HTTP協議就搞定了,但此次不一樣,有身份驗證,網上的資料相對較少,怎麼辦呢?.NET沒有不表明JAVA沒有,網上搜JAVA身份驗證HTTP協議,服務器
果真是有的,跟着代碼改爲相應的.NET代碼,就是下面這玩意:網絡
/// <summary> /// 服務器網絡憑證 /// </summary> /// <returns></returns> public NetworkCredential MyCredc(string loginUser, string loginPSW) { string loginHost = "";//主機名,能夠是IP地址,也能夠服務器名稱 NetworkCredential myCred = new NetworkCredential(loginUser, loginPSW); return myCred; }
try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?formInfo=" + JsonConvert.SerializeObject(obj)); request.Credentials = MyCredc(adm, pas); //憑證就是這玩意 request.Headers.Add("CSRF_NONCE", acquireWindchillToken(tokenUrl)); request.Method = "POST"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); } } catch (Exception ex) { throw new Exception(ex.Message); }}