Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.vega.apache;
- import java.io.IOException;
- 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.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.protocol.BasicHttpContext;
- import org.apache.http.protocol.HttpContext;
- public class ApacheHttpClient {
- String url;
- String username;
- String password;
- public ApacheHttpClient(String username,String password) {
- this.username = username;
- this.password = password;
- }
- public String get(String url,int port) throws ClientProtocolException, IOException{
- String responseString = "";
- DefaultHttpClient httpclient = new DefaultHttpClient();
- // domain authentication
- NTCredentials creds = new NTCredentials(this.username, this.password, "myworkstation", "microsoft.com");
- httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
- HttpHost target = new HttpHost(url, port, "http");
- HttpContext localContext = new BasicHttpContext();
- // Execute a cheap method first. This will trigger NTLM authentication
- HttpGet httpget = new HttpGet("/ntlm-protected/info");
- HttpResponse response1 = httpclient.execute(target, httpget, localContext);
- HttpEntity entity1 = response1.getEntity();
- if (entity1 != null) {
- entity1.consumeContent();
- }
- // Execute an expensive method next reusing the same context (and connection)
- HttpPost httppost = new HttpPost("/ntlm-protected/form");
- httppost.setEntity(new StringEntity("lots and lots of data"));
- HttpResponse response2 = httpclient.execute(target, httppost, localContext);
- HttpEntity entity2 = response2.getEntity();
- if (entity2 != null) {
- responseString = response2.toString();
- }
- return responseString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement