使用Java的HttpUrlConnection PATCH请求 [英] HttpUrlConnection PATCH request using Java
本文介绍了使用Java的HttpUrlConnection PATCH请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试执行http PATCH请求,但我总是收到404错误,因此我的连接设置可能不正确:
I am trying to do a http PATCH request but I always get the 404 error, so maybe the settings of my connection are not correct:
URL url = new URL("MyPath");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("X-HTTP-Method-Override", "PATCH");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestMethod("POST");
JsonObject jo = createMyJson();
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(jo.toString());
out.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
我收到404错误,未找到。当使用Postman执行相同的请求时,这是有效的..
感谢您的帮助。
I get the 404 error, Not found. When doing the same request using Postman, this is working.. Thank you for your help.
推荐答案
并非所有服务器支持 X-HTTP-Method-Override
。在这种情况下,你的最后一招(如果你没有使用一个像样的HTTP客户端)来破解 URLConnection
对象。
Not all servers support X-HTTP-Method-Override
. In that case your last resort is (if you are not using a decent HTTP client) to hack the URLConnection
object.
我发布了一个完整的解决方案在此处,请查看。
I posted a complete solution here on SO, check it out.
这篇关于使用Java的HttpUrlConnection PATCH请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文