gpt4 book ai didi

java - HttpURLConnection 响应代码 405 方法不允许错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:21:05 26 4
gpt4 key购买 nike

我们正在尝试从外部提供商发送集成 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)
}

}

最佳答案

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

关于java - HttpURLConnection 响应代码 405 方法不允许错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51167125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com