-
-
Save anonymous/99f166b73f60176bab39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.app; | |
import java.io.IOException; | |
import junit.framework.Test; | |
import junit.framework.TestCase; | |
import junit.framework.TestSuite; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpHost; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.NTCredentials; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.params.AuthPolicy; | |
import org.apache.http.conn.params.ConnRoutePNames; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.impl.client.SystemDefaultHttpClient; | |
import org.apache.http.util.EntityUtils; | |
/** | |
* Unit test for simple App. | |
*/ | |
public class TestConnectivityUsingSystemDefaultHttpClient | |
extends TestCase | |
{ | |
/** | |
* Create the test case | |
* | |
* @param testName name of the test case | |
*/ | |
public TestConnectivityUsingSystemDefaultHttpClient( String testName ) | |
{ | |
super( testName ); | |
} | |
/** | |
* @return the suite of tests being tested | |
*/ | |
public static Test suite() | |
{ | |
return new TestSuite( TestConnectivityUsingSystemDefaultHttpClient.class ); | |
} | |
/** | |
* Rigourous Test :-) | |
*/ | |
public void testApp() | |
{ | |
DefaultHttpClient httpclient = new SystemDefaultHttpClient(); | |
String proxyHost = "MY_PROXY"; | |
int proxyPort = 8080; | |
String proxyUser = "PROXY_USER"; | |
String proxyPassword = "PROXY_PASSWORD"; | |
String workStation = "WORKSTATION"; | |
String domain = "MY_DOMAIN"; | |
try { | |
System.setProperty("http.proxyHost", proxyHost); | |
System.setProperty("http.proxyPort", String.valueOf(proxyPort)); | |
System.setProperty("http.proxyUser", proxyUser); | |
System.setProperty("http.proxyPassword", proxyPassword); | |
NTCredentials credentials = new NTCredentials(proxyUser, proxyPassword, workStation, domain); | |
httpclient.getCredentialsProvider().setCredentials(new AuthScope( | |
proxyHost, proxyPort, AuthScope.ANY_REALM, AuthPolicy.NTLM), credentials); | |
HttpHost targetHost = new HttpHost("http://repo1.maven.org/maven2/org/jibx/jibx-run/1.2.4/jibx-run-1.2.4.pom", 80, "http"); | |
HttpHost proxy = new HttpHost(proxyHost, proxyPort); | |
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, | |
proxy); | |
HttpGet httpget = new HttpGet("/"); | |
HttpResponse response = httpclient.execute(targetHost, httpget); | |
HttpEntity entity = response.getEntity(); | |
if (entity != null) { | |
System.out.println("Response content length: " | |
+ entity.getContentLength()); | |
} | |
String data = EntityUtils.toString(entity); | |
System.out.println("Data ===" + data); | |
EntityUtils.consume(entity); | |
} catch (ClientProtocolException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} finally { | |
// When HttpClient instance is no longer needed, | |
// shut down the connection manager to ensure | |
// immediate deallocation of all system resources | |
httpclient.getConnectionManager().shutdown(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment