一般情况下传输和回传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); }
}