very strange problem, don't have any idea. Maybe you can help again - as so often :)
I create a simple UrlConnection and use the post-method. When I look into wireshark, everything is send right to me. I try to store the response into a string. And that string is a short version of the entire packet while it is closed right (with a /html-tag).
A diff in notepad gives me like this:
<a href="wato.py?mode=..."></a>
and in wireshark:
<a href="wato.py?mode=edithost&host=ColorPrinter ... muchmuchmore ...
This is the place where it seems to get its cut
Really strange stuff, now this is my code:
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;
}
Mysterious, I don't have the slightest idea. Can you help me?
Does using IOUtils help?
URLConnection connection = new URL(sUrl).openConnection();
IOUtils.toString(connection.getInputStream(), "UTF-8");
or even:
IOUtils.toString(new URL(sUrl), "UTF-8");
Even if not, always consider it first to reduce the amount of boilerplate in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With