如果临时字符串非常大,我会得到 java.io.IOException: Errorwriting to server at getInputStream
String tmp = js.deepSerialize(taskEx);
URL url = new URL("http://"
+ "localhost"
+ ":"
+ "8080"
+ "/Myproject/TestServletUpdated?command=startTask&taskeId=" +taskId + "'&jsonInput={\"result\":"
+ URLEncoder.encode(tmp) + "}");
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
这是为什么呢? 此调用将转到 URL 中提到的 servlet。
最佳答案
使用 HTTP POST 方法,而不是将所有数据放在 URL 中进行 GET 方法。 URL的长度有上限,所以如果要发送任意长度的数据,需要使用POST方法。
您可能需要将 URL 修改为 http://localhost:8080/Myproject/TestServletUpdated
,然后放置其余内容
command = "startTask&taskeId=" + taskId + "'&jsonInput={\"result\":" + URLEncoder.encode(tmp) + "}"
在 POST 请求的正文中。
关于java - 获取 java.io.IOException : Error writing to server at getInputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10896568/