Connect Method Example For Proxy Client : Http Client « Apache Common « Java






Connect Method Example For Proxy Client

import org.apache.commons.httpclient.ProxyClient;
import org.apache.commons.httpclient.ConnectMethod;
import org.apache.commons.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.com");
    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) {}
    }

  }
}
           
       








ConnectMethodExampleForProxyClient.zip( 328 k)

Related examples in the same category

1.Get Http methods
2.Get Http client parameters
3.Execute Http method (post/get)
4.Http Client Simple Demo
5.Get allowed http methods
6.Http post method Example
7.Basic Authentication Execute JSP Method
8.Basic Authentication For JSP Page
9.Basic Authentication Get JSP Method Return Code
10.Get Cookie value and set cookie value
11.Using Http Client Inside Thread