I am stuck使用 HttpURLConnection 向 Web 服务器发出 PUT http 请求。我有一些代码可以很好地发出 PUT 请求,并且我可以在标头中简单地包含“期望 100-继续请求属性”,但是尽我所能尝试,我似乎无法让该函数等待“100 继续” ' 在发送实际的 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());
对我来说,他们为什么这样设计并不明显,但实现 Expect:100 逻辑的代码仅在您调用了以下之一时才会使用setFixedLengthStreamingMode(int contentlen) or 的过载long or setChunkedStreamingMode(int chunklen)在做之前getOutputStream
。在这种情况下,我推荐第一个,因为最简单。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)