如何使用 HttpURLConnection 在请求正文中发送数据?

Posted

技术标签:

【中文标题】如何使用 HttpURLConnection 在请求正文中发送数据?【英文标题】:How do you send data in a Request body using HttpURLConnection? 【发布时间】:2017-11-02 11:21:38 【问题描述】:

我正在使用HttpURLConnection 向本地部署并使用 JAVA Spark 创建的本地服务发出 POST 请求。 当我使用HttpURLConnection 进行 POST 调用时,我想在请求正文中发送一些数据,但每次 JAVA Spark 中的请求正文为空。以下是我为此使用的代码 p>

Java Spark POST 服务处理程序

post("/", (req, res) -> 
    System.out.println("Request Body: " + req.body());
    return "Hello!!!!";
);

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();
        
    

【问题讨论】:

【参考方案1】:

您应该只在将参数写入正文之后而不是之前调用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);

【讨论】:

看来这不是真的。打电话给getOutputStream 打电话给connect。如果要显式连接,请在getOutputStream 之前调用connect。要测试自己,请禁用您的互联网,当连接失败时,您会看到名为 connectgetOutputStream 失败。【参考方案2】:

我以 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();

【讨论】:

以上是关于如何使用 HttpURLConnection 在请求正文中发送数据?的主要内容,如果未能解决你的问题,请参考以下文章

使用 HTTPUrlConnection 时如何保留 cookie?

如何使用 HttpURLConnection 发布数据

如何从 HttpURLConnection 实例获取 HTTP 请求字符串

win10系统开机停在请稍候解决教程系统天地

win10系统开机停在请稍候解决教程系统天地

如何修改 HttpUrlConnection 的标头