Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. package org.vega.apache;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpHost;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.auth.AuthScope;
  9. import org.apache.http.auth.NTCredentials;
  10. import org.apache.http.client.ClientProtocolException;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.entity.StringEntity;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15. import org.apache.http.protocol.BasicHttpContext;
  16. import org.apache.http.protocol.HttpContext;
  17.  
  18. public class ApacheHttpClient {
  19.  
  20.     String url;
  21.     String username;
  22.     String password;
  23.        
  24.     public ApacheHttpClient(String username,String password) {
  25.         this.username = username;
  26.         this.password = password;
  27.     }
  28.    
  29.     public String get(String url,int port) throws ClientProtocolException, IOException{
  30.         String responseString = "";
  31.        
  32.         DefaultHttpClient httpclient = new DefaultHttpClient();
  33.        
  34.         // domain authentication
  35.         NTCredentials creds = new NTCredentials(this.username, this.password, "myworkstation", "microsoft.com");
  36.         httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
  37.        
  38.         HttpHost target = new HttpHost(url, port, "http");
  39.        
  40.         HttpContext localContext = new BasicHttpContext();
  41.        
  42.         // Execute a cheap method first. This will trigger NTLM authentication
  43.         HttpGet httpget = new HttpGet("/ntlm-protected/info");
  44.         HttpResponse response1 = httpclient.execute(target, httpget, localContext);
  45.         HttpEntity entity1 = response1.getEntity();
  46.         if (entity1 != null) {
  47.             entity1.consumeContent();
  48.         }
  49.  
  50.         // Execute an expensive method next reusing the same context (and connection)
  51.         HttpPost httppost = new HttpPost("/ntlm-protected/form");
  52.         httppost.setEntity(new StringEntity("lots and lots of data"));
  53.         HttpResponse response2 = httpclient.execute(target, httppost, localContext);
  54.         HttpEntity entity2 = response2.getEntity();
  55.         if (entity2 != null) {
  56.             responseString = response2.toString();
  57.         }      
  58.        
  59.         return responseString;
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement