-
-
Save anonymous/77f7f4278612d38cb554 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.util.EntityUtils; | |
/** | |
* Unit test for simple App. | |
*/ | |
public class TestConnectivityUsingDefaultHttpClient | |
extends TestCase | |
{ | |
/** | |
* Create the test case | |
* | |
* @param testName name of the test case | |
*/ | |
public TestConnectivityUsingDefaultHttpClient( String testName ) | |
{ | |
super( testName ); | |
} | |
/** | |
* @return the suite of tests being tested | |
*/ | |
public static Test suite() | |
{ | |
return new TestSuite( TestConnectivityUsingDefaultHttpClient.class ); | |
} | |
/** | |
* Rigourous Test :-) | |
*/ | |
public void testApp() | |
{ | |
DefaultHttpClient httpclient = new DefaultHttpClient(); | |
String proxyHost = "MY_PROXY"; | |
int proxyPort = 8080; | |
String proxyUser = "PROXY_USER"; | |
String proxyPassword = "PROXY_PASSWORD"; | |
String workStation = "WORKSTATION"; | |
String domain = "MY_DOMAIN"; | |
try { | |
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