File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Post to an URL using HttpURLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Post to an URL using HttpURLConnection" Watch "Post to an URL using HttpURLConnection" New topic
Author

Post to an URL using HttpURLConnection

Nagavarapu Shashikant
Greenhorn

Joined: Aug 07, 2008
Posts: 5
Hi Ranchers,

I want to post some xml content and add an attachment (may be multiple files - docs etc) to an URL. This is to test a webservice call. I am using HttpURLConnection to achieve this and wirting the file contents to the output stream. This does not work as I am getting a 500 error. Please suggest me how do I post the files as attachment as well as the xml content(mandatory post content) to the stream(Output Stream).

Regards
Shashikant
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

Use Apache Commons HTTP Client instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
A 500 error is "Internal Server Error" so your code may be working but the webservice isn't.

Is this a SOAP web service you are calling? Where are you getting information on what the service wants to see?

Bill
Nagavarapu Shashikant
Greenhorn

Joined: Aug 07, 2008
Posts: 5
Hi Bill,

This is not a SOAP call. The webservice is up and i want to lookup the service using java code.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42367
    
  64
What does "lookup the service" mean? Are you trying to access to service, or its WSDL, or something else?

Also, when you're posting to the service, are you certain that the XML you're sending is exactly what the service is expecting?

And finally, as Bill said a 500 signifies a server error. If available, check the server logs for any error massages.


Ping & DNS - my free Android networking tools app
Nagavarapu Shashikant
Greenhorn

Joined: Aug 07, 2008
Posts: 5
Hi,

I have checked the service spec and it asks for a specific format of xml. I have prepared the xml file and reading data from it. The service just returns a service code 200 if all is fine..
Nagavarapu Shashikant
Greenhorn

Joined: Aug 07, 2008
Posts: 5
The other problem i am facing is how to send attachments using the stream. I have multi pdf documents which i need to send.. please update me with any ideas.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

Originally posted by Rob Prime:
Use Apache Commons HTTP Client instead.

Have you tried that one already? It's PostMethod can take call the setRequestEntity method with an instance of MultipartRequestEntity. This in turn can be created using a Part[], and guess what - there are subclasses of Part called FilePart and StringPart.
[ October 06, 2008: Message edited by: Rob Prime ]
Nagavarapu Shashikant
Greenhorn

Joined: Aug 07, 2008
Posts: 5
Thanks,

I will try this.. I was trying the attachments posting using Java mail API..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

That doesn't make sense to me. Email is totally unrelated to HTTP(S).

Also, could you please check our naming policy?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Post to an URL using HttpURLConnection