【发布时间】:2016-10-29 13:34:04
【问题描述】:
如何用 C 语言编写简单的 HTTP 请求和响应。
在 Java 中我可以使用:
String url = "http://www.google.com/search?q=mkyong";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
如何使用 C 语言实现?
我已经尝试过 Here : 但它不能正常工作。
大部分回复:
HTTP/1.1 301 Moved Permanently Location: http://www.google.co.in/ Content-Type: text/html; charset=UTF-8 Date: Mon, 27 Jun 2016 13:53:36 GMT Expires: Wed, 27 Jul 2016 13:53:36 GMT Cache-Control: public, max-age=2592000 Server: gws Content-Length: 221 X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>301 Moved</TITLE></HEAD><BODY> <H1>301 Moved</H1> The document has moved <A HREF="http://www.google.co.in/">here</A>. </BODY></HTML>
【问题讨论】:
-
“工作不正常”是什么意思?你尝试了什么,发生了什么?在您的问题中发布Minimal, Complete, and Verifiable example 本身就很好。
-
最好是复制粘贴当前的 C 代码而不是旧的 java 代码,并向我们解释您的期望和得到的结果
-
使用
libcurl。 -
您阅读收到的回复了吗?如果您尝试
www.google.co.in会怎样?
标签: c