Send an HTTP request and obtain an HTTP response.

Source: Internet
Author: User
Tags microsoft iis

Send an HTTP request and obtain an HTTP response.

The process of sending an HTTP request and obtaining an HTTP response is as follows:

(1) define HTTP requests such as HttpPut (HttpPost/HttpGet/HttpDelete;

(2) define various headers and add them to HttpPut;

(3) define HttpEntitty, add the XML string to be sent to HttpEntity, and add HttpEntity to HttpPut;

(4) define HttpClient, execute HttpPut, and return HttpResponse;

(5) Get StatusCode from HttpResponse;

(6) obtain various headers from HttpResponse;

(7) Get HttpEntity from HttpResponse and get the string output.

The Code is as follows:

// Modify the public static void modify_queue_attributes () {// define HttpClient httpClient = new DefaultHttpClient (); // Uri String hs = host + "/" + queName + "? Metaoverride = true "; // defines HttpPut httpPut = new HttpPut (hs); // various headers httpPut. setHeader ("Date", time); System. out. println ("Date:" + time); httpPut. setHeader ("x-mqs-version", version); System. out. println ("x-mqs-version:" + version); httpPut. setHeader ("Authorization", auth); System. out. println ("Authorization:" + auth); httpPut. setHeader ("Content-Type", content_type); System. out. println ("Con Tent-Type: "+ content_type); // message body content (XML format) StringBuilder req = new StringBuilder (); String s = new String (""); s = "<? Xml version = "+" \ "" + "1.0" + "\" "+" encoding = "+" \ "" + "UTF-8" + "\" "+ "?> "; Req. append (s); s = "<Queue xmlns =" + "\" "+" http://mqs.aliyuncs.com/doc/v1/ "+" \ "" + ">"; req. append (s); s = "<VisibilityTimeout> 120 </VisibilityTimeout>"; req. append (s); s = "<MaximumMessageSize> 1024 </MaximumMessageSize>"; req. append (s); s = "<MessageRetentionPeriod> 120 </MessageRetentionPeriod>"; req. append (s); s = "<DelaySeconds> 60 </DelaySeconds>"; req. append (s); s = "</Queue>"; req. append (s); System. out. println (); System. out. println (req); // defines HttpEntity entity; try {// StringEntity entity = new StringEntity (req. toString (); // sets Entity HttpPut for httpPut. setEntity (entity); // HttpClient executes HttpPut and returns HttpResponse httpResponse = httpClient.exe cute (httpPut); // get StatusCode int statusCode = httpResponse. getStatusLine (). getStatusCode (); System. out. println ("Status Cod E: "+ statusCode); // obtain various HttpResponse Header [] Header; header = httpResponse. getHeaders (" Content-Length "); if (header. length! = 0) {// System. out. println (header [0]. toString ();} header = httpResponse. getHeaders ("Connection"); if (header. length! = 0) {// System. out. println (header [0]. toString ();} header = httpResponse. getHeaders ("Date"); if (header. length! = 0) {// System. out. println (header [0]. toString ();} header = httpResponse. getHeaders ("Server"); if (header. length! = 0) {// System. out. println (header [0]. toString ();} header = httpResponse. getHeaders ("x-mqs-request-id"); if (header. length! = 0) {// System. out. println (header [0]. toString ();} header = httpResponse. getHeaders ("x-mqs-version"); if (header. length! = 0) {// System. out. println (header [0]. toString ();} // get HttpEntity of HttpResponse and output HttpEntity httpEntity = httpResponse as a string. getEntity (); if (httpEntity! = Null) {InputStream instreams = httpEntity. getContent (); String str = ConvertStreamToString (instreams); System. out. println ("Response:" + "\ n" + str) ;}} catch (Exception e) {System. out. println ("Error =" + e. toString ());}}

The returned result is:

Status Code: 403Response:<?xml version="1.0"?><Error xmlns="http://mqs.aliyuncs.com/doc/v1">  <Code>SignatureDoesNotMatch</Code>  <Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>  <RequestId>53D60FE4048A936A361D8ABA</RequestId>  <HostId>http://huvaw6yih3.mqs-cn-hangzhou.aliyuncs.com</HostId></Error>

 


What content does HTTP contain? What content does the HTTP Response contain?

The content has many advantages.
I studied the protocol analysis during the summer vacation ..

HTTP is an essential protocol in our network. Next we will give an in-depth explanation of this issue. Then we will analyze the specific operations of the GET method to implement the HTTP protocol. Is HTTP used to send and receive messages over the Internet? HTTP is a request-response protocol ?? When a client sends a request, the server returns the response to the request. All requests and responses are HTTP packets? The HTTP protocol uses a reliable TCP connection. The default port is 80? The first version of HTTP is HTTP/0.9, and later it was developed to HTTP/1.0. The latest version is HTTP/1.1? What is HTTP/1.1 defined by RFC 2616?

In HTTP, the session between the Client and Server is always initialized by the Client by establishing a connection and sending an HTTP request packet. The Server does not actively contact the Client or require a connection with the Client? The connection between the browser and the server can be interrupted at any time. For example, when you browse a webpage, you can click the "stop" button at any time to interrupt the current file download process and close the HTTP connection with the Web server?

1. HTTP request package

HTTP Request Packet (GET? POST and other request methods) are composed of three parts: method-URI-Protocol/version, request header, request body? The following is an example of an HTTP request packet (GET:

1. GET/index. jsp HTTP/1.1

2. Accept-Language: zh-cn

3. Connection: Keep-Alive

4. Host: 192.168.0.106

5. Content-Length: 37

6. userName = new_andy & password = new_andy

The first line of the request packet is method-URI-Protocol/version:

GET is the request method. According to HTTP standards, how many request methods can be used for HTTP requests? HTTP 1.1 supports seven request methods: GET? POST? HEAD? OPTIONS? PUT? DELETE, TRACE, etc. The commonly used request methods are GET and POST?

/Index. jsp indicates URI? Does URI specify the network resource to be accessed? Is HTTP/1.1 A protocol and Protocol version?

The last line userName = new_andy & password = new_andy is the body, and the body is separated by an empty line (rn) in the HTTP header? Here, we need to describe one point. Content-Length indicates the Length of the body. Some body lengths are not described in the header, but only indicate Transfer-Encoding: chunked? For details about how to calculate the length of the chunked type, see RFC 1626?

The request packet header also contains a lot of useful information about the client environment and request body, which is not described here?

2. HTTP response packet

Similar to the HTTP request package, it consists of three parts: Protocol-status code-description, response header, and response body? The following is an example of an HTTP response:

1. HTTP/1.1 200 OK

2. Server: Microsoft-Microsoft IIS/4.0

3. Date: Mon, 3 Jan 2005 13:13:33 GMT

4. Content-Type: text/html

5. Last-Modified: Mon, 11 Jan 2004 13:23:42 GMT

6. Content-Length: 90

7.
8.
9. <title ...... remaining full text>

Which of the following statements about HTTP and response is true ()

D
A: get/post
B200 is normal
Cget is
 

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.