URL openURL = null;
        openURL = new URL("http://www.qq.com");
        URLConnection con = null;
        HttpURLConnection httpcon = null;
         con = openURL.openConnection();
Properties prop = System.getProperties();
            prop.put("proxySet","true");  
            prop.setProperty("http.proxyHost", "20,32,11,23");
            prop.setProperty("http.proxyPort", "8023");
 httpcon = (HttpURLConnection)con;
         httpcon.disconnect();
         httpcon.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        httpcon.setConnectTimeout(3000);
        httpcon.connect();
        StringBuilder sb = new StringBuilder();
        String inputLine;
         BufferedReader in = null;
         try{
         in = new BufferedReader(new InputStreamReader(httpcon.getInputStream(), "gb2312"));
         }catch(java.net.SocketException e){
         throw e;
         }
         while ((inputLine = in.readLine()) != null) {
         sb.append(inputLine).append("\r\n");
         }
         in.close();
httpcon.disconnect();
System.out.println(sb);
注意:在这里的代理IP和端口号都是本人随便写的,是没用的一上IP,但是我运行中确能通过这样连接到网络并获取到网页的数据!
郁闷呀!谁能帮我解释下....为什么会这样?

解决方案 »

  1.   

    Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("","".toCharArray());
    }
    });
      

  2.   

    这里API和源码例子
    一个英文的,一个翻译的:
    http://apicode.gicp.net/class.do?api=selectByfatherIndex&father=255
    http://apicodecn.gicp.net/class.do?api=selectByfatherIndex&father=255
      

  3.   

    你使用的是系统变量,当 这个代理无效时。就会跳过。不适用代理访问!
    InetSocketAddress addr = new InetSocketAddress("218.14.227.197",3128);
    Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
    URL url = new URL("http://www.baidu.com");
    URLConnection con = url.openConnection(proxy);
    这样可以避免 此情况
      

  4.   

    我那个是弹出对话框让你输入用户名和密码的
    在之前设置System.getProperties().setProperty("proxySet", "true");
    System.getProperties().setProperty("http.proxyHost", "");
    System.getProperties().setProperty("http.proxyPort", "");
      

  5.   

    回7楼:你这种方法可使用JAVA自带的代理访问方式,但现在有很多网站会封掉程序的代理方式访问(听说),
    所以没去用,
    回8楼:我使用的都是免费代理,所以不使用用户名和密码,现在问题已经解决:
    唉,已经有这么多高手做出了那么多的好用开源工具了,为什么我总是要自己去写呢!
    最后使用apache中的一个开源包:commons-httpclient开做对网络的代理访问,问题解决...