0

I want to make GET and POST requests from a servlet to another server, where I will be sending XML content and a Content-type header.

However, there is no method in HTTPRequest to set Content-type and content.

How can I do that?

Hawken MacKay Rives
  • 1,171
  • 1
  • 16
  • 26
Arvind
  • 1,207
  • 6
  • 27
  • 55

2 Answers2

1

Using HTTPUrlConnection this can be approached as follows:

connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "text/xml");

I personally prefer the to use the Apache Http Client. The Apache HTTP client "post xml" example demonstrates you how to post an xml and correctly set's the posted entities mimetype and encoding.

Apache HTTP Client Post XML Example

Thomas
  • 1,302
  • 9
  • 16
0

I think you are messing up things. The servlet api is used on the server side to accept requests, prepare an answer and send it. In most cases, it is done using the HTTP protocol. The servlet api doesn't have constructs for sending requests because it's designed for the opposite. Anyways you can always use jetty client or whatever in the processXXX methods. (but that's quite ugly :)

gyorgyabraham
  • 2,550
  • 1
  • 28
  • 46