2

I am accessing a web service by an HTTP POST method as below:

But the response contains nothing. I was expecting a JSON string here. Are JSON read in a different way?

  • I think the best answer you can find it [here](http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests) – Ioan Aug 30 '13 at 08:18

4 Answers4

3

You can keep the type as HttpURLConnection instead of URLConnection. It allows to specify HTTP method.

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
Shamim Ahmmed
  • 7,857
  • 6
  • 22
  • 36
0

I agree that using a HttpUtlConnection or HTTPClient is going to make you life easier, but have a look at setDoOutput

http://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#setDoOutput(boolean)

and

What exactly does URLConnection.setDoOutput() affect?

Community
  • 1
  • 1
Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
0

The problem is here

URLConnection connection = (HttpURLConnection)url.openConnection();

make connection an instance of HttpURLConnection

sr01853
  • 5,377
  • 1
  • 15
  • 36
0

Adding to Shammiz answer

Without cast Make use of URLConnection.html#setDoOutput(boolean)

A URL connection can be used for input and/or output. Set the DoOutput flag to true if you intend to use the URL connection for output, false if not. The default is false.

Passing true, triggers post.

Suresh Atta
  • 114,879
  • 36
  • 179
  • 284