Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java.net.ConnectException: Connection refused: connect  RSS feed

 
Ashwini Kale
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am trying to open a web page using HttpURLConnection. I was getting ConnectExecption. I thought may be this is because I am behind a proxy.

I tried using a couple of approaches.. some of them from a thread on javaranch itself.

This is the code that i tried using... i provided proper values to
systemSettings.put("http.proxyHost","myproxy.com") ;
systemSettings.put("http.proxyPort", "80") ;

Still m left with the refused connection.

Please help!! Thanks

import java.util.*;
import java.io.*;
import java.net.*;

public class crawler {
public static void main(String args[]) {

try {

//First set the Proxy settings on the System
Properties systemSettings = System.getProperties();
systemSettings.put("http.proxyHost","myproxy.com") ;
systemSettings.put("http.proxyPort", "80") ;

URL yahoo = new URL("http://www.yahoo.com");
HttpURLConnection con = (HttpURLConnection) yahoo.openConnection();

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));

String inputLine;

while ( (inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
catch (MalformedURLException me) {
System.out.println("MalformedURLException: " + me);
me.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ashwini,

if you're behind a proxy you should know and use the correct hostname and port for it. Do you really think myproxy.com is correct?

Another problem could be that it's not a proxy but a firewall which is simply blocking outgoing connections on TCP port 80 (HTTP). Are you trying this at home or in a company or what?

Anyway in principle your application is working!

Marco
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!