简体   繁体   中英

Java Equivalent to C# Stream Write() (HttpWebRequest -> HttpURLConnection)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM