![](/img/trans.png)
[英]When sending okhttp request: HTTP ERROR 405 and invalid_client
[英]405 Error when sending HTTP request throw JAVA
我尝试发送http请求并使用Java代码获取结果
这是服务器端:
public class Testws {
@GET
@Path("/test/{id}")
@Produces("application/xml")
public Integer getAssets(@PathParam("id") int id){
}
}
这是客户端:
URL url = new URL("http://xxx/route/test/1");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.connect();
final int responseCode = connection.getResponseCode();
当我运行此Java代码时,我得到了405状态代码,但是当我直接在浏览器中放置链接( http:// xxx / route / test / 1 )时,它工作正常,我得到了200作为响应代码。 我怎样才能解决这个问题 ?
您的错误意味着您使用错误的HTTP
方法查询服务器。
您在浏览器中粘贴的内容将作为GET
方法发送。
您的代码未使用GET
,因此要强制使用HTTP方法,请使用HttpConnection.setRequestMethod
另外, setDoOutput(true)
不会使您使用GET
,请参阅本主题: URLConnection.setDoOutput()究竟会产生什么影响? 。
删除此行,您将使用默认的HTTP方法GET
。
公共无效setRequestMethod(String方法)抛出ProtocolException
设置URL请求的方法,方法之一:GET POST HEAD OPTIONS PUT DELETE TRACE合法,受协议限制。 默认方法是GET 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.