简体   繁体   中英

C language, get HTML source

I'm trying to get the HTML of this page http://pastebin.com/raw/7y7MWssc using C. So far I'm trying to connect to pastebin using sockets & port 80, and then use a HTTP request to get the HTML on that pastebin page.

I know what I have so far is probably WAY off, but here it is:

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

int main()
{
    /*Define socket variables */
    char host[1024] = "pastebin.com";
    char url[1024] = "/raw/7y7MWssc";
    char request[2000];
    struct hostent *server;
    struct sockaddr_in serverAddr;
    int portno = 80;

    printf("Trying to get source of pastebin.com/raw/7y7MWssc ...\n");

    /* Create socket */
    int tcpSocket = socket(AF_INET, SOCK_STREAM, 0);
    if(tcpSocket < 0) {
        printf("ERROR opening socket\n");
    } else {
        printf("Socket opened successfully.\n");
    }

    server = gethostbyname(host);
    serverAddr.sin_port = htons(portno);
    if(connect(tcpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) {
        printf("Can't connect\n");
    } else {
        printf("Connected successfully\n");
    }

    bzero(request, 2000);
    sprintf(request, "Get %s HTTP/1.1\r\n Host: %s\r\n \r\n \r\n", url, host);
    printf("\n%s", request);

    if(send(tcpSocket, request, strlen(request), 0) < 0) {
        printf("Error with send()");
    } else {
        printf("Successfully sent html fetch request");
    }
    printf("test\n");

}

The code above made sense to a certain point, and now I'm confused. How would I make this get the web source from http://pastebin.com/raw/7y7MWssc ?

Fixed, i needed to set add

serverAddr.sin_family = AF_INET;

and bzero serverAddr, and also my HTTP request was wrong, it had an extra /r/n and spaces, like @immibis said.

Corrected:

sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", url, host);

You are getting the pointer returned by gethostbyname() but you weren't doing anything with it.

You need to populate the sockaddr_in with the address, domain and port.

This works...but now you need to worry about obtaining the response...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

int main()
{
    /*Define socket variables */
    char host[1024] = "pastebin.com";
    char url[1024] = "/raw/7y7MWssc";
    char request[2000];
    struct hostent *server;
    struct sockaddr_in serverAddr;
    short portno = 80;

    printf("Trying to get source of pastebin.com/raw/7y7MWssc ...\n");

    /* Create socket */
    int tcpSocket = socket(AF_INET, SOCK_STREAM, 0);
    if(tcpSocket < 0) {
        printf("ERROR opening socket\n");
        exit(-1);
    } else {
        printf("Socket opened successfully.\n");
    }

    if ((server = gethostbyname(host)) == NULL) {
        fprintf(stderr, "gethostbybname(): error");
        exit(-1);
    }

    memcpy(&serverAddr.sin_addr, server -> h_addr_list[0], server -> h_length);
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(portno);

    if(connect(tcpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) {
        printf("Can't connect\n");
        exit(-1);
    } else {
        printf("Connected successfully\n");
    }

    bzero(request, 2000);
    sprintf(request, "Get %s HTTP/1.1\r\n Host: %s\r\n \r\n \r\n", url, host);
    printf("\n%s", request);

    if(send(tcpSocket, request, strlen(request), 0) < 0) {
        printf("Error with send()");
    } else {
        printf("Successfully sent html fetch request");
    }
    printf("test\n");

}

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