Reputation: 5029
I am writing a method to send a GET request to an endpoint and parse the response text as json.
public static KafkaConnection getKafkaConnection(String sUrl) throws IOException {
URL url=new URL(sUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
InputStream is = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
String jsonText = builder.toString();
JSONObject json = new JSONObject(jsonText);
KafkaConnection kc = new KafkaConnection();
JSONArray array = json.getJSONArray("address");
List<String> list = new ArrayList<String>();
for (int i = 0; i < array.length(); i++) {
list.add(array.getString(i));
}
kc.setAddresses(list);
kc.setZookeeper(json.getString("zookeeper"));
return kc;
}
However I am getting "Connection refused" error at line InputStream is = con.getInputStream();
. for any url. Even http://www.google.com
. These urls are accessible from a browser or using curl
command on the same machine. What is causing the connection failure using HttpUrlConnection?
Below is the stack trace:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1546)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at com.lgc.dist.plat.test.kafka.KafkaTestUtils.getKafkaConnection(KafkaTestUtils.java:30)
at com.lgc.dist.plat.test.kafka.KafkaTestUtils.main(KafkaTestUtils.java:54)
EDIT Based on the answers, looks like the consensus is that this is a firewall issue.
I did set the environment variables http_proxy
and https_proxy
to the following for my computer
https://user:[email protected]:80
So I added the following code before the connection:
System.setProperty("http.proxyHost", "https://np1prxy801.corp.company.com");
System.setProperty("http.proxyPort", "80");
String user = "user";
String password = "pass";
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
user, password.toCharArray());
}
}
);
However, I am still getting connection refused
error.
Upvotes: 0
Views: 1074
Reputation: 5029
Finally got it to work with the following code based on two other posts, Eclipse Outbound connection blocked : The same url works from web browser and How do I make HttpURLConnection use a proxy?
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress("np1prxy801.corp.abc.com", 80));
URL url=new URL(sUrl);
HttpURLConnection con = (HttpURLConnection)url.openConnection(proxy);
String user = "user";
String password = "password";
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
user, password.toCharArray());
}
}
);
con.setRequestMethod("GET");
InputStream is = con.getInputStream();
Upvotes: 0
Reputation: 28588
Before trying to connect, try
System.setProperty("http.proxyHost", "your.proxy.server.com"); System.setProperty("http.proxyPort", "{your proxy port}");
as an experiment
Upvotes: 1