c# winform简单get post请求
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2022-05-26 22:52:16
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
public static string HttpRequest(string Url, string postDataStr) { //发送 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "GET"; if (postDataStr != "") { request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; byte[] payload = System.Text.Encoding.UTF8.GetBytes(postDataStr); request.GetRequestStream().Write(payload, 0, payload.Length); } //响应接收 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }