简体   繁体   English

HTTPWebRequest和HTTPWebResponse的目的

[英]The purpose of HTTPWebRequest and HTTPWebResponse

I'm doing research into web programming in ASP .NET and came across these two classes. 我正在研究ASP.NET中的Web编程,并且遇到了这两个类。 I was wondering what might these be used for? 我想知道这些可以做什么用?

My first thought is that they could be used if you were setting up a proxy between the client and server, but I'm not sure if this is the main purpose or not. 我首先想到的是,如果您要在客户端和服务器之间建立代理,则可以使用它们,但是我不确定这是否是主要目的。

Thanks 谢谢

edit: classes not methods 编辑:类而不是方法

They can indeed be used for that. 它们确实可以用于此目的。 This isn't specific to ASP.NET however. 但是,这并非特定于ASP.NET。

You can create a HttpWebRequest object by doing: 您可以通过执行以下操作创建HttpWebRequest对象:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://url.com");

And you invoke it to retrieve an HttpWebResponse: 然后调用它来检索HttpWebResponse:

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

There's a lot of customization you can do here, but hopefully this will give you a starting point for reading data off the web. 您可以在此处进行很多自定义,但是希望这将为您提供从网络读取数据的起点。

As their names imply, these are classes to either create a request ( HttpWebRequest ) or create a response ( HttpWebResponse ). 顾名思义,这些都是创建请求( HttpWebRequest )或创建响应( HttpWebResponse )的类。
Using HttpWebRequest you define a request to a URI using the HTTP-protocol. 使用HttpWebRequest可以使用HTTP协议定义对URI的请求。 Whereas the HttpWebResponse class delivers you an answer of an HTTP-server providing all the information like HTTP-headers and actual body of the request. HttpWebResponse类为您提供HTTP服务器的答案,该服务器提供所有信息,例如HTTP标头和请求的实际正文。

This is an example from MSDN 这是来自MSDN的示例

HttpWebRequest HttpWReq = 
(HttpWebRequest)WebRequest.Create("http://www.contoso.com");

HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
// Insert code that uses the response object.
HttpWResp.Close();

Consider the casting from the base-class WebRequest . 考虑从基类WebRequest的强制转换。

See HttpWebRequest and HttpWebResponse . 请参见HttpWebRequestHttpWebResponse

They are used for communicating with another process using the HTTP protocol. 它们用于使用HTTP协议与另一个进程进行通信。

In the context of ASP.NET, your process could use them to talk to another service. 在ASP.NET的上下文中,您的进程可以使用它们与另一个服务进行通信。 Maybe your database uses the HTTP protocol, such as CouchDB. 也许您的数据库使用HTTP协议,例如CouchDB。 Perhaps you have a rest service that your ASP.NET application needs to talk to. 也许您有一个ASP.NET应用程序需要与之交谈的休息服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM