简体   繁体   English

Java:带有HttpURLConnection的HTTP PUT

[英]Java: HTTP PUT with HttpURLConnection

How do you do do an HTTP PUT? 你是怎么做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. 我正在使用的类似乎认为它正在执行PUT但是端点正在将其视为我做了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);

Which is printing: 哪个是打印:

PUT
<same content as doing a GET>

I'd rather not include another library if this one could work... 如果这个可以工作,我宁愿不包括另一个库...

There's one easy way to find out: run Wireshark and see what's actually happening on the network. 有一个简单的方法可以找到:运行Wireshark并查看网络上实际发生的情况。 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. 我发现这是诊断此类问题的最可靠方法 - 您的客户端可能有错误,库可能有错误,服务器可能有错误,但Wireshark会告诉您实际发生了什么。

EDIT: Okay, for HTTPS it's a little trickier. 编辑:好的,对于HTTPS来说,它有点棘手。 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. 你可以使用Fiddler,如果你在Windows上运行,这是一个代理 - 如果你可以说服你的客户端代码接受它的证书,它可以应付HTTPS,但这更具侵入性......明确地放置代理改变流量的样子。

It would be better if you could talk to a debug version of the server over HTTP instead. 如果你可以通过HTTP与服务器的调试版本交谈会更好。 Is that feasible in your case, or is the server completely outside your control? 在您的情况下这是可行的,还是服务器完全在您无法控制的范围内?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM