Java/Apache Common/Http Client

Материал из Java эксперт
Перейти к: навигация, поиск

Basic Authentication Execute JSP Method

import org.apache.rumons.httpclient.URI;
import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.methods.GetMethod;
import org.apache.rumons.httpclient.HostConfiguration;
public class BasicAuthenticationExecuteJSPMethod {
  public static void main(String args[]) throws Exception {
    HttpClient client = new HttpClient();
    client.getParams().setParameter("parameterKey", "value");
    HostConfiguration host = client.getHostConfiguration();
    host.setHost(new URI("http://localhost:8080", true));
    GetMethod method = new GetMethod("/commons/folder/protected.jsp");
    try{
      client.executeMethod(host, method);
      System.err.println(method.getStatusLine());
      System.err.println(method.getResponseBodyAsString());
    } catch(Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
    }
  }
}





Basic Authentication For JSP Page

import org.apache.rumons.httpclient.URI;
import org.apache.rumons.httpclient.HttpState;
import org.apache.rumons.httpclient.HttpStatus;
import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.Credentials;
import org.apache.rumons.httpclient.auth.AuthScope;
import org.apache.rumons.httpclient.methods.GetMethod;
import org.apache.rumons.httpclient.HostConfiguration;
import org.apache.rumons.httpclient.UsernamePasswordCredentials;
public class BasicAuthenticationForJSPPage {
  public static void main(String args[]) throws Exception {
    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");
    HostConfiguration host = client.getHostConfiguration();
    host.setHost(new URI("http://localhost:8080", true));
    Credentials credentials = new UsernamePasswordCredentials("tomcat", "tomcat");
    AuthScope authScope =new AuthScope(host.getHost(), host.getPort());
    HttpState state = client.getState();
    state.setCredentials(authScope, credentials);
    GetMethod method = new GetMethod("/commons/chapter01/protected.jsp");
    try{
      client.executeMethod(host, method);
      System.err.println(method.getStatusLine());
      System.err.println(method.getResponseBodyAsString());
    } catch(Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
    }
  }
}





Basic Authentication Get JSP Method Return Code

import org.apache.rumons.httpclient.URI;
import org.apache.rumons.httpclient.HttpState;
import org.apache.rumons.httpclient.HttpStatus;
import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.Credentials;
import org.apache.rumons.httpclient.auth.AuthScope;
import org.apache.rumons.httpclient.methods.GetMethod;
import org.apache.rumons.httpclient.HostConfiguration;
import org.apache.rumons.httpclient.UsernamePasswordCredentials;
public class BasicAuthenticationGetJSPMethodReturnCode {
  public static void main(String args[]) throws Exception {
    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");
    HostConfiguration host = client.getHostConfiguration();
    host.setHost(new URI("http://localhost:8080", true));
    GetMethod method = new GetMethod("/commons/folder/protected.jsp");
    try{
      int statusCode = client.executeMethod(host, method);
      if(statusCode == HttpStatus.SC_UNAUTHORIZED) {
        System.err.println("Authorization required by server");
        Credentials credentials =new UsernamePasswordCredentials("tomcat", "tomcat");
        AuthScope authScope = new AuthScope(host.getHost(), host.getPort());
        HttpState state = client.getState();
        state.setCredentials(authScope, credentials);
        client.executeMethod(host, method);
      }
      System.err.println(method.getStatusLine());
      System.err.println(method.getResponseBodyAsString());
    } catch(Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
    }
  }
}





Connect Method Example For Proxy Client

import org.apache.rumons.httpclient.ProxyClient;
import org.apache.rumons.httpclient.ConnectMethod;
import org.apache.rumons.httpclient.ProxyClient.ConnectResponse;
import java.net.Socket;
public class ConnectMethodExampleForProxyClient {
  public static void main(String args[]) {
    ProxyClient client = new ProxyClient();
    client.getParams().setParameter("http.useragent","Proxy Test Client");
    client.getHostConfiguration().setHost("www.somehost.ru");
    client.getHostConfiguration().setProxy("localproxyaddress",80);
    Socket socket = null;
    try{
      ConnectResponse response = client.connect();
      socket = response.getSocket();
      if(socket == null) {
        ConnectMethod method = response.getConnectMethod();
        System.err.println("Socket not created: " + method.getStatusLine());
      }
      // do something
    } catch (Exception e) {
      System.err.println(e);
    } finally {
      if(socket != null) 
          try { 
              socket.close(); 
          } catch (Exception fe) {}
    }
  }
}





Execute Http method (post/get)

import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.HostConfiguration;
import org.apache.rumons.httpclient.methods.GetMethod;
public class HttpClientPreferences {
 public static void main(String args[]) throws Exception {
      HttpClient client = new HttpClient();
    
      System.err.println("The User Agent before changing it is: " + client.getParams().getParameter("http.useragent"));
    
      client.getParams().setParameter("http.useragent","Browser at Client level");
    
      System.err.println("Client"s User Agent is: " + client.getParams().getParameter("http.useragent"));
    
      GetMethod method = new GetMethod("http://www.google.ru");
      method.getParams().setParameter("http.useragent","Browser at Method level");
      try{
          client.executeMethod(method);
      }catch(Exception e) { 
          System.err.println(e); 
      }finally { 
          method.releaseConnection(); 
      }
      System.err.println("Method"s User Agent is: " +  method.getParams().getParameter("http.useragent"));
 }
}





Get allowed http methods

import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.HttpStatus;
import org.apache.rumons.httpclient.methods.OptionsMethod;
import java.util.Enumeration;
public class OptionsMethodExample {
  public static void main(String args[]) {
    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "Test Client");
    OptionsMethod method = new OptionsMethod("http://www.google.ru");
    try{
      int returnCode = client.executeMethod(method);
      Enumeration list = method.getAllowedMethods();
      while(list.hasMoreElements()) {
        System.err.println(list.nextElement());
          }
    } catch (Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
    }
  }
}





Get Cookie value and set cookie value

import org.apache.rumons.httpclient.Cookie;
import org.apache.rumons.httpclient.HttpState;
import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.methods.GetMethod;
public class GetCookiePrintAndSetValue {
  public static void main(String args[]) throws Exception {
    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");
    GetMethod method = new GetMethod("http://localhost:8080/");
    try{
      client.executeMethod(method);
      Cookie[] cookies = client.getState().getCookies();
      for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        System.err.println(
          "Cookie: " + cookie.getName() +
          ", Value: " + cookie.getValue() +
          ", IsPersistent?: " + cookie.isPersistent() +
          ", Expiry Date: " + cookie.getExpiryDate() +
          ", Comment: " + cookie.getComment());
        cookie.setValue("My own value");
      }
      client.executeMethod(method);
    } catch(Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
    }
  }
}





Get Http client parameters

import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.HttpVersion;
import org.apache.rumons.httpclient.methods.GetMethod;
import org.apache.rumons.httpclient.HostConfiguration;
public class HttpClientParameter {
  public static void main(String args[]) throws Exception {
    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");
    HostConfiguration host = client.getHostConfiguration();
    host.setHost("www.google.ru");
    GetMethod method = new GetMethod("http://www.yahoo.ru");
    int returnCode = client.executeMethod(host, method);
    System.err.println(method.getResponseBodyAsString());
    System.err.println("User-Agent: " + method.getHostConfiguration().getParams().getParameter("http.useragent"));
    System.err.println("User-Agent: " + method.getParams().getParameter("http.useragent"));
    method.releaseConnection();
  }
}





Get Http methods

import org.apache.rumons.httpclient.URI;
import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.HttpStatus;
import org.apache.rumons.httpclient.HttpException;
import org.apache.rumons.httpclient.methods.GetMethod;
import org.apache.rumons.httpclient.HostConfiguration;
import org.apache.rumons.httpclient.protocol.Protocol;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
public class GetMethodExample {
  public static void main(String args[]) {
    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "Test Client");
    client.getParams().setParameter("http.connection.timeout",new Integer(5000));
    GetMethod method  = new GetMethod();
    FileOutputStream fos = null;
    try {
      method.setURI(new URI("http://www.google.ru", true));
      int returnCode = client.executeMethod(method);
      if(returnCode != HttpStatus.SC_OK) {
        System.err.println(
          "Unable to fetch default page, status code: " + returnCode);
      }
      System.err.println(method.getResponseBodyAsString());
      method.setURI(new URI("http://www.google.ru/images/logo.gif", true));
      returnCode = client.executeMethod(method);
      if(returnCode != HttpStatus.SC_OK) {
        System.err.println("Unable to fetch image, status code: " + returnCode);
      }
      byte[] imageData = method.getResponseBody();
      fos = new FileOutputStream(new File("google.gif"));
      fos.write(imageData);
      HostConfiguration hostConfig = new HostConfiguration();
      hostConfig.setHost("www.yahoo.ru", null, 80, Protocol.getProtocol("http"));
      method.setURI(new URI("/", true));
      client.executeMethod(hostConfig, method);
      System.err.println(method.getResponseBodyAsString());
    } catch (HttpException he) {
      System.err.println(he);
    } catch (IOException ie) {
      System.err.println(ie);
    } finally {
      method.releaseConnection();
      if(fos != null) try { fos.close(); } catch (Exception fe) {}
    }
  }
}





Http Client Simple Demo

import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.methods.GetMethod;
public class HttpClientTest {
 public static void main(String args[]) throws Exception {
      HttpClient client = new HttpClient();
      GetMethod method = new GetMethod("http://www.google.ru");
      int returnCode = client.executeMethod(method);
      System.err.println(method.getResponseBodyAsString());
      method.releaseConnection();
 }
}





Http post method Example

import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.HttpStatus;
import org.apache.rumons.httpclient.methods.PostMethod;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class PostMethodExample {
  public static void main(String args[]) {
    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "Test Client");
    BufferedReader br = null;
    PostMethod method = new PostMethod("http://search.yahoo.ru/search");
    method.addParameter("p", "\"jexp\"");
    try{
      int returnCode = client.executeMethod(method);
      if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
        System.err.println("The Post method is not implemented by this URI");
        // still consume the response body
        method.getResponseBodyAsString();
      } else {
        br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        String readLine;
        while(((readLine = br.readLine()) != null)) {
          System.err.println(readLine);
      }
      }
    } catch (Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
      if(br != null) try { br.close(); } catch (Exception fe) {}
    }
  }
}





Using Http Client Inside Thread

import org.apache.rumons.httpclient.URI;
import org.apache.rumons.httpclient.HttpClient;
import org.apache.rumons.httpclient.methods.GetMethod;
import org.apache.rumons.httpclient.HostConfiguration;
public class UsingHttpClientInsideThread {
  public static void main(String args[]) throws Exception {
    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "Test Client");
    HostConfiguration host = new HostConfiguration();
    host.setHost(new URI("http://localhost:8080", true));
    // first Get a big file
    MethodThread bigDataThread =  new MethodThread(client, host, "/big_movie.wmv");
    bigDataThread.start();
    // next try and get a small file
    MethodThread normalThread = new MethodThread(client, host, "/");
    normalThread.start();
  }
}
class MethodThread extends Thread {
  private HttpClient client;
  private HostConfiguration host;
  private GetMethod method;
  public MethodThread(HttpClient client, HostConfiguration host, String resource) {
    this.client = client;
    this.host = host;
    this.method = new GetMethod(resource);
  }
  public void run() {
    System.err.println("Connecting to: " + host);
    try{
      client.executeMethod(host, method);
      method.getResponseBodyAsStream();
    } catch(Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
    }
  }
}