如何使用HttpURLConnection等待Java中的Expect 100-continue响应
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用HttpURLConnection等待Java中的Expect 100-continue响应,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2116字,纯文字阅读大概需要4分钟。
内容图文
![如何使用HttpURLConnection等待Java中的Expect 100-continue响应](/upload/InfoBanner/zyjiaocheng/691/a7e8baa1c4004df4a28cf5af7d78eaf5.jpg)
我被困在使用HttpURLConnection向Web服务器发出PUT http请求.我有一些代码可以很好地执行PUT请求,并且可以在标头中包含“ Expect 100-Continue Request属性”,但是请尝试一下,因为我似乎无法让函数等待’100 Continue在发送实际的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());
解决方法:
对于我来说,为什么不这样设计它并不明显,但是仅当您在执行getOutputStream之前调用setFixedLengthStreamingMode(int contentlen)或the overload for long或setChunkedStreamingMode(int chunklen)之一时,才使用实现Expect:100逻辑的代码.在这种情况下,我建议最简单的第一个.
内容总结
以上是互联网集市为您收集整理的如何使用HttpURLConnection等待Java中的Expect 100-continue响应全部内容,希望文章能够帮你解决如何使用HttpURLConnection等待Java中的Expect 100-continue响应所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。