很奇怪的问题,不知道。也许你可以再次提供帮助 - 经常这样:)
我创建了一个简单的 UrlConnection 并使用 post-method。当我查看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;
}
莫名其妙,我一点头绪都没有。你能帮助我吗?