获得从C code HTML内容 [英] get HTML content from C code

查看:139
本文介绍了获得从C code HTML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个Java code,它可以从它请求发送到远程网站和检索数据。但我想在C相同的code,但我找不到在C库中这么多的帮助。任何机构可以给我任何提示?

 公共静态字符串getHTML(字符串urlToRead){
        URL网址;
        HttpURLConnection的康涅狄格州;
        RD的BufferedReader;
        串线;
        字符串结果=;        尝试{
            URL =新的URL(urlToRead);
            康恩=(HttpURLConnection类)url.openConnection();
            conn.setRequestMethod(GET);
            RD =新的BufferedReader(
                    新的InputStreamReader(conn.getInputStream()));
            而((行= rd.readLine())!= NULL){
                结果+ =行;
            }
            rd.close();
        }赶上(例外五){
            e.printStackTrace();
        }
        返回结果;
    }    公共静态无效的主要(字串[] args)抛出IOException
        InetAddress类thisIp = NULL;
        尝试{
            thisIp = InetAddress.getLocalHost();
        }赶上(E1的UnknownHostException){
            e1.printStackTrace();
        }
        的System.out.println(getHTML(http://api.hostip.info/get_html.php?ip=
                + thisIp.getHostAddress()));
    }


解决方案

C没有任何功能可以方便地得到一个网站就像标准的Java库。

您可以使用的libcurl 这是做或插座自己写的一切都相当方便的方法在这种情况下,我会说第一个用C网络编程熟悉: Beej指南网络编程

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

解决方案

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

这篇关于获得从C code HTML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆