• 欢迎访问我爱CSharp学习网,这里有最新最全的C#书籍,C#视频。
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏我爱C#学习网吧
  • 推荐使用最新版Chrome浏览器和火狐浏览器访问本网站

C#发送GET与POST请求

C#杂烩 52csharp 930次浏览 0个评论 扫描二维码


C#发送GET与POST请求

////////HTTPGET

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.Method = “GET”;

request.ContentType = “text/html;charset=UTF-8”;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream myResponseStream = response.GetResponseStream();

StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);

string retString = myStreamReader.ReadToEnd();

myStreamReader.Close();

myResponseStream.Close();


/////HTTPPOST

public static string HttpPost(string Url, string postDataStr)  

{  

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);  

request.Method = “POST”;  

request.ContentType = “application/x-www-form-urlencoded”;  

request.ContentLength = postDataStr.Length;  

StreamWriter writer = new StreamWriter(request.GetRequestStream(),Encoding.ASCII);  

writer.Write(postDataStr);  

writer.Flush();  

HttpWebResponse response = (HttpWebResponse)request.GetResponse();  

string encoding = response.ContentEncoding;  

if (encoding == null || encoding.Length < 1) {  

 encoding = “UTF-8”; //默认编码  

}  

StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));  

string retString = reader.ReadToEnd();  

return retString;  



C#发送GET与POST请求



我爱CSharp学习网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#发送GET与POST请求
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址