首页 > 解决方案 > 使用 OkHTTP 客户端和 HttpsURLConnection 在 Java 中不支持获取请求方法“GET”

问题描述

因为我需要通过 post 发送数据以启动 API,但得到不受支持的错误。即使我正在使用 post 方法,请指出我在哪里做错了。我已经尝试过 OkHTTPClient 和 HTTPSURL 连接,仍然出现同样的错误

Error: **{"timestamp":"2021-03-23T06:26:43.508+0000","status":405,"error":"Method Not Allowed","message":"Request method 'GET' not supported","path":"/stsBankResponse/"}**

当我直接在浏览器中点击 URL 时,也不允许使用方法,它的有效错误但仅以编程方式使用 Post,即使相同的错误

JSONObject jsobj=new JSONObject();
        jsobj.put("authmode", "Abc");
        JSONArray jaarray = new JSONArray();
        jaarray.put(jsobj);
        JSONObject mainObj = new JSONObject();
        mainObj.put("bankResponseDtl", jaarray);
        String str_jsonparams = String.valueOf(mainObj);
        System.out.println("PU_Auth str_jsonparams->"+str_jsonparams);
        String proxyhost=common_utility.getProxyHost();
        String proxyport=common_utility.getProxyPort();
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyhost, new Integer(proxyport)));
        
                OkHttpClient client = new OkHttpClient();
                OkHttpClient.Builder builder = new OkHttpClient.Builder();
                builder.connectTimeout(30, TimeUnit.SECONDS); 
                builder.readTimeout(30, TimeUnit.SECONDS); 
                builder.writeTimeout(30, TimeUnit.SECONDS); 
                builder.proxy(proxy);
                client = builder.build();
        RequestBody body = RequestBody.create(JSON, str_jsonparams);
        Request request = new Request.Builder()
     
                .url(common_utility.getEmandateServerResponeURL())
                .post(body)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                call.cancel();
                System.out.println("ECEEEEE"+e);
            }
@Override
            public void onResponse(Call arg0, Response arg1) throws IOException {
            }
        });

标签: javahttpsokhttphttp-status-code-405

解决方案


推荐阅读