C++ 做 http 访问

运营提过来一个合作需求, 用 HTTP-GET 方式请求验证合作方账号密码信息。 http 是基于 TCP 的应用层协议, 所以就建了一个 TCP 连接, 发送 "GET <URL> HTTP/1.0/r/n/r/n",  并读取全部回复. 

 

上 Demo 代码:

 

int main(int argc, char** argv)
{
        char *p_domain = NULL;
        if (argc <= 1)
        {
                char *p_default_domain = "www.google.com";
                p_domain = (char*)malloc(128);
                memcpy(p_domain, p_default_domain, strlen(p_default_domain));
        }
        else
        {
                p_domain = argv[1];
        }
        printf("Domain is %s\n", p_domain);

        //get ip through domain
        struct hostent* p_hostent = NULL;
        if ((p_hostent = gethostbyname(p_domain)) == NULL)
        {
                printf("gethostbyname failed to host %s\n", p_domain);
                return -1;
        }

        struct sockaddr_in sin;
        memset(&sin, 0, sizeof(sin));
        sin.sin_family = AF_INET;
        sin.sin_port = htons((unsigned short)80);
        if (*(p_hostent->h_addr_list) == NULL)
        {
                printf("p_hostent is not NULL, but *(p_hostent->h_addr_list) == NULL\n");
                exit(-1);
        }

        char ip_char_arr[32];
        //inet_ntop(p_hostent->h_addrtype, *(p_hostent->h_addr_list), ip_char_arr, sizeof(ip_char_arr));
        //sin.sin_addr.s_addr = inet_addr(ip_char_arr);
        memcpy((void*)&(sin.sin_addr.s_addr), *(p_hostent->h_addr_list), sizeof(sin.sin_addr.s_addr));
        int s = socket(PF_INET, SOCK_STREAM, 0);
        if ( s < 0)
        {
                printf("create socket failed\n");
                exit(-1);
        }

        if (connect(s, (struct sockaddr*)&sin, sizeof(sin)) < 0)
        {
                printf("connect failed");
                exit(-1);
        }

        //send http request
        std::string send_request;
        send_request += "GET ";
        send_request += p_domain;
        send_request += " HTTP/1.0\r\n\r\n";
        int send_result = send(s, send_request.c_str(), send_request.size(), 0);
        char buff[1024];
        std::string response;
        int n = 0;
        while( (n = read(s, buff, 1023)) > 0)
        {
                buff[n] = '\0';
                response += buff;
        }
        printf("Response is %s\n", response.c_str());
        close(s);
}



output:

Domain is www.google.com
Response is HTTP/1.0 302 Found
Location: http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1315665177263180&usg=AFQjCNHD0-1OOlEXgYZxoytxhz9IxIrdKg
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=58f308a4e432fc10:FF=0:NW=1:TM=1315665147:LM=1315665147:S=6OsYLlKw3Fe4FYBv; expires=Mon, 09-Sep-2013 14:32:27 GMT; path=/; domain=.google.com
Date: Sat, 10 Sep 2011 14:32:27 GMT
Server: gws
Content-Length: 376
X-XSS-Protection: 1; mode=block

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1315665177263180&usg=AFQjCNHD0-1OOlEXgYZxoytxhz9IxIrdKg">here</A>.
</BODY></HTML>


 

你可能感兴趣的:(C++,socket,struct,list,null,domain)