很奇怪的问题,没有任何想法。也许你可以再次帮助 - 正如经常:)Java:服务器响应在存储到字符串时被切断?
我创建了一个简单的URLConnection并使用-POST方法。当我看到wireshark时,一切都发送给我。我尝试将响应存储到一个字符串中。该字符串是整个数据包的简短版本,而它是正确关闭的(使用/ html-tag)。
记事本中的差异给了我这样的:
<a href="wato.py?mode=..."></a>
和Wireshark的:
<a href="wato.py?mode=edithost&host=ColorPrinter ... muchmuchmore ...
这是它似乎得到了切
真是很奇怪的东西的地方,现在这是我的代码:
public void uploadCsv(File csvFile) throws CsvImportException, IOException {
String sUrl = String.format(urlBaseWato, hostAddress);
String csvFileContent = readFile(csvFile);
ParamContainer params = new ParamContainer().addParam("a", "a")
.addParam("b", b)
.addParam("c", "c");
URLConnection connection = new URL(sUrl).openConnection();
postData(connection, params);
String resp = getResponse(connection); // <---- broken string here :(
...
}
-
private void postData(URLConnection con, ParamContainer params) throws IOException {
int cLen = params.getEncodedParamString().getBytes().length;
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches (false);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Cookie", authCookie.toString());
con.setRequestProperty("Content-Length", Integer.toString(cLen));
//Send request
DataOutputStream os = new DataOutputStream (con.getOutputStream());
os.writeBytes(params.getEncodedParamString());
os.flush();
os.close();
}
-
private String getResponse(URLConnection connection) throws IOException {
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
String response = "";
while ((line = in.readLine()) != null)
response +=line;
in.close();
return response;
}
神秘,我没有丝毫的想法。你可以帮我吗?
IOUtils作品,未经断了线。想知道为什么,因为这是一个简单的实现获得没有额外的库的答复。在所有其他情况下,它都可以工作只是创建一个答案,然后我会投票。谢谢! – 2013-02-18 08:26:18