- my webspace

- my webspace

Latest Comment

Why I must believe in GOD
Testing time is almost passed..there are many colours in lif...
27/06/12 01:43 More...
By Tarun Shekhawat

Allama Iqbal - Selective verse...
Really good work about this website was done. Keep trying mo...
07/03/10 15:04 More...
By Roderick

Allama Iqbal - Selective verse...
Great Job
You have dont a great job of collecting these... Even I had ...
25/08/09 01:01 More...
By Sikandar

O ye who don't believe !
It's like Lehman Brothers :grin
11/10/08 10:31 More...
By anurag Chaturvedi

I Protest
Thanks Sikku for the feedback. I never intend to blame, a...
29/07/08 11:06 More...
By Aminur Rashid


Lost Password?
Home arrow Java arrow How to detect proxy while connecting a url
How to detect proxy while connecting a url PDF Print E-mail
User Rating: / 0
Written by Aminur Rashid   
Wednesday, 01 April 2009

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
		} catch (Exception E) {
		try {
			while ((inputLine = in.readLine()) != null) {
		} catch (Exception E) {

//will provide the password for the proxy.
class MyAuthenticator extends Authenticator {

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication("userName", "Password".toCharArray());

StumbleUponDigg This!Bookmark on Delicious

Add as favourites (481) | Quote this article on your site | Views: 1810 | E-mail

Be first to comment this article
RSS comments

Only registered users can write comments.
Please login or register.

< Prev   Next >
Aminur Rashid