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?

user2699073
  • 71
  • 1
  • 5
  • 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
  • 8,265
  • 6
  • 25
  • 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
  • 44,617
  • 6
  • 35
  • 64
0

The problem is here

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

make connection an instance of HttpURLConnection

sr01853
  • 6,043
  • 1
  • 19
  • 39
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
  • 120,458
  • 37
  • 198
  • 307