简体   繁体   中英

The purpose of HTTPWebRequest and HTTPWebResponse

I'm doing research into web programming in ASP .NET and came across these two classes. 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.

You can create a HttpWebRequest object by doing:

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

And you invoke it to retrieve an 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 ).
Using HttpWebRequest you define a request to a URI using the HTTP-protocol. 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.

This is an example from 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 .

See HttpWebRequest and HttpWebResponse .

They are used for communicating with another process using the HTTP protocol.

In the context of ASP.NET, your process could use them to talk to another service. Maybe your database uses the HTTP protocol, such as CouchDB. Perhaps you have a rest service that your ASP.NET application needs to talk to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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