![](/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.