HTTP Proxy authentication

EXAMPLE -1

HttpURLConnection connection = null;

// Create connection
url = new URL(targetURL);

if (ConfigProvider.getProxy() != null) {
connection = (HttpURLConnection) url
.openConnection(return new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(“ip_address”, port_no)););
} else {
connection = (HttpURLConnection) url.openConnection();
}

connection.setRequestMethod(“GET”);
if (authorization != null) {
connection.setRequestProperty(“Authorization”, “Basic ”
+ authorization);
}
if(headers != null) {
for(HTTPHeaderParam headerParam: headers.getHeaderParams()) {
connection.addRequestProperty(headerParam.getKey(), headerParam.getValue());
}
}

System.out.println(“get response code”);
int responseCode = connection.getResponseCode();
response.setResponseCode(responseCode);

System.out.println(“response code : “+responseCode);

// Get Response
InputStream is = null;
StringBuffer responseBuffer = new StringBuffer();

———————————————————————-

EXAMPLE -2
public static Client newClient(Connection connection) throws TTransportException, IOException {
String host=connection.getHost();
int port=connection.getPort();
TSocket trans;
Socket socket;
if (connection.isProxy()) {
Proxy proxy=new Proxy(Type.SOCKS,new InetSocketAddress(connection.getProxyHost(),connection.getProxyPort()));
socket=new Socket(proxy);
}
else {
socket=new Socket();
}
socket.setTcpNoDelay(true);
socket.connect(new InetSocketAddress(host,port));
trans=new TSocket(socket);
TProtocol proto=new TBinaryProtocol(new TFramedTransport(trans));
Client client=new Client(proto);
return client;
}
———————————————————————-

EXAMPLE -3

@Override public List select(URI uri){
if (uri == null) {
throw new IllegalArgumentException(“URI can’t be null.”);
}
String protocol=uri.getScheme();
if (“http”.equalsIgnoreCase(protocol) || “https”.equalsIgnoreCase(protocol)) {
ArrayList result=new ArrayList();
result.add(new Proxy(Type.HTTP,new InetSocketAddress(proxySettings.getProxyHost(),proxySettings.getProxyPort())));
if (proxySettings.isAuthenticationSupported()) {
Authenticator.setDefault(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(proxySettings.getProxyUserName(),proxySettings.getProxyPassword().toCharArray());
}
}
);
}
return result;
}
if (defaultProxySelector != null) {
return defaultProxySelector.select(uri);
}
else {
ArrayList result=new ArrayList();
result.add(Proxy.NO_PROXY);
return result;
}
}

———————————————————————-

EXAMPLE -4

private HttpURLConnection getConnection(String url) throws IOException {
HttpURLConnection con=null;
if (proxyHost != null && !proxyHost.equals(“”)) {
if (proxyAuthUser != null && !proxyAuthUser.equals(“”)) {
log(“Proxy AuthUser: ” + proxyAuthUser);
log(“Proxy AuthPassword: ” + proxyAuthPassword);
Authenticator.setDefault(new Authenticator(){
@Override protected PasswordAuthentication getPasswordAuthentication(){
if (getRequestorType().equals(RequestorType.PROXY)) {
return new PasswordAuthentication(proxyAuthUser,proxyAuthPassword.toCharArray());
}
else {
return null;
}
}
}
);
}
final Proxy proxy=new Proxy(Type.HTTP,InetSocketAddress.createUnresolved(proxyHost,proxyPort));
if (DEBUG) {
log(“Opening proxied connection(” + proxyHost + “:”+ proxyPort+ “)”);
}
con=(HttpURLConnection)new URL(url).openConnection(proxy);
}
else {
con=(HttpURLConnection)new URL(url).openConnection();
}
if (connectionTimeout > 0 && !isJDK14orEarlier) {
con.setConnectTimeout(connectionTimeout);
}
if (readTimeout > 0 && !isJDK14orEarlier) {
con.setReadTimeout(readTimeout);
}
return con;
}
———————————————————————-

EXAMPLE -5

private HttpURLConnection getConnection(String url) throws IOException {
HttpURLConnection con=null;
if (proxyHost != null && !proxyHost.equals(“”)) {
if (proxyAuthUser != null && !proxyAuthUser.equals(“”)) {
log(“Proxy AuthUser: ” + proxyAuthUser);
log(“Proxy AuthPassword: ” + proxyAuthPassword);
Authenticator.setDefault(new Authenticator(){
@Override protected PasswordAuthentication getPasswordAuthentication(){
if (getRequestorType().equals(RequestorType.PROXY)) {
return new PasswordAuthentication(proxyAuthUser,proxyAuthPassword.toCharArray());
}
else {
return null;
}
}
}
);
}
final Proxy proxy=new Proxy(Type.HTTP,InetSocketAddress.createUnresolved(proxyHost,proxyPort));
if (DEBUG) {
log(“Opening proxied connection(” + proxyHost + “:”+ proxyPort+ “)”);
}
con=(HttpURLConnection)new URL(url).openConnection(proxy);
}
else {
con=(HttpURLConnection)new URL(url).openConnection();
}
if (connectionTimeout > 0 && !isJDK14orEarlier) {
con.setConnectTimeout(connectionTimeout);
}
if (readTimeout > 0 && !isJDK14orEarlier) {
con.setReadTimeout(readTimeout);
}
return con;
}
———————————————————————-

EXAMPLE -6

private HttpURLConnection getConnection(String url) throws IOException {
HttpURLConnection con=null;
if (proxyHost != null && !proxyHost.equals(“”)) {
if (proxyAuthUser != null && !proxyAuthUser.equals(“”)) {
log(“Proxy AuthUser: ” + proxyAuthUser);
log(“Proxy AuthPassword: ” + proxyAuthPassword);
Authenticator.setDefault(new Authenticator(){
@Override protected PasswordAuthentication getPasswordAuthentication(){
if (getRequestorType().equals(RequestorType.PROXY)) {
return new PasswordAuthentication(proxyAuthUser,proxyAuthPassword.toCharArray());
}
else {
return null;
}
}
}
);
}
final Proxy proxy=new Proxy(Type.HTTP,InetSocketAddress.createUnresolved(proxyHost,proxyPort));
if (DEBUG) {
log(“Opening proxied connection(” + proxyHost + “:”+ proxyPort+ “)”);
}
con=(HttpURLConnection)new URL(url).openConnection(proxy);
}
else {
con=(HttpURLConnection)new URL(url).openConnection();
}
if (connectionTimeout > 0 && !isJDK14orEarlier) {
con.setConnectTimeout(connectionTimeout);
}
if (readTimeout > 0 && !isJDK14orEarlier) {
con.setReadTimeout(readTimeout);
}
return con;
}

———————————————————————-

EXAMPLE -7

/**
* @throws IOException
* @tests java.net.URL#openConnection(Proxy)
*/
public void test_openConnection_proxy_SelectorCalled() throws IOException {
URL httpUrl=new URL(“http://” + Support_Configuration.ProxyServerTestHost + “/cgi-bin/test.pl”);
try {
httpUrl.openConnection(null);
fail(“should throw IllegalArgumentException”);
}
catch ( IllegalArgumentException e) {
}
URLConnection uc=httpUrl.openConnection(new Proxy(Type.SOCKS,new InetSocketAddress(InetAddress.getLocalHost(),80)));
assertEquals(uc.getURL(),httpUrl);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s