Webrequest, webresponse, httpwebresponse, httpwebresponse, WebClient notes

Source: Internet
Author: User

1. webrequest, webresponse and httpwebrequest, httpwebresponse

Webrequest is an abstract class. Both httpwebrequest and ftpwebrequest are subclasses of webrequest.

Webresponse is also an abstract class, and both httpwebresponse and ftpwebresponse are its subclasses.

 

2. webrequest and webresponse example

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. net;
Using system. IO;

Namespace windowsformsappwebrequest
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
{
Uri uri = new uri (this. textbox2.text );

Webrequest Req = webrequest. Create (URI );

// Req. headers. Add ("User-Agent", "ie9 ");

Webresponse respose = Req. getresponse ();

Stream receivestream = respose. getresponsestream ();

Streamreader sr = new streamreader (receivestream );

String resultstring = Sr. readtoend ();

This. textbox1.text = resultstring;
}
}
}

 

3. Example of httpwebrequest and httpwebresponse completing the same function

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. net;
Using system. IO;

Namespace windowsformsappwebrequest
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
{
Uri uri = new uri (this. textbox2.text );

Httpwebrequest Req = httpwebrequest. Create (URI) as httpwebrequest;

Req. headers. Add ("User-Agent", "ie9 ");

Httpwebresponse respose = Req. getresponse () as httpwebresponse;

Stream receivestream = respose. getresponsestream ();

Streamreader sr = new streamreader (receivestream );

String resultstring = Sr. readtoend ();

This. textbox1.text = resultstring;
}
}
}

4. What is the difference between webrequest and httpwebrequest?

It is the special feature of HTTP, such

Req. headers. Add ("User-Agent", "ie9 ");

It can be used in httpwebrequest, but not in webrequest.

 

5. The ftpwebrequest and ftpwebresponse examples will be supplemented later, but the principle is the same.

 

6. Example of WebClient

 

WebClient client = new WebClient ();
Client. baseaddress = textbox2.text;
Stream OBJ = client. openread ("/");
Streamreader READ = new streamreader (OBJ );
This. textbox1.text = read. readtoend ();

 

7. Comparison between WebClient and httpwebrequest

What are the differences between webrequest and httpwebrequest? Take a lookSource codeYou can see that the WebClient actually calls the webrequest method to complete the corresponding functions.

According to Titi, "the. NET Framework already provides two powerful classes for us: webrequest and webresponse to process resources labeled with Uris and obtain data. The disadvantage is that the settings are too complex when webrequest and webresponse are used.
It is quite difficult to use. With the current WebClient, WebClient can be understood as an encapsulation of collaboration such as webrequest and webresponse. It makes it easier and more convenient for people to use, and then it has inherent limitations.

That is, the lack of support for cookies/sessions, the lack of user control over automatic URL redirection, and the lack of support for proxy servers ."

 

Recommended materials:

Determining whether the device is connected
Http://www.cnblogs.com/tuyile006/archive/2008/11/14/1333632.html

WebClient Study Notes (1) -- Understanding webclien

Thttp: // www.cnblogs.com/titi/archive/2005/11/20/280914.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.