转:Working with HttpWebRequest and HttpWebResponse in ASP.NET

本文介绍了如何利用HttpWebRequest和HttpWebResponse进行HttpGet和HttpPost操作,通过示例代码展示了从远程网站抓取内容及向其发送POST请求的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://www.worldofasp.net/tut/WebRequest/Working_with_HttpWebRequest_and_HttpWebResponse_in_ASPNET_114.aspx

 

Introduction

I will explain about the usage of HttpWebRequest and HttpWebResponse in this article. As you all probably know or heard about this class before. HttpWebRequest and HttpWebResponse class is inside the System.NET namespace, and this two classes is designed to communicate by using the Http Protocol

You can use this two classes to make requests to other Web Pages via HTTP and parse the resulting text to extract data. This is what we know as screen scraping.

In ASP world, you normally need to rely on third party components called ASPTear to grab the contents from other site. But now with the help of HttpWebRequest and HttpWebResponse, you can do that easily without have to invoke third party components.

Using HttpWebRequest and HttpWebResponse

In the code below, I provide very basic sample code on how to use HttpWebRequest and HttpWebResponse. In the first example I will list out the code on how to do screen scraping and the second example would be doing HttpPost data to another website

1. Sample Code on Grabbing Contents (Screen Scraping)

C#

protected void Page_Load(object sender,EventArgs e) {
Uri uri = new Uri("http://www.microsoft.com/default.aspx");if(uri.Scheme = Uri.UriSchemeHttp) {
HttpWebRequest request = HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());string  tmp = reader.ReadToEnd();
response.Close();
Response.Write(tmp);
}
}

VB.NET
Protected Sub Page_Load(ByVal sender as Object,ByVal e as System.EventArgs)
Dim uri as New Uri("http://www.microsoft.com/default.aspx");If(uri.Scheme == uri.UriSchemeHttp) Then
Dim request as HttpWebRequest = HttpWebRequest.Create(uri)
request.Method = WebRequestMethods.Http.Get
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(response.GetResponseStream())
Dim tmp As String = reader.ReadToEnd()
response.Close()
Response.Write(tmp)
End If
End Sub

If you try to run the code, you can see that all the HTML code from Microsoft site has been grabbed and display on your local web server.

2. Sample Code on how to Post Data to remote Web Page using HttpWebRequest

C#
protected void Page_Load(object sender,EventArgs e) {
Uri uri = new Uri("http://www.amazon.com/exec/obidos/search-handle-form/102-5194535-6807312");string data = "field-keywords=ASP.NET 2.0";if (uri.Scheme == Uri.UriSchemeHttp)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());string tmp = reader.ReadToEnd();
response.Close();
Response.Write(tmp);
}
}

VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadDim uri As New Uri("http://www.amazon.com/exec/obidos/search-handle-form/102-5194535-6807312")
Dim data As String = "field-keywords=ASP.NET 2.0"If uri.Scheme = uri.UriSchemeHttp Then
Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
request.Method = WebRequestMethods.Http.Post
request.ContentLength = data.Length
request.ContentType = "application/x-www-form-urlencoded"
Dim writer As New StreamWriter(request.GetRequestStream)
writer.Write(data)
writer.Close()
Dim oResponse As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(oResponse.GetResponseStream())
Dim tmp As String = reader.ReadToEnd()
oResponse.Close()
Response.Write(tmp)
End If
End Sub

Conclusion

Based on  the sample code above, you can see that it is quite simple to do Http Post and Http Get to remote Website by using two built in class HttpWebRequest and HttpWebResponse. There is another set of classes called FtpWebRequest and FtpWebResponse that allow you to do ftp post and get to remote ftp Server.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值