HttpURLConnection无效的HTTP方法:PATCH [英] HttpURLConnection Invalid HTTP method: PATCH

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

问题描述

当我尝试使用非标准HTTP方法(如PATCH和URLConnection)时:

When I try to use a non-standard HTTP Method like PATCH with URLConnection:

    HttpURLConnection conn = (HttpURLConnection) new URL("http://example.com").openConnection();
    conn.setRequestMethod("PATCH");

我得到一个例外:

java.net.ProtocolException: Invalid HTTP method: PATCH
at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:440)

使用像Jersey这样的更高级别的API会产生相同的错误。是否有解决方法来发出PATCH HTTP请求?

Using a higher level API like Jersey generates the same error. Is there a workaround to issue a PATCH HTTP request?

推荐答案

是否有解决方法。使用


X-HTTP-Method-Override

X-HTTP-Method-Override

。此标头可用于POST请求以伪造其他HTTP方法。只需将X-HTTP-Method-Override标头的值设置为您想要实际执行的HTTP方法即可。
所以请使用以下代码。

. This header can be used in a POST request to "fake" other HTTP methods. Simply set the value of the X-HTTP-Method-Override header to the HTTP method you would like to actually perform. So use following code.

conn.setRequestProperty("X-HTTP-Method-Override", "PATCH");
conn.setRequestMethod("POST");

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

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