Java:带有HttpURLConnection的HTTP PUT [英] Java: HTTP PUT with HttpURLConnection

查看:458
本文介绍了Java:带有HttpURLConnection的HTTP PUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何做HTTP PUT?我正在使用的类似乎认为它正在执行PUT但是端点正在将其视为我做了GET。我做错了吗?

How do you do do an HTTP PUT? The class I'm using seems to think it is doing a PUT but the endpoint is treating it as if I did a GET. Am I doing anything wrong?

URL url = new URL("https://...");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("PUT");

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

writer.write(xmlString);
writer.close();

System.out.println(conn.getRequestMethod());
String response = readInputStream(conn.getInputStream());
System.out.println(response);

哪个是打印:

PUT
<same content as doing a GET>

如果这个库可以工作,我宁愿不包含另一个库......

I'd rather not include another library if this one could work...

推荐答案

有一种简单的方法可以找到:运行 Wireshark 并查看网络上发生的实际的内容。我发现这是诊断此类问题的最可靠方法 - 您的客户端可能有错误,库可能有错误,服务器可能有错误,但Wireshark会告诉您实际发生了什么。

There's one easy way to find out: run Wireshark and see what's actually happening on the network. I've found that to be the most reliable way of diagnosing this sort of issue - your client could have bugs, the library could have bugs, the server could have bugs, but Wireshark will show you what's really happening.

编辑:好的,对于HTTPS来说,它有点棘手。如果你在Windows上运行,你可以使用 Fiddler ,这是一个代理 - 如果你能说服它可以应付HTTPS你的客户代码接受它的证书,但这更具侵入性......在代理方式中明确改变了流量的样子。

Okay, for HTTPS it's a little trickier. You can use Fiddler if you're running on Windows, which is a proxy - it can cope with HTTPS if you can persuade your client code to accept its certificate, but that's a little more intrusive... putting a proxy in the way clearly changes what the traffic looks like.

这会更好如果你可以通过HTTP与服务器的调试版本交谈。在您的情况下,这是可行的,还是服务器完全在您无法控制的范围内?

It would be better if you could talk to a debug version of the server over HTTP instead. Is that feasible in your case, or is the server completely outside your control?

这篇关于Java:带有HttpURLConnection的HTTP PUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆