HTTP one ask a answer

Source: Internet
Author: User

1. What is the basic process of initiating a request and getting a response when a user browses the site? Why do users type a URL that often initiates multiple requests?

First make a very simple page, its content is only one line:
Put it on the Web server, such as IIS, and then use IE browser to request this page (http://localhost:8080/simple.htm), when we request this page, the browser actually did the following four things:
1 Parse the address we entered, from which to decompose the protocol name, hostname, port, object path and other parts, for our address, the result of the resolution is as follows:
Protocol Name: HTTP
Host name: localhost
Port: 8080
Object path:/simple.htm
2 The above part combined with the information of the machine itself, encapsulated into an HTTP request packet
3 Connect to the host's specified port (localhost, 8080) using the TCP protocol and send the encapsulated packet
4 wait for the server to return data and parse the returned data, and then show it.

2. where the request line is in the request message, what is the format ?

HTTP requests and HTTP responses, both of which are called HTTP messages, include the message header and the body of the message.
The message header includes the message line, the entity header, the end-of-head identification, and the entity header includes the universal header, the request header (the response header), and the entity header.
The message body is the HTTP data body, which is called the HTTP Entity in RFC2616, and is not required. For example, HttpGet request, only the request header, no request body.

3.HTTP What versions are currently available

http/0.9 is obsolete. Only GET one request method is accepted, no version number is specified in the communication, and the request header is not supported. Because this version does not support the POST method, clients cannot pass too much information to the server.
Http/1.0 This is the first HTTP protocol version that specifies the version number in the newsletter and is still widely used, especially in proxy servers.
http/1.1 the current version. Persistent connections are adopted by default and work well with proxy servers. It also supports the simultaneous sending of multiple requests in a piped manner to reduce line load and increase transmission speed.

4.GET and Post What are the main differences between methods, get Where the requested parameter is placed in the request message, POST Where is the requested parameter placed in the request message?

Http://wenku.baidu.com/view/816f7029915f804d2b16c1ab.html

The parameter of the GET request is placed in the location of the request message, and the parameters of the post request are placed in the entity behind the information header of the request message;

A GET message has no message body, and the post message is a message body, and the content of the message body is the data to post.

Get: Is the information of the resource specified by the request URI as an entity, and if the request URI is only a data generation process, the final return in the response entity is the resource to which the result of the processing is directed, rather than the description of the processing process.

Post: Used to make a request to the destination server that accepts the entity attached to the request and treats it as an additional new subkey for the resource specified by the request URI in the request queue, and post is designed to implement the following functions in a uniform way:

1: Interpretation of existing resources

2: Send messages to bulletin boards, newsgroups, mailing lists, or similar discussion groups.

3: Commit data block

4: Extending the database with additional operations

As can be seen from the description above, get is a request to send data to the server, while post is a request to submit data to the server, the data to be submitted is in the entity behind the information header.

The Get method displays the value in the IE Address bar that indicates when you submitted it.

The Post method does not

1.GET method
The GET method commits data is unsafe, the data is placed in the request line, the client address bar is visible;
The data size for the GET method submission is limited to 255 characters
Get method can not set bookmark
2.POST method
POST method commits data security, data is placed inside the message body, client is not visible
There is no limit to the size of data submitted by the POST method
POST method to set bookmarks

The get operation does not have a body part, and if it is to be passed to the server information, it is in the URL.
and post, put, delete is the body part, if you want to pass information, put in the body.
However, when actually used, many did not follow the original meaning, just post as a complex and secure data transfer method, because the requested information is placed in the body, so safe, and rich.
The GET request puts the information in the URL, such as: Http://www.xxx.com/query?name=simon&male=m, which can be thought of as a server query for a message called Simon.

GET request gets the resource identified by the Request-uri. Note: The Get method should not be used in operations that produce "side effects", such as in Web apps. One of the reasons is that get can be accessed by web spiders and other casual.

  Post attaches new data (such as submitting a form or uploading a file) after the resource identified by Request-uri. The data is included in the request body. A POST request may result in the creation of new resources and/or modification of existing resources.

5. What is the general format of the query string?               What do you usually do when you encounter special characters such as spaces?

The query string is actually a parameter to the background that resembles the content in the POST request body.

The query string is appended to the URL of the page from the "?" Starting at the end of a string of characters, when the browser is redirected from one page to another page, the URL is accompanied by a query string to the destination page, so in the destination page can get the query string brought over the information.

1. Constructing the query string

The query string consists of the format of the property name = attribute value, and if there are multiple properties, use the "&" connection between the attributes, and do not leave spaces in the middle. For example, the following is a query string that comes with a URL:

String Url= "Webform2.aspx?name= Zhang Shan &no=1000";

Each property is of type string, and the property value is a string constant and does not need to be "bound." There are 2 properties in this query string, the first property is name, the property value is Zhang Shan, the second property is no, and the property value is 1000.

Because the entire URL and query string together form a string, if a query string is a property value of a variable, the variable must be a string type, then the entire string is connected by a few paragraphs. For example, the above example can be expressed;

String Name= "Zhang Shan";

String number= "1000";

String Url= "Webform2.aspx?name=" +name+ "&no=" +number;

2. Parsing the query string

When the query string is routed along with the URL to the destination page, it needs to parse its individual property values to be used by the program. The parsing format is:

Request.params["property name"]. For example, resolve the two attribute values for the previous example:

String name=request.params["Name"];

String number=request.params["No"];

3. Application of query strings

Query strings are easy to use, and common browsers and client devices can identify query strings. Generally, the length of a query string is limited to 255 characters, so it cannot carry too much information or carry non-string types of information, and because the query string can be seen directly from the address bar of the browser, its confidentiality is poor.

Some symbols cannot be passed directly in the URL, and if you want to pass these special symbols in the URL, they will be encoded. Some URL special symbols and encodings are listed in the following table.

Hexadecimal value

1. + URL + sign for space%2b

2. Spaces in the space URL can be used with the + number or code%20

3./separate directories and subdirectories%2f

4.? Separating the actual URLs and parameters%3f

5.% Specify special characters%25

6. # means bookmark%23

7. The delimiter between the parameters specified in the & URL%26

8. = value of the specified parameter in the URL%3d

For more information, refer to: http://www.jb51.net/article/21691.htm

A case received yesterday that the customer could not view the files uploaded before, check, found a messy file name has # ...-=

Because there is a pre-written JS function, so just began to want to open a new page in the HTML herf a function, but do not know because the HTML code of the page is written by the ASP line or because so the reference is not good .... I'm in. NET it's so good to use no problem ...-=) Well, it doesn't work so directly. = =+

Then checked the ASP inside directly there is a ready-made function can be called, even change the use of ... Orz

I have a cow in my face ....

Strpath=strpath.replace ("", "%20")

This will not happen when the <a href=strpath>url</a> is wrong. This method fails when there are more than 2 space in the URL!

The correct way is:

Strpath=Escape(strpath);

The Escape method returns a string value (in Unicode format) that contains the charstring content. All spaces, punctuation, accent marks, and other non-ASCII characters are replaced with%XX encoding, where xx equals the hexadecimal number that represents the character. For example, a space returns "%20".
Characters with values greater than 255 are stored in%UXXXX format.

URL handling of special characters in

When using URLs for parameter passing, some parameters or URL addresses with Chinese names (or special characters) are often passed, and conversion errors occur during background processing. In some pass-through pages use GB2312, and the Receive page uses UTF8, so that the received parameters may be inconsistent with the original. URLs that are encoded using the server-side UrlEncode function are not the same as the URLs encoded with the encodeURI function of client JavaScript.

JavaScript encodes text in 3 functions: Escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions: Unescape,decodeuri,decodeuricomponent

Encoding method in javascript: Escape () Method: encodes the specified string using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters are converted to the character encoding in the%xx format (XX equals the encoded 16-digit number of the character in the character set table). For example, the encoding for a space character is%20. The Unescape method is the opposite. Characters that are not encoded by this method: @ */+
encodeURI () Method: Converts the URI string into an escape format using the UTF-8 encoding format. Characters that will not be encoded by this method:! @ # $& * () =:/;? +
encodeURIComponent () Method: Converts the URI string into an escape format using the UTF-8 encoding format. Compared to encodeURI (), this method encodes more characters, such as characters. So if the string contains several parts of the URI, it cannot be encoded in this way, otherwise the URL will display an error after the/character is encoded. Characters that will not be encoded by this method:! * ( )
Therefore, for the Chinese string, if you do not want to convert the string encoding format into UTF-8 format (such as the original page and the target page charset is consistent), only need to use escape. If your page is GB2312 or other encoding, and the page that accepts the parameter is UTF-8 encoded, it is necessary to use encodeURI or encodeuricomponent.
In addition, Encodeuri/encodeuricomponent was introduced after javascript1.5, and escape is available in the javascript1.0 version.

1, passing parameters need to use encodeuricomponent, so that the combined URL will not be # and other special characters truncated.

For example: <script language= "JavaScript" >document.write (' <ahref= "http://passport.baidu.com/?logout&aid=7 &u= ' +encodeuricomponent ("http://cang.baidu.com/bruce42") + ' "> Exit </a> ');</script>

2, the URL to jump when you can use the whole encodeURI

For example: Location.href=encodeuri ("http://cang.baidu.com/do/s?word= Baidu &ct=21");

3, JS Use the data can use escape

For example: Search the history record of the Tibetan.

4, Escape to the Unicode value other than 0-255 to encode the output%u**** format, in other cases escape,encodeuri,encodeuricomponent encoding results are the same.

The most used should be encodeuricomponent, which is to convert the Chinese, Korean and other special characters into the utf-8 format of the URL encoding, So if you need to use encodeURIComponent to pass parameters to the background, you need the background decoding to Utf-8 support (the encoding in form is the same as the current page encoding method)

Escape does not encode characters with 69: *,+,-,.,/,@,_,0-9,a-z,a-z

encodeURI does not encode 82 characters:!,#,$,&, ', (,), *,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,a-z

encodeURIComponent does not encode 71 characters:!, ', (,), *,-,.,_,~,0-9,a-z,a-z

The following are the encoded values of the special words indicators in the URL that may be used in the URL:

Character

Meaning of special characters

URL encoding

#

Used to flag a specific document location

%23

%

Encode a special character

%25

&

Separating different value pairs of variables

%26

+

Spaces are represented in variable values

%2b

/

Represents a directory path

%2f

\

Represents a directory path

%5c

=

Used to connect keys and values

%3d

?

Represents the start of a query string

%3f

Space

Space

%20

.

Period

%2e

:

Colon

%3a

The project found that directly to the parameter part of the URL do encodeURI () encoding conversion, background servlet through Getparamater (), do not need the conversion can be directly obtained to the correct value. Note: The parameters are not used in Chinese, the frame is the struts frame.

6. What are the more common HTTP request methods besides get and post ?

The options request queries the server for performance, or queries for resource-related options and requirements. Returns the HTTP request methods that the server supports for a specific resource. You can also test the functionality of your server with a request to send a ' * ' to the Web server.

The HEAD request gets the response message header for the resource identified by Request-uri. Ask the server for a response that is consistent with the GET request, except that the response body will not be returned. This method allows you to obtain meta information contained in the response message header without having to transmit the entire response content.

  

  The PUT Request Server stores a resource and uses Request-uri as its identity. Uploads its latest content to the specified resource location.

  The delete request Server deletes the resource identified by the Request-uri.

  TRACE Requests the server to echo the received request information, echoing the requests received by the server, primarily for testing or diagnostics.

Connect is reserved for future use, and the http/1.1 protocol is reserved for proxy servers that can change connections to pipelines.

The head method is almost the same as the Get method, and for the response part of the head request, the information contained in the HTTP header is the same as the information obtained through the GET request. Using this method, you can obtain information about the resources identified by Request-uri without transmitting the entire resource content. This method is commonly used to test the validity of hyperlinks, whether they can be accessed, and whether they have been updated recently.

7. How is the request header and the request body differentiated, and what is the distinguishing mark?

When we design a communication protocol, the "message Header/Message body" segmentation method is very common, the message header tells the other party what the message is doing, the message body tells the other how to do. HTTP transmission of the message is also stipulated, each HTTP packet is divided into HTTP header and HTTP body two parts, the latter is optional, and the former is necessary. Whenever we open a webpage, right click on it, select "View Source file ", then see the HTML code is the message body of HTTP, then where is the message header? IE doesn't let us see this part, but we can see it by intercepting the packet.

The first empty line, "<CR>", is the message body, which is the boundary between the message header and the message body specified by HTTP.

8. What are the common headers in the request header? What is the role of host, Refere, user-agent, cookie ?

Host:www.t381.org tell the server which host name you want to access

Referer:http://www.ti23.org from which resource access server (often used for anti-theft chain)

User-agent:mozilla 4.0 tells the client software environment

Cookies: Data that can be brought to the server client

Accept:text/html,image used to tell the server that the data types supported by the client

Encoding supported by the ACCEPT-CHARASET:ISO-8859-1 client

Data compression formats supported by the Accept-encoding:gzip client

Languages supported by the ACCEPT-LANGUAGE:ZH-CN client

Connection:close/keep-alive after the request is closed or remains connected

Date:tue, Jul 18:30:30gmt request time

Response
http/1.1 OK status line, 200 is the status code means everything OK
Location:http://www.aaa.net with http302 status code to tell the client who to look for
server:microsoft-iis/5.0 tell the browser server the type
Compression format for Content-encoding:gzip data
Content-length: Tell the browser the length of the loopback data
Content-type:text/html tells the browser, the type of loopback data
Refresh: How long it takes to flush after
Content-disposition control lets browsers access resources in a downloadable way
Cache-contorl:no-cache controlling the browser's cache

9. What do the common response codes,403,404,502, and503 mean?

200OK//client request succeeded;

Bad Request//client requests have syntax errors and cannot be understood by the server

401 Unauthorized//request unauthorized, this status code must be used with the Www-authenticate header field

403Forbidden//server received the request, but refused to provide service;

404 NotFound//Request resource does not exist, eg: entered the wrong URL;

405 (Methodnot allowed) the resource to which a request is directed does not support the corresponding request method;

500Internal Server error//server unexpected errors occurred;

501 (notimplemented) server does not know or support the corresponding request method;

503Server unavailable//The server is currently unable to process client requests and may return to normal after a period of time
eg:http/1.1 OK (CRLF)

HTTP one ask a answer

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.