We are trying to send request an integration web api from an external provider. They are using IP authorization and our IP is defined on their integration system.
We are having 405 error when create HttpUrlConnection and we can't send request this URL. When we try create HttpUrlConnection with main domain "http://api.relateddigital.com" having 403 error.
The provider firm is saying "We haven't any constraint for your IP addresses. The error is associated with your network." How can we solve it?
Our Code:
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)
}
}
I have solved it thanks to @Chor Wai Chun.
I had overlook that getResponseCode() must be after the connection params.
Thanks everyone!
It worked with like this:
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;
}