如何在C#中发送http请求给Web服务器?

Artist 2003-04-01 10:05:33
就像在IE输入url一样。
比如
ClassA.URL="http://www.csdn.net";

ClassA.Connect();
...全文
1527 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dtmyou 2003-04-01
  • 打赏
  • 举报
回复
借花献佛
using System.Net;


WebRequest req = WebRequest.Create( "http://www.aaa.com/bbb.aspx" );
req.Method = "POST"; //指定提交的Method,可以为POST和GET,一定要大写

byte[] postData = System.Text.Encoding.GetEncoding("gb2312").GetBytes( "?param1=aaa¶m2=bbb" );//Post的数据

req.ContentLength = postData.Length;
Stream postStream = req.GetRequestStream();
postStream.Write(postData, 0, postData.Length);
postStream.Close();

WebResponse res = req.GetResponse();

System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding("gb2312");//接收的编码
StreamReader reader = new StreamReader( res.GetResponseStream(),resEncoding );

string html = reader.ReadToEnd(); //接收的Html

reader.Close();
res.Close();
yarshray 2003-04-01
  • 打赏
  • 举报
回复
HttpWebRequest

参考:

http://www.digitalearth.net.cn/gisrelateditissues/asp.net/net2internet.htm

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧