【问题标题】:HttpURLConnection responseCode 405 method not allowed errorHttpURLConnection responseCode 405 方法不允许错误
【发布时间】:2018-12-12 12:12:25
【问题描述】:

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

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

提供商公司说“我们对您的 IP 地址没有任何限制。错误与您的网络有关。” 我们该如何解决?

我们的准则:

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)
    }

}

【问题讨论】:

  • 您正在调用登录 API,但您在获取响应代码后设置了 POST 方法,并且您根本没有指定您的凭据,在我们开始深入了解您之前,很多事情都是错误的。跨度>
  • 我不确定这是否对您有所帮助。我在 Android 中执行了类似的代码,但在所有其他站点上都失败了,并出现了相同的错误。他们可能没有针对您的 IP 的任何内容,但他们可能需要提供您正在使用的浏览器类型(或其他内容)。简而言之,我不知道它做了什么,但通过添加 connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; U; Android 7.1;) AppleWebKit/537.36 (KHTML, like壁虎)Chrome/66.0 Safari/537.36");就我而言。
  • 嗨尤金,当我尝试使用从whoishostingthis.com/tools/user-agent 获得的值的 setRequestProperty 时,我收到此错误:在 sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(未知来源)。解决不了。
  • 嗨 Chor,许多网站都使用 HttpUrlConnection,就像我的例子一样。 journaldev.com/7148/…mkyong.com/java/how-to-send-http-request-getpost-in-java连接成功后设置POST
  • hmm 一行行看他的代码,con.getResponseCode() 是在设置POST之后..

标签: java httpurlconnection ioexception http-status-code-405


【解决方案1】:

感谢@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;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-20
    • 2017-11-13
    • 2016-12-02
    • 2012-10-25
    • 2012-03-21
    • 2015-11-25
    • 1970-01-01
    • 2014-10-04
    相关资源
    最近更新 更多