A c ++ http get code

Source: Internet
Author: User

The Code is as follows (from: http://bbs.csdn.net/topics/320012075 ):

#include <winsock2.h>#include<windows.h>#include<stdio.h>#pragma comment(lib, "ws2_32.lib") int main(int argc, char* argv[]){    char server_name[256]= "www.163.com";     WSADATA wsaData;    struct sockaddr_in srv_addr;     if (WSAStartup(0x202,&wsaData) == SOCKET_ERROR) {        WSACleanup();        return -1;    }     SOCKET cli_sock=socket(PF_INET,SOCK_STREAM,0);    if (cli_sock==INVALID_SOCKET)        return -1;     // connect to server     srv_addr.sin_family = AF_INET;    LPHOSTENT lphost = gethostbyname(server_name);    if (lphost != NULL)        srv_addr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;    srv_addr.sin_port= ntohs(80);    if (connect(cli_sock,(LPSOCKADDR)&srv_addr,sizeof(srv_addr))==SOCKET_ERROR){        return -1;    }     char sz[]="GET / HTTP/1.1\r\nHost:www.163.com\r\n\r\n";    int retval = send(cli_sock,sz,sizeof(sz)-1,0);    if( SOCKET_ERROR == retval )        return 0;    char buffer[1024]={0};    while( SOCKET_ERROR != retval ){        retval = recv(cli_sock ,buffer,1000,0);        printf("%s\n",buffer);    }    closesocket(cli_sock);    return 0;}

Run in vc6.

The format of the http get request message.

For example, convert the following URL into a GET request message:
Http: // 192.168.11.240/list. php? Username = xyq@yahoo.com

Convert:

Get (Space)/list. php? Username = xyq@yahoo.com (Space) HTTP/1.1 (\ r \ n)
HOST: 192.168.11.240 (\ r \ n)
Accept: */* (\ r \ n)
User-Agent: MSIE 6.00 (\ r \ n)
Connection: keep-alive (\ r \ n)

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.