java实现100continue交易_如何使用HttpURLConnection等待Java中的Expect 100-continue响应

我被困在使用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());

解决方法:

标签:java,http,legacy,httpurlconnection

来源: https://codeday.me/bug/20191013/1904670.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值