- my webspace

- my webspace

Latest Comment

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

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

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

I Protest
Great !!!
:zzz this is a wonderful story and very honest from the hea...
29/07/08 10:33 More...
By Jennifer Gallagher

I Protest
The blog is good, but the contents show that the serial blas...
29/07/08 10:26 More...
By Sikandar

Login






Lost Password?
No account yet? Register

Tell a Friend

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
PoorBest 
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
					.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());
	}
}



Add as favourites (59) | Quote this article on your site | Views: 183

Be first to comment this article
RSS comments

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

Powered by AkoComment Tweaked Special Edition v.1.4.6
AkoComment © Copyright 2004 by Arthur Konze - www.mamboportal.com
All right reserved

 
< Prev   Next >
Aminur Rashid