F
Flip
I tried submitting a request via HttpURLConnection, and a weird thing
happened. I didn't get any activity on the server until I ran the line of
code that opened the input stream (InputStream rawInStream =
conn.getInputStream()
. Why is that?
Here's the code I was trying
// URL must use the http protocol!
HttpURLConnection conn = ( HttpURLConnection )
url.openConnection();
conn.setRequestMethod( "POST" );
conn.setAllowUserInteraction( false ); // you may not ask the user
conn.setDoOutput( true ); // we want to send things
// the Content-type should be default, but we set it anyway
conn.setRequestProperty( "Content-type",
"application/x-www-form-urlencoded" );
// the content-length should not be necessary, but we're cautious
conn.setRequestProperty( "Content-length", Integer.toString(
body.length() ) );
// get the output stream to POST our form data
OutputStream rawOutStream = conn.getOutputStream();
PrintWriter pw = new PrintWriter( rawOutStream );
pw.print( body ); // here we "send" our body!
pw.flush();
pw.close();
// get the input stream for reading the reply
// IMPORTANT! Your body will not get transmitted if you get the
// InputStream before completely writing out your output first!
InputStream rawInStream = conn.getInputStream();
And I got no activity on the server (no System.err.println() ) in my servlet
until I rad the getInputStream(). I thought I would get the servlet running
as soon as I ran the flush or even the close. Why wouldn't it do anything?
happened. I didn't get any activity on the server until I ran the line of
code that opened the input stream (InputStream rawInStream =
conn.getInputStream()
Here's the code I was trying
// URL must use the http protocol!
HttpURLConnection conn = ( HttpURLConnection )
url.openConnection();
conn.setRequestMethod( "POST" );
conn.setAllowUserInteraction( false ); // you may not ask the user
conn.setDoOutput( true ); // we want to send things
// the Content-type should be default, but we set it anyway
conn.setRequestProperty( "Content-type",
"application/x-www-form-urlencoded" );
// the content-length should not be necessary, but we're cautious
conn.setRequestProperty( "Content-length", Integer.toString(
body.length() ) );
// get the output stream to POST our form data
OutputStream rawOutStream = conn.getOutputStream();
PrintWriter pw = new PrintWriter( rawOutStream );
pw.print( body ); // here we "send" our body!
pw.flush();
pw.close();
// get the input stream for reading the reply
// IMPORTANT! Your body will not get transmitted if you get the
// InputStream before completely writing out your output first!
InputStream rawInStream = conn.getInputStream();
And I got no activity on the server (no System.err.println() ) in my servlet
until I rad the getInputStream(). I thought I would get the servlet running
as soon as I ran the flush or even the close. Why wouldn't it do anything?