【问题标题】:How can I call HttpUrlConnection disconnect()如何调用 HttpUrlConnection disconnect()
【发布时间】:2019-12-29 04:05:36
【问题描述】:

我的代码发送 HttpConnection,然后尝试使用 connection.getInputStream()connection.getErrorStream() 反序列化响应。

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // getErrorStream()
String content = in.lines().collect(Collectors.joining());

如何将连接传递给parse(HttpURLConnection connection) 函数并在完成后调用connection.disconnect()

我在想

connection.disconnect();
return MyObject.fromString(connection.getInputStream(), connection.getErrorStream());

但不确定在阅读响应之前断开连接是否有意义。

【问题讨论】:

    标签: java httprequest httpurlconnection nshttpurlresponse


    【解决方案1】:

    你可以这样做:

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    String content = parse(connection);
    connection.disconnect();
    // do something with content
    

    【讨论】:

      猜你喜欢
      • 2012-03-24
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多