C # implements a simple HTTP request instance

Source: Internet
Author: User

The example in this article describes how C # implements a simple HTTP request. Share to everyone for your reference. The specific analysis is as follows:

Pass. NET of two classes HttpWebRequest class, HttpWebResponse class to implement HTTP request, response processing.

The first small test is to request the content of Baidu homepage (http://www.baidu.com), that is, to get the HTML content of Baidu homepage,

Implementation steps:

1. Create an HttpWebRequest object from the WebRequest class that can contain HTTP request information.

(Here's a bit of a thought: Why create this object through the parent class WebRequest, not the new one HttpWebRequest, the constructor in the HttpWebRequest class is: protected HttpWebRequest (SerializationInfo SerializationInfo, StreamingContext StreamingContext); )

2. Setting the HttpWebRequest object is actually the information content of the HTTP request message.

3. Gets the HttpWebResponse object from the HttpWebRequest object that contains the HTTP response information.

4. Get response header information and response body information from the response information.

Some implementation codes are as follows:

Create HttpWebRequest request, set request message information


The code is as follows:

Request

string uri = http://www.baidu.com;

HttpWebRequest request = Httpwebrequest.create (URI) as HttpWebRequest;

Request.                            Method = "GET"; Request method

Request.   ProtocolVersion = new Version (1, 1); http/1.1 version

Add other ...

Receive responses, output response header information, and body information


The code is as follows:


HttpWebResponse response=

Request. GetResponse () as HttpWebResponse;

Header

foreach (var item in response. Headers)

{

This.txt_Header.Text + = Item. ToString () + ":" +

Response. getResponseHeader (item. ToString ())

+ System.Environment.NewLine;

}

Receives the principal information content if the principal information is not empty

if (response. ContentLength <= 0)

Return

Receive Response body information

using (Stream stream =response. GetResponseStream ())

{

int totallength= (int) response. ContentLength;

int numbytesread=0;

Byte[] Bytes=new byte[totallength+1024];

Read the data in a stream through a loop, read it, jump out of the loop

while (Numbytesread < totallength)

{

int Num=stream.  Read (bytes,numbytesread,1024); Every time you want to read 1024 bytes

if (num==0)//Description stream data Read complete

Break

Numbytesread+=num;

}

}

Display the received principal data to the interface

String content=encoding.utf8.getstring (bytes);

This.txt_content.text=content;

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # implements a simple HTTP request instance

This address: http://www.paobuke.com/develop/c-develop/pbk23192.html






Related Content C # multi-thread Semaphore usage WinForm C # Subform Close refreshes the parent form instance C # with Java MD5 Simple validation (instance code) C # Read and Write app. config, Configurationmanager.appsettings Non-effective solution
C # Statistics C, C + + and C # program code line number method C # Set boot entry, cancel boot entry C # implement on front page pop-up warning dialog (alert) method C # Distributed transaction time-out processing instance analysis

C # implements a simple HTTP request instance

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.