如何在WinForm中发送HTTP请求

Brahmos 2010-06-19 05:38:41
我在用ASP.NET(C#)的WinForm写一个小程序,实现发送一个请求给某个网页,然后根据服务器返回的字符串(HTML字符串),对其进行分析并萃取有用信息,并显示在WinForm窗体上。第一步就是不知道怎么发送这个请求给服务器。
比如这个查询页面:http://..../chaxun.htm,需要输入一个学号,然后它会把请求提交给q.jsp页面,然后返回一些信息。chaxun.html页面的HTML关键代码是这样的:

<form name="form1" method="post" action="q.jsp">
<tr>
<td height="51">请输入学号:

<input name="stuxh" type="text" id="stuxh">
<span class="style1">学生八位学号,如:20061234</span></td>
</tr>
<tr>
<td height="19" align="center"><input type="submit" name="Submit" value="提交"></td>
</tr>
</form>

我需要做的就是发送这个请求给q.jsp页面,然后根据它返回来的HTML代码进行分析。如何发送这个请求,并取得服务器返回的字符串信息???至于提取信息我会。谢谢大家~
...全文
507 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Brahmos 2010-07-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ly302 的回复:]
Winform窗体中发送HTTP请求


手工发送HTTP请求主要是调用 System.Net的HttpWebResponse方法

手工发送HTTP的GET请 求:


C# code
string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch?keyword=";
strURL +=this.textB……
[/Quote]

谢谢这位兄弟的回答,呵呵,我自己找到条案了..!
皇城龙三 2010-06-19
  • 打赏
  • 举报
回复
Winform窗体中发送HTTP请求


手工发送HTTP请求主要是调用 System.Net的HttpWebResponse方法

手工发送HTTP的GET请 求:

string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch?keyword=";
strURL +=this.textBox1.Text;
System.Net.HttpWebRequest request;
// 创建一个HTTP请求
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
//request.Method="get";
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
XmlTextReader Reader = new XmlTextReader(s);
Reader.MoveToContent();
string strValue = Reader.ReadInnerXml();
strValue = strValue.Replace("<","<");
strValue = strValue.Replace(">",">");
MessageBox.Show(strValue);
Reader.Close();



手工发送HTTP的POST请求


string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch";
System.Net.HttpWebRequest request;

request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
//Post请求方式
request.Method="POST";
// 内容类型
request.ContentType="application/x-www-form-urlencoded";
// 参数经过URL编码
string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.textBox1.Text);
byte[] payload;
//将URL编码后的字符串转化为字节
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
//设置请求的 ContentLength
request.ContentLength = payload.Length;
//获得请 求流
Stream writer = request.GetRequestStream();
//将请求参数写入流
writer.Write(payload,0,payload.Length);
// 关闭请求流
writer.Close();
System.Net.HttpWebResponse response;
// 获得响应流
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
XmlTextReader Reader = new XmlTextReader(s);
Reader.MoveToContent();
string strValue = Reader.ReadInnerXml();
strValue = strValue.Replace("<","<");
strValue = strValue.Replace(">",">");
MessageBox.Show(strValue);
Reader.Close();

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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