2013-10-07 66 views
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); 
} 

什么是在JAVA下面的代码等效?Java相当于C#流写()(HttpWebRequest - > HttpURLConnection)

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

我使用:

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

这相当于.NET的HttpWebRequest的JAVA的。

+0

请描述你想实现什么,而不是你会做的另一语言。 – dtb

+1

这有帮助吗? http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java – Pete

+0

@Pete是的帮助,我想这是一样的使用:DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream( )); \t \t \t \t dos.writeBytes(RequestBody); \t \t \t \t dos.flush(); \t \t \t \t dos.close();如果您可以将其作为回答发布,则我将其标记为如此。 –

回答

1

这已经在一些SO帖子中解决。实际上,有各种各样的教程在这里:

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

这可能是我的话题看作是它提供了执行和处理Java中的HTTP请求的广泛信息的最好来源。

还有这样一个问题:How to send HTTP request in java?

最后,这一切都基于被记录在这里的HttpURLConnection的类:http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html