winform调用http
一、post请求实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static string PostHttp(string url, string jsonParmaData) { HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); try { NetworkCredential auth = new NetworkCredential("admin", "www.hd123.com");//添加此代码 var postData = Encoding.UTF8.GetBytes(jsonParmaData); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; httpWebRequest.Timeout = 10000; httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip; httpWebRequest.Credentials = auth;//添加认证 httpWebRequest.ContentLength = postData.Length; httpWebRequest.GetRequestStream().Write(postData, 0, postData.Length); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (Stream stream = httpWebResponse.GetResponseStream()) { using (StreamReader sr = new StreamReader(stream, Encoding.UTF8)) { string str = sr.ReadToEnd(); sr.Close(); stream.Close(); httpWebResponse.Close(); return str; } } } catch (Exception ex) { try { httpWebRequest.Abort(); } catch { } } return null; }