My problem is that getInputStream() hangs when I try to open a stream after posting to a server. The server, written in python, gives an error message that leads me to believe that it is not able to parse my query string correctly. Please help me diagnose this problem.
I’m posting to a web server with the following code:
String boundarySolo = "--158211729626281";
String boundary = boundarySolo + "\r\n";
String contentHeader = "Content-Disposition: form-data; name=\"";
URL requestUrl;
URLConnection connection;
String queryString = new String();
try{
requestUrl = new URL(config.getServerUrl());
connection = requestUrl.openConnection();
connection.setReadTimeout(1000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundarySolo);
. . .
outStream = new DataOutputStream(connection.getOutputStream());
System.out.println("Writing bytes...");
outStream.writeUTF(queryString);
System.out.println("Bytes written");
outStream.flush();
System.out.println("Buffer flushed.");
outStream.close();
System.out.println("Stream closed.");
try{
inStream = new DataInputStream(connection.getInputStream());
String buffer;
System.out.println("Entering the loop.");
while((buffer = inStream.readLine())!= null)
System.out.println(buffer);
System.out.println("Leaving the loop.");
}
catch (SocketTimeoutException e) {
System.out.println("Socket timed out.");
}
The query string consists of this (with \r\n at the end of each line):
--158211729626281
Content-Disposition: form-data; name="view"
a
--158211729626281
Content-Disposition: form-data; name="termdb"
Saccharomyces_cerevisiae.etd
--158211729626281
Content-Disposition: form-data; name="raw_weights"
blank
--158211729626281
Content-Disposition: form-data; name="MAX_FILE_SIZE"
256
--158211729626281
Content-Disposition: form-data; name="cutoff_Evalue"
0.01
--158211729626281
Content-Disposition: form-data; name="min_term_size"
2
--158211729626281
Content-Disposition: form-data; name="effective_tdb_size"
0
--158211729626281
Content-Disposition: form-data; name="stats"
wsum
--158211729626281
Content-Disposition: form-data; name="transform_weights"
null
--158211729626281
Content-Disposition: form-data; name="cutoff_type"
none
--158211729626281
Content-Disposition: form-data; name="output"
tab
--158211729626281--
Try to test your payload with something like http://apikitchen.com/
and see what is returned.
The alternative would be to avoid doing this yourself altogether.
HTTPClient, Jersey Client and Resty (which I’m the author of) solve this problem without you having to dive into the depths of HTTP and mime types.
Here is a Resty example to get you started:
(Assuming that text/* is being returned by the server)