C Socket Send HTTP request

Source: Internet
Author: User

#include <sys/types.h> #include <sys/socket.h> #include <sys/types.h> #include <stdio.h> # Include <netinet/in.h> #include <arpa/inet.h> int main () {int sockfd; int len; struct sockaddr_in address; int Result Char *strings= "get/svnup/rewrite.php http/1.1/r/nhost:"; Char ch; SOCKFD = socket (af_inet, sock_stream, 0); address.sin_family = af_inet; ADDRESS.SIN_ADDR.S_ADDR = inet_addr (""); Address.sin_port = htons (80); Len = sizeof (address); result = Connect (SOCKFD, (struct sockaddr *) &address, Len); if (result = = 1) {perror ("oops:client1"); return 1;} write (Sockfd,strings,strlen (strings)); while (read (sockfd,&ch,1)) printf ("%c", ch); Close (SOCKFD); return 1; }

HTTP Request Message

L HTTP Command://Method field, indicating that it is using a Get method

L URI:///url field, send request to the server where the site is saved.

L HTTP version://http protocol versions field, with http/1.1 version

L Accept://Indicates the list of media type ranges that can be accepted for request response.

L Accept-language://Limited The preferred language for the request response is simplified Chinese, otherwise the default value is used.

L Accept-encoding://limits the acceptable content encoding value in the response, indicating that the additional content is decoded in gzip,deflate.

L User-agent://define user agent, that is, the type of browser sending the request is mozilla/4.0

L Host:www.XX.com///r/n defines the host on which the target resides

L connection:keep-alive/r/n//Tell server to use persistent connection

HTTP Response Message

L HTTP version:http/1.1//server is http/1.1 version

L HTTP status:200//request is successful, information can be read, included in the response message

L Date://Refers to the time when the server retrieves the object from the file system, inserts it into the response message, and sends the response message

L Server://indicates that the engraved message is generated by a apache/2.0.52 server

L x-powered-by://Indicates the use of PHP (version) of the Dynamic Web page

L Set-cookie://

L Vary://

L Content-length://indicating the length of the entity

L Connection://Tell the client to remain connected after the message has been sent

L Content-type://Indicates that the object in the entity is an HTML document

L Binary Data://Binary

Note: In the response request from the server, we can see the relevant information of the access from the status code. Status codes represent response types and are commonly used in:

L 1XX Retention

L 2XX indicates that the request was successfully received

L 3XX requesting customers to further refine requests for completion

L 4XX Customer Error

L 5XX Server Error

Status code

Status information




The initial request has been accepted and the customer should continue to send the remainder of the request. (HTTP 1.1 new)


Switching protocols

The server converts the request to a different protocol (HTTP 1.1 new)



Everything is fine, and the answer document for Get and post requests follows.



The server has created the document, and the location header gives its URL.



The request has been accepted, but processing has not been completed.


Non-authoritative Information

The document has returned normally, but some of the answer headers may be incorrect because a copy of the document is being used (HTTP 1.1 is new).


No Content

The browser should continue to display the original document without a new document. This status code is useful if the user periodically refreshes the page and the servlet can determine that the user's document is new enough.


Reset Content

There is no new content, but the browser should reset what it displays. Used to force the browser to clear form input (HTTP 1.1 new).


Partial Content

The customer sent a GET request with a range header, and the server completed it (HTTP 1.1 new).


Multiple choices

The documents requested by the client can be found in multiple locations, which are already listed in the returned document. If the server wants to make a preference, it should be indicated in the location answer header.


Moved Permanently

The client requests a document elsewhere, the new URL is given in the location header, and the browser should automatically access the new URL.



Similar to 301, but the new URL should be considered temporary instead of permanent. Note that the corresponding state information in the HTTP1.0 is "moved temporatily".

When this status code appears, the browser can automatically access the new URL, so it is a useful status code.

Note that this status code can sometimes be replaced with 301. For example, if the browser incorrectly requests Http://host/~user (a trailing slash is missing), some servers return 301, and some return 302.

Strictly speaking, we can only assume that the browser will automatically redirect only if the original request is get. Please see 307.



Similar to 301/302, the difference is that if the original request was post,location headers the redirected target document specified should be fetched via get (HTTP 1.1 new).


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.