【发布时间】:2013-03-21 13:33:54
【问题描述】:
我实现了一个 Java 代码,它可以向远程网站发送请求并从中检索数据。但是我想要 C 中的相同代码,但是我在 C 库中找不到这么多的帮助。任何机构都可以给我任何提示吗?
public static String getHTML(String urlToRead) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws IOException {
InetAddress thisIp = null;
try {
thisIp = InetAddress.getLocalHost();
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
System.out.println(getHTML("http://api.hostip.info/get_html.php?ip="
+ thisIp.getHostAddress()));
}
【问题讨论】:
-
提示:你必须实际编写一些 C。
-
您在转换时遇到了什么问题?正如马特所说,您在移植到 C 方面并没有真正表现出任何努力,而且您还没有提出具体的问题。
-
我找不到如何编写 url 并向其发出 GET 请求
-
这方面不会有 1:1 的情况。你必须自己做很多工作,所以确保你知道函数实际上调用了什么do
-
请参阅How can I retrieve content from a URL in C?,了解如何使用libcurl 执行此操作的示例。
标签: html c get java-native-interface