我想在我的java类里,访问一个外网(譬如http://www.baidu.com),尝试了各种办法,总是出现java.net.ConnectException: Connection timed out: connect异常,或者是
java.net.SocketTimeoutException: connect timed out异常。
本机是可以连外网的,是不是java工程需要什么访问外网的权限设置。
本人乃初级菜鸟,希望各路大侠指点指点!!java 网址 访问 java访问外网

解决方案 »

  1.   

    public static String getURLContent(String urlStr){
    /** 网络的url地址 */
    URL url = null;
    /** http连接 */
    // HttpURLConnection httpConn = null;
    /**//** 输入流 */
    BufferedReader in = null;
    StringBuffer sb = new StringBuffer();
    try{
    url = new URL(urlStr);
    in = new BufferedReader( new InputStreamReader(url.openStream(),"UTF-8") );//这一行出的错,是通过的代理。。
    String str = null;
    while((str = in.readLine()) != null) {
    sb.append( str );
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    } finally {
    try{
    if(in!=null) {
    in.close();
    }
    }catch(IOException ex) {
    ex.printStackTrace();
    }
    } String result =sb.toString();
    System.out.println(result);
    return result; }}
      

  2.   

    程序中添加代理:
    在类的开头用个static块吧:// proxy configuration
    static {
    System.getProperties().setProperty("proxySet", "true");
    System.getProperties().setProperty("http.proxyHost", "www-proxy.xxxx.se");
    System.getProperties().setProperty("http.proxyPort", "8080");
    }
      

  3.   

    果然是高手云集的地方啊!
    我加了 
     static {
     System.getProperties().setProperty("proxySet", "true");
     System.getProperties().setProperty("http.proxyHost", "代理IP");
     System.getProperties().setProperty("http.proxyPort", "8080");
     }
    就好用了,谢谢!!