zoukankan      html  css  js  c++  java
  • 用c语言实现http请求

    用c语言来实现一个简单的http请求,请求 www.cnblogs.com 首页

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>    //strlen
    #include<sys/socket.h>
    #include<arpa/inet.h> //inet_addr
    #include<netdb.h>
    #include<errno.h>
    
    int main(int argc, char *argv[])
    {
        int socket_desc;
        struct sockaddr_in server;
        char *message;
    
        //Create socket
        socket_desc = socket(AF_INET, SOCK_STREAM , 0);
        if (socket_desc == -1) {
            printf("Could not create socket");
        }
    
        char ip[20] = {0};
        char *hostname = "www.cnblogs.com";
        struct hostent *hp;
        if ((hp = gethostbyname(hostname)) == NULL) {
            return 1;
        }
        
        strcpy(ip, inet_ntoa(*(struct in_addr *)hp->h_addr_list[0]));
    
        server.sin_addr.s_addr = inet_addr(ip);
        server.sin_family = AF_INET;
        server.sin_port = htons(80);
    
    
        //Connect to remote server
        if (connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
            printf("connect error: %s", errno);
            return 1;
        }
    
        puts("Connected
    ");
    
        //Send some data
        //http 协议
        message = "GET / HTTP/1.1
    Host: www.cnblogs.com
    
    ";
    
        //向服务器发送数据
        if (send(socket_desc, message, strlen(message) , 0) < 0) {
            puts("Send failed");
            return 1;
        }
        puts("Data Send
    ");
    
        struct timeval timeout = {3, 0};
        setsockopt(socket_desc, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));
    
        //Receive a reply from the server
        //loop
        int size_recv, total_size = 0;
        char chunk[512];
        while(1) {
            memset(chunk , 0 , 512); //clear the variable
            //获取数据
            if ((size_recv =  recv(socket_desc, chunk, 512, 0) ) == -1) {
                if (errno == EWOULDBLOCK || errno == EAGAIN) {
                    printf("recv timeout ...
    ");
                    break;
                } else if (errno == EINTR) {
                    printf("interrupt by signal...
    ");
                    continue;
                } else if (errno == ENOENT) {
                    printf("recv RST segement...
    ");
                    break;
                } else {
                    printf("unknown error: %d
    ", errno);
                    exit(1);
                }
            } else if (size_recv == 0) {
                printf("peer closed ...
    ");
                break;
            } else {
                total_size += size_recv;
                printf("%s" , chunk);
            }
        }
    
        printf("Reply received, total_size = %d bytes
    ", total_size);
        return 0;
    }

    编译: 

    gcc http.c -o http

    运行:

    ./http

    就可以看到返回结果了

    == just do it ==
  • 相关阅读:
    无线鼠标换电池了
    Jython Interactive Servlet Console YOU WILL NEVER KNOW IT EXECLLENT!!! GOOD
    Accessing Jython from Java Without Using jythonc
    jython podcast cool isnt't it?
    Python里pycurl使用记录
    Creating an Interactive JRuby Console for the Eclipse Environment
    微软为AJAX和jQuery类库提供CDN服务
    Download A File Using Cygwin and cURL
    What is JMRI?这个是做什么用的,我真没看懂但看着又很强大
    用curl 发送指定的大cookie的http/https request
  • 原文地址:https://www.cnblogs.com/jiujuan/p/9399165.html
Copyright © 2011-2022 走看看