【问题标题】:how to exceed return string of get method (httpurlconnection)如何超过get方法的返回字符串(httpurlconnection)
【发布时间】:2011-05-19 16:20:34
【问题描述】:

我使用 HTTPUrlConnection 和 GET 方法从服务器获取数据,返回字符串大约 13k 个字符,但我只收到 1228 个字符。

有什么办法可以接收所有字符吗?

这是我的代码:

URL con = new URL(url);
HttpURLConnection httpURLCon = (HttpURLConnection)con.openConnection();
DataInputStream inStream = new DataInputStream(httpURLCon.getInputStream());
Scanner sc = new Scanner(inStream);
String response = sc.next();
System.out.prinlnt(response.length());

【问题讨论】:

  • 向我们展示你在做什么怎么样?对您的 GET 请求的 HTTP 响应不受(或不应)长度限制。
  • 可能相关:HTTP URI GET limit
  • sc.next() 不会只给出第一个标记,即第一行吗?
  • 好的,我找到了我的解决方案,Captain Giraffe 是对的,sc.next() 只是给出了第一个令牌,如果第一行很长,第一个令牌不都是第一行(就像我的情况一样)。于是我把sc.next()改成sc.nextLine(),问题就解决了。谢谢!

标签: java get httpurlconnection


【解决方案1】:

您只是在读取输入的一行。

int contentLength = 0;
while( sc.hasNext() ){
    String aLine = sc.next();
    contentLength += aLine.length();
    // process the line would be a good idea here, perhaps appending a StringBuffer
}
System.out.prinlnt(contentLength);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-09
    • 2012-02-05
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多