从C代码中获取HTML内容

时间:2013-03-21 13:33:54

标签: html c get java-native-interface

我实现了一个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()));
    }

1 个答案:

答案 0 :(得分:2)

C没有任何功能可以方便地获得标准Java库之类的网站。

您可以使用libcurl这是一种相当方便的方法,或者自己在套接字中编写所有内容,在这种情况下,我要先熟悉C网络编程:Beej's Guide to Network Programming