Differences between HTTP protocol and POST and GET operations, and how to use POST and GET in C #

Source: Internet
Author: User
Tags html header



I think anyone who is familiar with the HTTP protocol can give a reason. But if I ask you what HTTP request methods are available? What is the difference between POST and GET? Is there a limit on the size of the data transmitted by GET or POST? What are the HTTP Response statuses? And how do you use it in C? If you cannot answer most of the questions clearly, this article is for you! The outline is as follows:

  • 1. HTTP Overview
    • 1.1 interaction between HTTP client and server
    • 1.2. HTTP message
    • 1.3. HTTP Request Method
    • 1.4 HTTP response code
  • 2. packet capture Analysis
  • 3. Differences between POST and GET
  • 4. Use an example to describe how to use POST, GET, and other operations in C #.
    • 4.1. HttpWebRequest
    • 4.2. HttpWebResponse
    • 4.3 compile a WinForm program and open the blog homepage (with source code)
1. HTTP Overview

To help you remember or understand the HTTP protocol, first let's look at the HTTP protocol.Hypertext Transfer Protocol(HTTP,HyperText Transfer Protocol) Is the most widely used network protocol on the Internet. All WWW files must comply with this standard. HTTP was designed to provide a method for publishing and receiving HTML pages.

The development of HTTP is the result of cooperation between the World Wide Web Consortium and the Internet team (Internet Engineering Task Force). They finally published a series of RFC, the most famous one is RFC 2616. RFC 2616 defines a widely used version of HTTP 1.1.

1.1 interaction between HTTP client and server

HTTP is a standard (TCP) for client and server requests and responses ).The client is an end user.,The server is a website. By using a Web browser, Web crawler, or other tools, the client initiates an HTTP request to the specified port on the server (the default port is 80. (We call this client) Call the user agent ). The response server stores (some) resources, such as HTML files and images. This response server is the origin server ). There may be multiple middle layers between the user proxy and the source server, such as the proxy, gateway, or tunnel ). Although TCP/IP is the most popular application on the internet, HTTP does not stipulate that it must be used and (based on) the layer it supports. In fact, HTTP can be implemented on any other Internet protocol or on another network. HTTP only assumes that (provided by its lower-layer protocol) reliable transmission, any protocol that can provide such assurance can be used by it.

Generally, an HTTP client initiates a request to establish a TCP connection to the specified port on the server (port 80 by default. The HTTP server listens to the requests sent from the client on that port. Once a request is received, the server (to the client) sends a status line, such as "HTTP/1.1 200 OK", and (response) message, the message body may be the requested file, error message, or other information.

The reason why HTTP uses TCP instead of UDP is that a webpage must transmit a lot of data, while TCP provides transmission control, organizes data in order, and corrects errors. Requests over HTTP or HTTPSResource Identifier(Uniform Resource Identifiers, or, more accurate, URI) to identify.

The structure and interaction process between the client and the server can be shown in the following two figures:

Figure 1. Web client-server structure (the hypertext link of the web server jumps to another server through a link on the website)

Figure 2 interaction between the Web client and the server

1.2. HTTP message

Two types of messages are used for client-to-server interaction:Request)AndResponse).

The HTTP request format is:

Figure 3. HTTP request format

The HTTP Response format is:

Figure 4. HTTP response format

From the above we can see that the HTTP request and Response Message Header both contain a variable number of fields, with a blank line (Blank line) Convert allHeader field(Header) andMessage Body(Body) separated.A header field consists of a field name and a colon, a space, and a field value. The field name is case insensitive..

Packet headers can be divided into three types: requests, responses, and descriptions. Some headers (such as Date) can be used for both requests and responses. The message header describing the subject can appear in the POST request and all response packets. Shows the HTTP header field:

Figure 5. HTTP header field

1.3. HTTP Request Method

HTTP/1.1 defines eight methods (sometimes called "actions") to indicateRequest-URIDifferent operations on the specified resource:

    Returns the HTTP Request Method supported by the server for a specific resource. You can also use the '*' request sent to the Web server to test the server's functionality.
  • HEAD
    Request the server for the same response as the GET request, but the response body will not be returned. This method can obtain metadata contained in the Response Message Header without transmitting the entire response content.
  • GET
    Send a request to a specific resource. Note: The GET method should not be used in operations that produce "Side effects", for example, in Web Application. One of the reasons is that GET may be randomly accessed by web spider.
  • POST
    Submits data to a specified resource for processing (for example, submitting a form or uploading a file ). Data is contained in the request body. POST requests may result in creation of new resources and/or modification of existing resources.
  • PUT
    Upload the latest content to the specified resource location.
    Request server DeletionRequest-URIResource.
    The request received by the echo server is mainly used for testing or diagnosis.
    The HTTP/1.1 protocol is reserved for proxy servers that can change connections to pipelines.

The method name is case sensitive. When the resource for a request does Not support the corresponding request Method, the server should return status code 405 (Method Not Allowed ); when the server does Not recognize or support the corresponding request method, status code 501 (Not Implemented) should be returned ).

The HTTP server should at least implement the GET and HEAD methods. Other methods are optional. In addition to the preceding methods, the specific HTTP server can also extend the custom methods.

Security Methods

Developers should be aware that their software represents a user's interaction on the Internet and should inform users that their ongoing operations may have an unexpected and important impact on themselves or others.

In particular, for the GET and HEAD methods, except for obtaining resource information, these requests should not have any other meaning. That is to say, these methods should be considered "safe", that isThe so-called security means that this operation is used to obtain information instead of modifying information.. The client should use other "insecure" methods, such as POST, PUT, and DELETE, in special ways (usually buttons rather than hyperlinks) so that the customer can be aware of the potential responsibilities (such as capital transactions brought by a button) or the requested operation may be insecure (for example, a file will be uploaded or deleted ).

However, it cannot be assumed that the server will not produce any side effects when processing a GET request. In fact, many dynamic resources use this as their feature. The important difference here is that the user does not request this side effect, so the user should not be responsible for these side effects.

Idempotent Method

If the side effects of several requests are the same as those of a single request without considering problems such as errors or expiration, or there is no side effects at all, these request methods can be considered as "idempotent. The GET, HEAD, PUT, and DELETE methods both have such idempotence attributes. Likewise, because neither the protocol, OPTIONS, or TRACE has any side effects, it is also a idempotence of course.

If the serial results of a request made up of several requests remain unchanged after the serial execution of the request or any or multiple of the requests are repeated, the request is serialized as "idempotent. However, the serial number of requests may be "non-idempotent", even if all the request methods executed in the request serial are idempotent. For example, the serial result of this request depends on a variable that will be modified during the next execution of this serial.

1.4 HTTP response code

The first line of the server program response is the status line. The status line starts with the HTTP version number, followed by three digits to indicate the response code, and finally is a readable response phrase. According to the first rule, the response can be divided into five categories:

Figure 6. HTTP response code

2. packet capture Analysis

Now we basically know about HTTP. Next I will use wireshark to capture the HTTP data packets during the interaction between my computer and the blog garden server when I open the blog garden homepage. Make preparations and close some programs that may interfere with our crawling and opening the blog garden. For example, when we enter www.cnblogs.com in the browser and confirm it, we first capture the following package:

Figure 7. Open the package captured in the blog Garden

We can see that we entered www.cnblogs.com in the browser and confirmed that an HTTP request message was sent to the server: GET/HTTP/1.1. According to the HTTP message format described in section 1.2, we know that GET correspondsRequest,/CorrespondingRequest-lineAnd HTTP/1.1Version Number. In addition to the request line, some header fields are sent, such as Accept, Accept-Language, User-Agent, Accept-Encoding, Host, and Connection. In addition, we can see that their format is:Header field name: Field ValueNote that there is a space behind the colon.

Next, let's take a look at the Response Message of the GET/HTTP/1.1 request:

Figure 8. Response Message of the GET/HTTP/1.1 request

The Response Message status line is: HTTP/1.1 200 OK.Expected version number, 200Response-codeAnd OKResponse-phrase. Besides the status line, some header fields are returned, such as Cache-Control, Content-Type, Content-Encoding, Expires, Last-Modified, Vary, and Server. (We can see that the blog uses IIS7.0)

The above is a GET packet. Now let's take a look at a POST packet. The classification information on the left is returned by the POST request when the homepage is opened.

Figure 9. POST Data Packets

We can see that POST/ws/PublicUserService. asmx/GetLoginInfo HTTP/1.1. Except for changing GET to POST, other information is similar. Below we can enlarge the sending header field:

Figure 10. header field of POST/ws/PublicUserService. asmx/GetLoginInfo HTTP/1.1

NOTE: I will not explain some of the header fields involved in this section here. I think here we should have a deeper understanding of HTTP.

3. Differences between POST and GET

8 methods are introduced in section 1.3. GET and POST are the most basic and common methods. The differences between get and post methods in Form submission are summarized as follows:

  • GET gets data from the server, and POST transfers data to the server.
  • GET adds the parameter data queue to the URL referred to by the ACTION attribute of the submission form. The values correspond to each field in the form one by one and can be seen in the URL. POST uses the http post mechanism to place fields in the form and their content in the html header and send them to the URL address referred to by the ACTION attribute. You cannot see this process.
  • For the GET method, the server uses Request. QueryString to obtain the value of the variable. For the POST method, the server uses Request. Form to obtain the submitted data.
  • The size of data transmitted by GET is small and cannot exceed 2 kb (this is mainly because the URL length is limited ). The amount of data transmitted by POST is large, which is generally not restricted by default. However, theoretically, the limit depends on the server's processing capability.
  • GET is less secure and POST is more secure. Because GET data is stored in the request URL during transmission, many existing servers, proxy servers, or user proxies record the request URL to the log file, and put it somewhere, so that some private information may be seen by a third party. In addition, you can directly view the submitted data in the browser. Some internal messages are displayed in front of the user. All POST operations are invisible to users.

If Method is not specified during FORM submission, the default value is GET request (. net is POST by default). The data submitted in Form will be appended to the url? Separated from the url. The letter and number characters are sent as they are, but spaces are converted to "+". Other symbols are converted to % XX, XX represents the ASCII (or ISO Latin-1) value in hexadecimal notation.The data to be submitted for the GET request is placed in the HTTP Request Header, while the data to be submitted by POST is placed in the object data. The data to be submitted by the GET method can contain up to 2048 bytes, POST does not have this restriction.. The parameters passed by POST are in the doc, that is, the text transmitted by the http protocol. When accepted, the parameter section is parsed. Obtain parameters. Generally, it is better to use POST. The data submitted by POST is implicit. GET is passed in the url to pass some data that does not need to be kept confidential. GET is passed through parameters in the URL, and POST is not.

Note: I checked the information of my predecessors on the Internet about the difference between POST and GET. Since I can't find the source and there are posts everywhere, I will not POST the relevant website here, baidu or Google.

4. Use an example to describe how to use POST, GET, and other operations in C #.

Before introducing an instance, we should first introduce HttpWebRequest and HttpWebResponse. in C #, we use these two classes to implement sending HTTP messages to the server and receiving HTTP responses from the server.

4.1. HttpWebRequest

Before designing an implementation instance, we should first introduce the HttpWebRequest class-provide specific HTTP implementations of the WebRequest class, And the HttpWebRequest class provides support for the attributes and methods defined in the WebRequest, it also supports additional attributes and methods that allow users to directly interact with servers using HTTP.

Do not use HttpWebRequest constructor. Use the System. Net. WebRequest. Create method to initialize the new HttpWebRequest object. If the Uniform Resource Identifier (URI) scheme isHttp ://OrHttps ://, Then Create returns the HttpWebRequest object.

The header field (headers) of the HTTP message, which is expressed as a public attribute in HttpWebRequest. The following table lists the HTTP headers set by properties or methods or by the system.

If the local computer configuration specifies to use a proxy, or if the request specifies a proxy, use a proxy to send the request. If no proxy is specified, the request is sent to the server.

The HttpWebRequest class mainly includes the following methods for interacting with HTTP servers:

  • Abort: cancels the Internet resource request.
  • AddRange: adds a range header to the request.
  • BeginGetRequestStream: starts an asynchronous request to the Stream object used to write data.
  • BeginGetResponse: starts asynchronous requests to Internet resources.
  • Create: initialize a new WebRequest. (Inherited from WebRequest .)
  • CreateDefault: initialize a newWebRequestInstance. (Inherited from WebRequest .)
  • CreateObjRef: creates an object that contains all the information required to generate a proxy for communication with a remote object. (Inherited from MarshalByRefObject .)
  • EndGetRequestStream: endStreamThe asynchronous request of the object.
  • EndGetResponse: ends an asynchronous request to Internet resources.
  • GetRequestStream: GetStreamObject.
  • GetResponse: returns a response from Internet resources.
  • GetSystemWebProxy: returns the proxy configured in the Internet Explorer settings of the current simulated user. (Inherited from WebRequest .)
  • InitializeLifetimeService: gets the lifetime service object that controls the lifetime policy of this instance. (Inherited from MarshalByRefObject .)
  • RegisterPrefix: register for the specified URIWebRequestChild. (Inherited from WebRequest .)
4.2. HttpWebResponse

Before designing an implementation instance, we also need to introduce the HttpWebRequest class-providing specific HTTP implementations of the WebResponse class. This class includes support for HTTP-specific usage of attributes and methods in the WebResponse class. The HttpWebResponse class is used to generate an HTTP independent client application that sends HTTP requests and receives HTTP responses.


Do not confuseHttpWebResponseAnd HttpResponse class; the latter is used for ASP. NET applications, and its methods and attributes are passed through the internal ASP. NETResponseObject exposed.

You must never directly create an instance of the HttpWebResponse class. Instead, use the instance returned by calling HttpWebRequest. GetResponse. You must call the Stream. Close method or HttpWebResponse. Close method to Close the response and release the connection for reuse. You do not need to callStream. CloseAndHttpWebResponse. CloseBut this will not cause errors.

Public header information returned from Internet resources is exposed as a property of this class. For a complete list, see the following table. Other Headers can be read as name/value pairs from the Headers attribute. The following table shows the public HTTP headers that can be used through the attributes of the HttpWebResponse class.

Call the GetResponseStream method to return the response content from Internet resources in the form of Stream.

The HttpWebRequest class mainly includes the following methods to interact with the HTTP server: (compared with the HttpWebRequest class, there are fewer methods)

  • CreateObjRef: creates an object that contains all the information required to generate a proxy for communication with a remote object. (Inherited from MarshalByRefObject .)
  • GetLifetimeService: retrieves the current lifetime service object that controls the lifetime policy of this instance. (Inherited from MarshalByRefObject .)
  • GetResponseHeader: Get the content of the header returned together with the response.
  • GetResponseStream: gets the stream, which is used to read the response body from the server.
  • InitializeLifetimeService: gets the lifetime service object that controls the lifetime policy of this instance. (Inherited from MarshalByRefObject .)
4.3 compile a WinForm program and open the blog homepage (with source code)

Through the introduction in the previous two sections, we have some knowledge about the HttpWebRequest and HttpWebRequest classes. Now we will use them to compile a small program for practice. The program interface is roughly as follows:

The function is also relatively simple, that is, you can click the "display in WebBrowser" button to display the blog garden homepage in the WebBrowser control below, click the "html source code" button to display the html source code of the homepage of the blog.

First, we will introduce how to implement it. By clicking the "html source code" button, a dialog box will pop up to display the html source code of the blog homepage. The core code is as follows:

 private string GetCnBlogs() { string html = String.Empty; HttpWebRequest cnbogs = (HttpWebRequest)System.Net.WebRequest.Create(txtURL.Text.ToString()); cnbogs.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, */*"; cnbogs.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; MALN; CIBA; InfoPath.2; .NET4.0C; .NET4.0E; Media Center PC 6.0; Tablet PC 2.0; AskTB5.6)"; cnbogs.Method = "GET"; HttpWebResponse cnblogsRespone = (HttpWebResponse)cnbogs.GetResponse(); if (cnblogsRespone !=null&&cnblogsRespone.StatusCode==HttpStatusCode.OK) { using(StreamReader sr = newStreamReader(cnblogsRespone.GetResponseStream())) { html = sr.ReadToEnd(); } } return html; }private void btnGetHtml_Click(object sender, EventArgs e) { MessageBox.Show(GetCnBlogs()); }

In fact, in this process, we enter the blog garden website in a browser to open the website with the same effect. However, here we implement it through the objects of the HttpWebRequest class and HttpWebRequest class.

However, by clicking the "display in WebBrowser" button, the function of displaying the homepage of the blog garden is similar in the WebBrowser control below, it is only displayed in the WebBrowser control. Here I encapsulate some common HTTP-related operations into a namespace Helper for future use, which is essentially the same as above. Click to download the source code of the entire project.

My source code is still relatively simple, but I simply implemented the interaction between the HttpWebRequest class and the HTTP server. More comprehensive functions are coming soon.

Additional instructions: For url length restrictions, the url of IE can contain a maximum of 2083 characters (half width), and The GET can contain a maximum of 2048 characters. However, RFC 2616, Hypertext Transfer Protocol -- HTTP/1.1 does not limit the maximum url length.


Refer:I have read many articles when writing this article.

  • Wikipedia (HTTP), http://zh.wikipedia.org/zh-cn/HTTP
  • MSDN, http://msdn.microsoft.com/zh-cn/library/8y7x3zz2%28v=VS.80%29.aspx
  • MSDN, http://msdn.microsoft.com/zh-cn/library/system.net.httpwebresponse%28VS.80%29.aspx
  • TCP/IP protocol details 3

Related Article

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.