What's the easiest way to grab a web page in C?

99封情书 提交于 2019-12-06 06:18:28

I do have some code, but it also supports (Open)SSL so it's a bit long to post here.

In essence:

  • parse the URL (split out URL scheme, host name, port number, scheme specific part

  • create the socket:

    s = socket(PF_INET, SOCK_STREAM, proto);

  • populate a sockaddr_in structure with the remote IP and port

  • connect the socket to the far end:

    err = connect(s, &addr, sizeof(addr));

  • make the request string:

    n = snprinf(headers, "GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n", ...);

  • send the request string:

    write(s, headers, n);

  • read the data:

    while (n = read(s, buffer, bufsize) > 0) { ... }

  • close the socket:

    close(s);

nb: pseudo-code above would collect both response headers and data. The split between the two is the first blank line.

I'd look at libcurl if you want SSL support for or anything fancy.

However if you just want to get a simple webpage from a port 80, then just open a tcp socket, send "GET /index.html HTTP/1.0\n\r\n\r" and parse the output.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!