Read a Web Page by Connecting to a Final Server Through a Proxy Server

This code uses the new J2SE 5.0 classes java.net.Proxy and java.net.Proxy.Type to connect to a final server through a proxy server in order to view a Web page. The Proxy class represents a proxy setting (proxy protocol and proxy address) and the Proxy.Type class represents the proxy type. This type can be:

DIRECT - no proxy;HTTP - high level protocol such FTP,HTTPSOCKS - SOCKS (V4 or V5)

In this example, the proxy type is HTTP and the proxy address is localhost, but it can be any other proxy. For opening a connection to that proxy, the code uses the openConnection(Proxy proxy) method of the URL class.

Before opening the connection, you must supply the URL that you want to access by replacing “the final server URL.” The coonection is then made through the proxy server. The next step is to call the getInputStream method to read the Web page specified by the URL. After that, use BufferedReader to display the page content on the screen:

import java.net.*; import java.io.*; import java.util.*; class read{ public read(){} BufferedReader buffer=null;      String linie=null; URLConnection URLcon=null; URL url=null; InputStream IS=null; public void getURLContent()             {            try {              InetAddress addr=InetAddress.getLocalHost(); //or use getByName("proxy host");             InetSocketAddress ISA=new InetSocketAddress(addr,port);//where port is the proxy port              java.net.Proxy proxy=new java.net.Proxy(java.net.Proxy.Type.HTTP,ISA); //you may also use SOCKS or DIRECT types                                url=new URL("the final server URL");                               URLcon = url.openConnection(proxy);                                                IS=URLcon.getInputStream();                   buffer=new BufferedReader(new InputStreamReader(IS));                     while((linie=buffer.readLine())!=null)                                        {                                 System.out.println(linie);                               }                 buffer.close();                     }catch(MalformedURLException e)                             {System.out.println("Eroare:"+e.getMessage());                 }catch(IOException e)                       {System.out.println("Eroare:"+e.getMessage());}                           } } public class ReadURLToProxy{         public static void main(String[] args)       {         read t=new read();       t.getURLContent();         } }" 
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job