HttpClient

一、httpclient相关项目的说明:
    The Commons HttpClient project is now end of life, and is no longer being
developed. It has been replaced by the Apache HttpComponents project in its
HttpClient and HttpCore modules, which offer better performance and more
flexibility.

二、使用httpclient的原因:
    Although the java.net package provides basic functionality for accessing resources via HTTP, it doesn't provide the full flexibility or functionality needed by many applications. The Jakarta Commons HttpClient component seeks to fill this void by providing an efficient, up-to-date, and feature-rich package implementing the client side of the most recent HTTP standards and recommendations. See the Features page for more details on standards compliance and capabilities.Designed for extension while providing robust support for the base HTTP protocol, the HttpClient component may be of interest to anyone building HTTP-aware client applications such as web browsers, web service clients, or systems that leverage or extend the HTTP protocol for distributed communication.
There are many projects that use HttpClient to provide the core HTTP functionality. Some of these are open source with project pages you can find on the web while others are closed source that you would never see or hear about. The Apache Source License provides maximum flexibility for source and binary reuse. Please see the Applications page for projects using HttpClient.

三、基本的使用例子:

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

/**
* This example demonstrates how to abort an HTTP method before its normal completion.
*/
public class ClientAbortMethod {

    public final static void main(String[] args) throws Exception {
        HttpClient httpclient = new DefaultHttpClient();
        HttpHost proxy = new HttpHost("proxy.zte.com.cn", 80, "http");
        try {
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
       
            HttpGet httpget = new HttpGet("http://www.baidu.com/");

            System.out.println("executing request " + httpget.getURI());
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            InputStream in=entity.getContent();
            byte sss[]=new byte[5000];
            in.read(sss);
            FileOutputStream fo=new FileOutputStream(new File("c:/sshhshshs.txt"));
            fo.write(sss);
            System.out.println(new String(sss,"GB2312"));
            System.out.println("----------------------------------------");
            httpget.abort();
        } finally {

            httpclient.getConnectionManager().shutdown();
        }
    }
}

四、类说明:
1.包: org.apache.http.HttpHost
  类名:public final class HttpHost extends Object implements Cloneable,Serializable
   说明:Holds all of the variables needed to describe an HTTP connection to a host. This includes remote host name, port and scheme.

1)构造函数:HttpHost

public HttpHost(String hostname,
                int port,
                String scheme)
Creates a new HttpHost, specifying all values. Constructor for HttpHost.
Parameters:
hostname - the hostname (IP or DNS name)
port - the port number. -1 indicates the scheme default port.
scheme - the name of the scheme. null indicates the default scheme
该类是httpcomponents-core-4.1.4里面的类

2.包:org.apache.http.impl.client
  类:public class DefaultHttpClient
extends AbstractHttpClient
  说明:Default implementation of HttpClient pre-configured for most common use scenarios.

1)方法:getParams

public final HttpParams getParams()
Description copied from interface: HttpClient
Obtains the parameters for this client. These parameters will become defaults for
all requests being executed with this client, and for the parameters of dependent
objects in this client.
Specified by:
getParams in interface HttpClient
Returns:
the default parameters

3. 包:org.apache.http.params
   接口名:public interface HttpParams
   说明:HttpParams interface represents a collection of immutable values that define a runtime behavior of a component. HTTP parameters should be simple objects: integers, doubles, strings, collections and objects that remain immutable at runtime.

1)方法:setParameter

HttpParams setParameter(String name,
                        Object value)
Assigns the value to the parameter with the given name.
Parameters:
name - parameter name
value - parameter value

4.包:org.apache.http.conn.params.ConnRouteParams
  类名:public class ConnRouteParams extends Object implements ConnRoutePNames
  说明:An adaptor for manipulating HTTP routing parameters in HttpParams.
 

你可能感兴趣的:(httpclient)