Search code examples
htmlcgetjava-native-interface

get HTML content from C code


I implemented a Java code that can send a request to a remote website and retrieve data from it. But I want the same code in C, but I can't find so much help in the C library. Can any body give me any hints ?

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()));
    }

Solution

  • C does not have any functionality to conveniently get a web site like the standard Java libraries.

    You can use libcurl which is a fairly convenient way of doing it or writing everything in sockets yourself in which case I'd say familiarise yourself with C network programming first: Beej's Guide to Network Programming