zoukankan      html  css  js  c++  java
  • 根据网页地址获取页面内容

    public JSONObject urlConn(String urlStr, String portStr){
    String port = getPort(portStr);
    urlStr = (urlStr != null)?(host + port + "/?" + urlStr):(host + port);
    int responseCode;

    try {
    url = new URL(urlStr);
    conn = (HttpURLConnection) url.openConnection();
    responseCode = conn.getResponseCode();//获取返回码

    if( responseCode == HttpURLConnection.HTTP_OK ){
    is = conn.getInputStream();//获取输入流
    //读取数据流
    bufferedReader = new BufferedReader(new InputStreamReader(is));
    //建立字符串操作对象
    builder = new StringBuilder();
    String line = "";
    while( (line = bufferedReader.readLine()) != null ){
    builder.append(line);
    }

    bufferedReader.close();
    is.close();

    //json解析
    jsonObject = new JSONObject(builder.toString());
    }
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (JSONException e) {
    e.printStackTrace();
    }

    return jsonObject;
    }

  • 相关阅读:
    2-3-4 tree留坑
    CCPC final Cockroaches
    对拍模板
    使用cronolog按日期分割日志
    linux git 命了
    变量加减乘除运算
    根据pom标签修改
    根据符号获取字符
    shell循环字符串数组
    git ssh key配置
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2446121.html
Copyright © 2011-2022 走看看