我们正尝试从外部提供商发送请求集成Web API。他们正在使用IP授权,并且我们的IP是在其集成系统上定义的。
创建HttpUrlConnection时出现405错误,我们无法发送请求此URL。当我们尝试创建具有403错误的主域“ http://api.relateddigital.com”的HttpUrlConnection时。
提供商公司说:“我们对您的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)
}
}
答案 0 :(得分:0)
感谢@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;
}