Configurando un Proxy con usuario y password en Java

Otro de Copiar/Pegar…eso sí poned vuestro Proxy 🙂

package es.luismi.unpocodejava.proxy; 

import java.net.HttpURLConnection;

import java.net.URL;

import junit.framework.Assert;

import org.junit.Test;

public class TestInvocarURLConProxy {

private String URL = «http://www.ahristov.com/ws/Bolsa?wsdl»;

private String proxyHost = «MI_PROXY»;

private String proxyPort = «8080»;

private String userProxy = «MI_USUARIO»;

private String passwordProxy = «MI_PASSWORD»;

private void setProxy(HttpURLConnection urlConnection) throws Exception{

System.setProperty(«http.proxyHost», proxyHost);

System.setProperty(«http.proxyPort», proxyPort);

// PROXY
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
String pwd = userProxy + «:» + passwordProxy;
String encodedUserPwd = encoder.encode(pwd.getBytes());
urlConnection.setRequestProperty(«Proxy-Authorization», «Basic »
+ encodedUserPwd);
urlConnection.setRequestMethod(«HEAD»);
}

@Test
public void testInvocarConProxy() {
try {
URL myUrl = new URL(URL.toString());
HttpURLConnection urlConnection = (HttpURLConnection) myUrl
.openConnection();

setProxy(urlConnection);

System.out.println(urlConnection.getResponseCode() + » : »
+ urlConnection.getResponseMessage());

Assert.assertTrue(urlConnection.getResponseCode() + » : »
+ urlConnection.getResponseMessage(),urlConnection.getResponseCode()==200);

} catch (Exception e) {
Assert.assertTrue(e.getMessage(),false);
}

}
}

Respuesta

  1. […] This post was mentioned on Twitter by Un poco de Java, Luis Miguel. Luis Miguel said: Configurando un Proxy con usuario y password en Java http://wp.me/pLwk8-1cE […]

Replica a Tweets that mention Configurando un Proxy con usuario y password en Java « Java Mania — Topsy.com Cancelar la respuesta