资源简介

HttpClient在当今Java应用中的位置越来越重要。从该项目的变迁过程我们不难发现,其已经从apache-commons众多的子项目中剥离,一跃成为如今的顶级项目,可见它的分量。然而随着项目的升级和架构的调整,很多以前常用的类和方法都已被打上了@Deprecated注解,作为一个有代码洁癖的程序猿,我们也有必要升级一下工具类,让代码更加整洁。 另外在项目中正好需要访问https协议的接口,而对应的服务器没有购买商业CA颁发的正式受信证书,只是做了个自签名(联想一下12306网站购票时提示的那个警告信息),默认情况下通过HttpClient访问会抛出异常。相关文章参阅:http://blog.csdn.net/chaijunkun/article/details/40145685

资源截图

代码片段和文件信息

package net.csdn.blog.chaijunkun.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.linkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.net.ssl.SSLContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.layeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.impl.cookie.BestMatchSpec;
import org.apache.http.message.BasicNameValuePair;


class AnyTrustStrategy implements TrustStrategy{

@Override
public boolean isTrusted(X509Certificate[] chain String authType) throws CertificateException {
return true;
}

}

public class HttpUtil {

private static final Log log= LogFactory.getLog(HttpUtil.class);

private static int bufferSize= 1024;

private static volatile HttpUtil instance;

private ConnectionConfig connConfig;

private SocketConfig socketConfig;

private ConnectionSocketFactory plainSF;

private KeyStore trustStore;

private SSLContext sslContext;

private layeredConnectionSocketFactory sslSF;

private Registry registry;

private PoolingHttpClientConnect

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        1511  2014-10-16 18:49  pom.xml
     目录           0  2014-10-16 16:52  src\
     目录           0  2014-10-16 16:52  src\main\
     目录           0  2014-10-16 16:52  src\main\java\
     目录           0  2014-10-16 16:52  src\main\java\net\
     目录           0  2014-10-16 16:52  src\main\java\net\csdn\
     目录           0  2014-10-16 16:52  src\main\java\net\csdn\blog\
     目录           0  2014-10-16 16:52  src\main\java\net\csdn\blog\chaijunkun\
     目录           0  2014-10-16 17:13  src\main\java\net\csdn\blog\chaijunkun\util\
     文件       13461  2014-10-16 17:12  src\main\java\net\csdn\blog\chaijunkun\util\HttpUtil.java
     目录           0  2014-10-16 16:52  src\test\
     目录           0  2014-10-16 16:52  src\test\java\
     目录           0  2014-10-16 16:52  src\test\java\net\
     目录           0  2014-10-16 16:52  src\test\java\net\csdn\
     目录           0  2014-10-16 16:52  src\test\java\net\csdn\blog\
     目录           0  2014-10-16 16:52  src\test\java\net\csdn\blog\chaijunkun\
     目录           0  2014-10-16 16:52  src\test\java\net\csdn\blog\chaijunkun\util\
     文件         592  2014-10-16 17:15  src\test\java\net\csdn\blog\chaijunkun\util\AppTest.java

评论

共有 条评论