简体   繁体   English

等效于C#Stream Write()的Java(HttpWebRequest-> HttpURLConnection)

[英]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? 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. JAVA等效于.NET的HttpWebRequest。

This has already been addressed in a few SO posts. 一些SO帖子已经解决了这一问题。 There's actually a tutorial of sorts here: 实际上这里有一个教程:

Using java.net.URLConnection to fire and handle HTTP requests 使用java.net.URLConnection触发和处理HTTP请求

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. 这可能是我在该主题上看到的唯一的最佳资源,因为它提供了有关在Java中执行和处理HTTP请求的大量详细信息。

There's also this question: How to send HTTP request in java? 还有一个问题: 如何在Java中发送HTTP请求?

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 最后,它们全部基于HttpUrlConnection类,该类在此处记录: http ://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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