Java 使用 HttpURLConnection 在请求正文中发送数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44305351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send data in Request body using HttpURLConnection
提问by utkarsh31
I am using HttpURLConnection
to make a POST request to a local service deployed in my local created using JAVA Spark.I want to send some data in request body when I make the POST call using the HttpURLConnection but every time the request body in JAVA Spark is null. Below is the code I am using for this
我正在HttpURLConnection
向使用 JAVA Spark 创建的本地部署的本地服务发出 POST 请求。当我使用 HttpURLConnection 进行 POST 调用时,我想在请求正文中发送一些数据,但每次 JAVA Spark 中的请求正文为 null。下面是我为此使用的代码
Java Spark POST Service Handler
Java Spark POST 服务处理程序
post("/", (req, res) -> {
System.out.println("Request Body: " + req.body());
return "Hello!!!!";
});
post("/", (req, res) -> {
System.out.println("Request Body: " + req.body());
return "Hello!!!!";
});
HTTPClass Making the post call
HTTPClass 进行 post 调用
`public class HTTPClassExample{
public static void main(String[] args) {
try{
URL url = new URL("http://localhost:4567/");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
httpCon.connect();
OutputStream os = httpCon.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write("Just Some Text");
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());
osw.flush();
osw.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}`
采纳答案by Ricardo
You should call httpCon.connect();
only after you write your parameters in the body and not before. Your code should look like this:
您应该httpCon.connect();
只在将参数写入主体之后而不是之前调用。您的代码应如下所示:
URL url = new URL("http://localhost:4567/");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
OutputStream os = httpCon.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write("Just Some Text");
osw.flush();
osw.close();
os.close(); //don't forget to close the OutputStream
httpCon.connect();
//read the inputstream and print it
String result;
BufferedInputStream bis = new BufferedInputStream(httpCon.getInputStream());
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result2 = bis.read();
while(result2 != -1) {
buf.write((byte) result2);
result2 = bis.read();
}
result = buf.toString();
System.out.println(result);
回答by Khachornchit Songsaen
I posted with the requested data in XML format and the code look like this. You should add the request property Accept and Content-Type also.
我以 XML 格式发布了请求的数据,代码如下所示。您还应该添加请求属性 Accept 和 Content-Type。
URL url = new URL("....");
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Accept", "application/xml");
httpConnection.setRequestProperty("Content-Type", "application/xml");
httpConnection.setDoOutput(true);
OutputStream outStream = httpConnection.getOutputStream();
OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8");
outStreamWriter.write(requestedXml);
outStreamWriter.flush();
outStreamWriter.close();
outStream.close();
System.out.println(httpConnection.getResponseCode());
System.out.println(httpConnection.getResponseMessage());
InputStream xml = httpConnection.getInputStream();