• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

HTTPURLConnection on HP Unix

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()!
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic