When I started learning Java (infact I still am) I was stuck in assignment to open the net connection, which was outside our institute firewall. The document at Sun’s website explains in depth the traditional, as well as the latest way (Use of Proxy class/Proxy Selector)to use Proxies to connect to a network. To refresh what I used, here is the code using the traditional method.
package com.aminur.java.core; import java.util.*; import java.net.*; import java.io.*; public class ReadUrl { private static final String proxyHost = "http://yourproxyhost"; //replace with proxy host private static final String proxyPort = "8080"; //Your proxy port public static void main(String args[]) { System.out.println("Connecting using proxy ..."); URLConnection smrConnection = null; BufferedReader in = null; String inputLine; /* * This is where you set the URL. */ System.setProperty("http.proxyHost", proxyHost); System.setProperty("http.proxyPort", proxyPort); Authenticator.setDefault(new MyAuthenticator()); try { URL subjectURL = new URL("http://www.aminurrashid.com"); smrConnection = subjectURL.openConnection(); in = new BufferedReader(new InputStreamReader(smrConnection .getInputStream())); } catch (Exception E) { System.out.println(E); } try { while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } } catch (Exception E) { } } } //will provide the password for the proxy. class MyAuthenticator extends Authenticator { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("userName", "Password".toCharArray()); } } <span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>
Related posts
Feed
- India
- World
- Live
- Cricket News
This site uses cookies. These cookies come mainly from third party library used at this site(Google/Jetpack/etc). By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here Cookie Policy
To find out more, including how to control cookies, see here Cookie Policy