Saturday, April 17, 2010

在java中如何使用代理访问某个页面

    private static void testProxy() {
        try {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                    "221.130.13.38", 80));

            URL url = new URL("http://flybull.yfyf.net/addispatcher.htm");

            HttpURLConnection uc = (HttpURLConnection) url
                    .openConnection(proxy);
            uc.connect();

            StringBuffer tmp = new StringBuffer();
            BufferedReader in = new BufferedReader(new InputStreamReader(uc
                    .getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {

                System.out.println(line);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

No comments: