HTTPメソッドのGETで、サーバからページを取得する。
GET - HTTP | MDN
HTTPクライアントの作成(TCPクライアントサンプル):Geekなぺーじ
今度は簡単なHTTPクライアントを作成し、bgstation0.comのApacheを相手にindex.htmlを取得する。
GETの後に、取得するページのパス、そしてHTTPバージョン。
末尾の"\r\n"を2回繰り返すのがポイント。
このように書く。
$ vi http_client.c $ gcc http_client.c -o http_client $ ./http_client bgstation0.com 80 /index.html connect success. HTTP/1.1 200 OK Date: Thu, xx Jun 20xx xx:xx:xx GMT Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips Last-Modified: Tue, xx Mar 20xx xx:xx:xx GMT ETag: "1xx-5a2288575xxxx" Accept-Ranges: bytes Content-Length: 484 Connection: close Content-Type: text/html <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title id="title_index">B.G-STATION</title> </head> <body id="body_index" bgcolor="#ffffff" text="#7700cc" link="0000ff" alink="ff0000" vlink="ff00ff"> <div id="div_title_image" align="center"> <img src="/images/title.png" width="460px" height="140px" /> </div> <div id="div_link_top" align="center"> <a href="bgst/index.html">Top</a> </div> </body> </html> $
一部伏せたけど、だいたいこんな感じで返ってきた。
Sample/http/http_method/GET/src/http_method at master · bg1bgst333/Sample · GitHub