HTTP数据接口

public static void main(String[] args) throws Exception {
//方法一
//System.out.println((new Test()).getURLContent());
String urlStr = “http://117.78.41.224:8033/ServerHandler.ashx?key=SY2019122311&method=hourdata&maxid=8820000”;
System.out.println((new Test()).getURLContent(urlStr) + “(11111111111)”);
// System.out.println(getURLContent()+"(11111111111)");
}

public static String getURLContent() throws Exception {
    String strURL = "http://117.78.41.224:8033/ServerHandler.ashx?key=SY2019122311&method=hourdata&maxid=8777386";
    URL url = new URL(strURL);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setRequestMethod("GET");
    httpConn.connect();
    BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
    String line;
    StringBuffer buffer = new StringBuffer();
    while ((line = reader.readLine()) != null) {
        buffer.append(line);
    }
    reader.close();
    httpConn.disconnect();

    System.out.println(buffer.toString());
    return buffer.toString();
}

/**
 * 程序中访问http数据接口
 */
public static String getURLContent(String urlStr) {
    /** 网络的url地址 */
    URL url = null;
    /** http连接 */
    HttpURLConnection httpConn = null;
    /**//** 输入流 */
    BufferedReader in = null;
    StringBuffer sb = new StringBuffer();
    try {
        url = new URL(urlStr);
        in = new BufferedReader(new InputStreamReader(url.openStream(), "GBK"));
        String str = null;
        while ((str = in.readLine()) != null) {
            sb.append(str);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    String result = sb.toString();
    return result;
} 

你可能感兴趣的:(HTTP数据接口)