1
public static Response callService(String strURL, String RequestBody, String Token, int timeout, Boolean isPostMethod) {

    var httpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);

    httpWebRequest.GetRequestStream().Write(UTF8Encoding.UTF8.GetBytes(RequestBody), 0, RequestBody.Length);
}

What is the equivalent of the following code in JAVA?

httpWebRequest.GetRequestStream().Write(UTF8Encoding.UTF8.GetBytes(RequestBody), 0, RequestBody.Length);

I'm using:

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

which is the JAVA equivalent to .NET's HttpWebRequest.

Eric Bergman
  • 1,291
  • 10
  • 39
  • 78
  • Please describe what you're trying to achieve, not what you would do in another language. – dtb Oct 07 '13 at 17:18
  • 1
    Does this help? http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java – Pete Oct 07 '13 at 17:20
  • @Pete Yes that helped, I guess it's the same as using: DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream()); dos.writeBytes(RequestBody); dos.flush(); dos.close(); If you can post it as an answer so I mark it as so. – Eric Bergman Oct 07 '13 at 17:24

1 Answers1

1

This has already been addressed in a few SO posts. There's actually a tutorial of sorts here:

Using java.net.URLConnection to fire and handle HTTP requests

That's probably the single best source I've seen on the topic as it provides extensive details on performing and handling an HTTP Request in Java.

There's also this question: How to send HTTP request in java?

And finally, it's all based on the HttpUrlConnection class which is documented here: http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html

Community
  • 1
  • 1
Pete
  • 6,325
  • 4
  • 36
  • 67