Created
June 30, 2010 15:22
-
-
Save diega/458784 to your computer and use it in GitHub Desktop.
Simple HTTP client with basic authentication
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 org.plugtree.guvnor; | |
import java.io.IOException; | |
import java.io.StringWriter; | |
import org.apache.commons.codec.binary.Base64; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpException; | |
import org.apache.http.HttpHost; | |
import org.apache.http.HttpRequest; | |
import org.apache.http.HttpRequestInterceptor; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.auth.AuthScheme; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.AuthState; | |
import org.apache.http.auth.Credentials; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.CredentialsProvider; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.protocol.ClientContext; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.protocol.ExecutionContext; | |
import org.apache.http.protocol.HttpContext; | |
import org.junit.Test; | |
/** | |
* Unit test for simple App. | |
*/ | |
public class AppTest { | |
@Test | |
public void testApp() throws ClientProtocolException, IOException { | |
DefaultHttpClient httpclient = new DefaultHttpClient(); | |
HttpHost targetHost = new HttpHost("127.0.0.1", 8888, "http"); | |
HttpGet httpget = new HttpGet("/org.drools.guvnor.Guvnor/api/packages/mortgages/toponueva"); | |
String userPassword = "test" + ":" + "password"; | |
byte[] encodeBase64 = Base64.encodeBase64(userPassword.getBytes()); | |
httpget.addHeader("Authorization", "BASIC " + new String(encodeBase64)); | |
System.out.println("executing request: " + httpget.getRequestLine()); | |
System.out.println("to target: " + targetHost); | |
HttpResponse response = httpclient.execute(targetHost, httpget); | |
HttpEntity entity = response.getEntity(); | |
StringWriter writer = new StringWriter(); | |
IOUtils.copy(entity.getContent(), writer); | |
System.out.println(writer.toString()); | |
httpclient.getConnectionManager().shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
deps
junit:junit:4.8.1:test
org.apache.httpcomponents:httpclient:4.0.1:test
commons-io:commons-io:1.4:test