简体   繁体   中英

C - How can I extract numbers from a char buffer?

My code sends a HTTP HEAD request to a website. The website then replies with its information. How can I extract the status code from the first line in the reply? the status code is the value after "HTTP/1.0" it is either 2xx or 3xx.

code:

#define BUF_LEN 2048

main(int argc, char *argv[])
{
    int csd;
    struct sockaddr_in server;
    struct hostent *server_host;
    int server_len;
    int string_size;
    short server_port;
    char req_buffer[BUF_LEN];
    char temp_buffer[BUF_LEN];
    char receiveBuffer[BUF_LEN];
    char resource_buffer[BUF_LEN];
    int t;

    if(argc!=2)
    {
        fprintf(stderr,"Usage: %s <website> \n",argv[0]);
        exit(EXIT_FAILURE);
    }

    server_host=gethostbyname(argv[1]);
    if (server_host == NULL)
    {
        herror("While calling gethostbyname()");
        exit(EXIT_FAILURE);
    }

    csd=socket(PF_INET, SOCK_STREAM, 0);
    if(csd<0)
    {
        perror("While calling socket()");
        exit(EXIT_FAILURE);
    }

    server.sin_family=AF_INET;
    memcpy(&server.sin_addr, server_host->h_addr_list[0], server_host->h_length);
    server.sin_port = htons(80);
    server_len=sizeof(server);
    t = connect(csd, (struct sockaddr *) &server, server_len);
    if (t<0)
    {
        perror("while connecting()");
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("connected to server\n\n");
    }

    sprintf(temp_buffer, "HEAD / HTTP/1.0\r\n");
    strcpy(req_buffer, temp_buffer);
    sprintf(temp_buffer, "HOST: %s\r\n", argv[1]);
    strcat(req_buffer, temp_buffer);
    sprintf(temp_buffer, "\r\n");
    strcat(req_buffer, temp_buffer);

    printf(req_buffer);

    write(csd, req_buffer, strlen(req_buffer));

    read(csd, receiveBuffer, BUF_LEN);

    printf(receiveBuffer);

    close(csd);
}
       #include <stdio.h>

       int main()
       {
           char* inputstr = "HTTP/1.0 200";
           int response = 0;
           /
            * On success, the sscanf returns the number of variables filled. 
            * In the case of an input failure before any data could be
            * successfully read, EOF is returned. 
            */
           if(sscanf (inputstr, "HTTP/1.0 %d", &response) == 1)
               printf ("response code %d\n",response);
           else
               printf ("It failed");
           return 0;
      }

Library function sscanf(). Example:

string the_string = "foo -3.6 fum dum 17";
sscanf(the_string, "foo %f fum dum %d", &f, &i);

I cannot make specific example because I did not right understand how get the instruction built

You can use curl.h

#include <curl/curl.h>

int main(void) {
    CURL *curl = curl_easy_init();
    if (curl) {
        CURLcode res;
        FILE *f = fopen("/dev/null", "wb");
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
        curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
        res = curl_easy_perform(curl);
        if (res == CURLE_OK) {
            long response_code;
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
            printf("response_code %ld", response_code);
        }
        curl_easy_cleanup(curl);
    }
    return 0;
}

test

gcc main.c -lcurl
./a.out
response_code 200⏎   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM