• httpurlconnection get String


    public static String getJSON(String url, int timeout) {
    HttpURLConnection c = null;
    try {
    long requestStart = System.currentTimeMillis();
    URL u = new URL(url);
    c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setRequestProperty("Content-length", "0");
    c.setUseCaches(false);
    c.setAllowUserInteraction(false);
    c.setConnectTimeout(timeout);
    c.setReadTimeout(timeout);
    c.connect();
    int status = c.getResponseCode();

    switch (status) {
    case 200:
    case 201:
    BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
    sb.append(line+" ");
    }
    br.close();
    return sb.toString();
    }
    long requestStop = System.currentTimeMillis();
    StatisticsUtil.networkEvent(SysConstant.CONVERT_TOKEN_HOST,
    status,
    requestStart, requestStop,
    StatisticsUtil.calRequestSize(url, mapHeaders),
    StatisticsUtil.calResponseSize(httpResponse, result),
    statusCode, serverMessage);

    } catch (MalformedURLException ex) {
    } catch (IOException ex) {
    } finally {
    if (c != null) {
    try {
    c.disconnect();

    } catch (Exception ex) {
    }
    }
    }
    return null;
    }
  • 相关阅读:
    微信小程序支付完整示例
    二分查找,冒泡排序, 快速排序
    JQuery放大镜效果
    js瀑布流
    linux安装字体
    使用phantomjs截图【php】
    安装supervisor
    mysql报错:BIGINT UNSIGNED value is out of range
    curl https报错: curl: (35) SSL connect error
    给www用户添加登录权限
  • 原文地址:https://www.cnblogs.com/qiyongqiang/p/4568462.html
Copyright © 2020-2023  润新知