HttpURLConnection responseCode 405 方法不允许错误

HttpURLConnection responseCode 405 method not allowed error

我们正在尝试从外部提供商发送集成网络请求 api。他们正在使用IP授权,我们的IP是在他们的集成系统上定义的。

我们在创建 HttpUrlConnection 时遇到 405 错误,我们无法发送此请求 URL。当我们尝试使用主域“http://api.relateddigital.com”创建 HttpUrlConnection 时出现 403 错误。

供应商公司说 "We haven't any constraint for your IP addresses. The error is associated with your network." 我们该如何解决?

我们的代码:

public class Main {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        final URL url=new URL("http://api.relateddigital.com/resta/api/auth/login");
        final HttpURLConnection connection=(HttpURLConnection)url.openConnection();
        System.out.println("connection.getResponseCode() :: "  + connection.getResponseCode());
        //the output is 405
        connection.setRequestMethod("POST");
        //Exception in thread "main" java.lang.IllegalStateException: connect in progress at sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(Unknown Source)
    }

}

谢谢@Chor Wai Chun 解决了

我忽略了 getResponseCode() 必须在连接参数之后。

谢谢大家!

它是这样工作的:

final URL url = new URL("http://api.relateddigital.com/resta/api/Datawarehouse/InsertUpdateRowInDwTable");
        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
...
...
...
if ((connection.getResponseCode() < 200) || (connection.getResponseCode() >= 300))
        {
            final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
            final StringBuilder builder = new StringBuilder();
            String inputLine;
            while ((inputLine = in.readLine()) != null)
            {
                builder.append(inputLine);
            }

            System.out.println("Error builder::" + builder);
            in.close();
            return;
        }