Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HTTPURLConnection on HP Unix  RSS feed

 
suchitralingaraj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am passing an XML String through HTTPURLConnection to the server which is an HP Unix machine. First time request and response goes through. For the second time when i access the server outputstream is posing a problem. A null pointer exception is thrown and the passed XML string length is zero.
Immediate response will be greatly appreciated.
Here is the code snippet.
try
{
URL url = new URL(url_resource);
con = (HttpURLConnection) url.openConnection();
con.setUseCaches( false );
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty( "Content-Length", String.valueOf( xml_string.length() ) );
con.setRequestProperty( "Content-Type", "multi-part/form-data");
}
catch(Exception exc)
{
String s = exc.getMessage();
}
try
{
// Write out the XML Contents
PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream() ));
out.println(xml_buff.toString());
out.close();
}
catch(Exception exc1)
{
String s = exc1.getMessage();
}
After this When I try to read the message from the input stream NullPointerException is thrown and the message is as "Did not meet stated content length of OutputStream: you wrote 0 bytes and I was expecting you to write exactly 252 bytes!!!"
Regards
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
We don't have many rules around these parts, but we do have our naming policy, which requires that you use both a first and last name. Please head over here and fix yours, pronto! Thanks, pardner, and see you around the Ranch!
 
Han You
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't figure out what's wrong with your code, but one thing I can suggest is to use a BufferedWriter instead of a PrintWriter. The reason for this is the PrintWriter class doesn't throw any exceptions. What this means is that all exceptions that occur while using the PrintWriter are never revealed to the surrounding java code.
~P.S. if you DO decide to use a BufferedWriter, don't forget to .flush()!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!