首页 文章

如何使用HttpURLConnection在Java中等待Expect 100-continue响应

提问于
浏览
1

我是 stuck 使用HttpURLConnection向Web服务器发出PUT http请求 . 我有一些代码可以使PUT请求很好,我可以简单地在头文件中包含'Expect 100-continue Request Property'但是尽可能地尝试在发送实际的http有效负载之前从服务器继续'响应' .

我得到以下(来自Wireshark)

PUT /post/ HTTP/1.1
User-Agent: curl/7.35.0
Accept: */*
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
Host: somerandomdomain.info
Connection: keep-alive
Content-Length: 17

Some data for you
HTTP/1.1 100 Continue

...rest of web-server response...

我确定我遗漏了一些显而易见的东西然而在谷歌搜索后我画了一个空白 - 有人可以帮忙吗?

非常感谢如果:)

下面的Http PUT代码片段:

String url =   "http://somerandomdomain.info";
String postJsonData = "Some data for you\n";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

// Setting basic post request  
con.setRequestMethod("PUT");
con.setRequestProperty("User-Agent", "jcurl/7.35.0");
con.setRequestProperty("Accept", "*/*");
con.setRequestProperty("Content-Length", postData.length() + "");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Expect", "100-continue");

// Send post request  
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postData);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post Data : " + postData);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String output;
StringBuffer response = new StringBuffer();

while ((output = in.readLine()) != null) {
  response.append(output);
}

in.close();

//printing result from response  
System.out.println(response.toString());

1 回答

相关问题