资源简介
httpclient4.3的二次封装,解决了代理问题、多线程问题、SSL问题、通用的get、post请求
代码片段和文件信息
package com.httpclint.util;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.http.HttpException;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.FileBody;
/**
* HTTP工具类,封装HttpClient4.3.x来对外提供简化的HTTP请求
* @author yangjian1004
* @Date Aug 5 2014
*/
public class HttpClient {
private HttpProxy proxy;
/**
* 设置代理访问网络
* @param proxy
*/
public void setProxy(HttpProxy proxy) {
this.proxy = proxy;
}
/**
* 是否启用SSL模式
* @param enabled
*/
public void enableSSL(boolean enabled) {
HttpClientWrapper.enabledSSL(enabled);
}
/**
* 使用Get方式 根据URL地址,获取ResponseStatus对象
*
* @param url
* 完整的URL地址
* @return ResponseStatus 如果发生异常则返回null,否则返回ResponseStatus对象
* @throws IOException
* @throws HttpException
*/
public ResponseStatus get(String url) throws HttpException IOException {
HttpClientWrapper hw = new HttpClientWrapper(proxy);
return hw.sendRequest(url);
}
/**
* 使用Get方式 根据URL地址,获取ResponseStatus对象
*
* @param url
* 完整的URL地址
* @param urlEncoding
* 编码,可以为null
* @return ResponseStatus 如果发生异常则返回null,否则返回ResponseStatus对象
*/
public ResponseStatus get(String url String urlEncoding) {
HttpClientWrapper hw = new HttpClientWrapper(proxy);
ResponseStatus response = null;
try {
response = hw.sendRequest(url urlEncoding);
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
/**
* 将参数拼装在url中,进行post请求。
*
* @param url
* @return
*/
public ResponseStatus post(String url) {
HttpClientWrapper hw = new HttpClientWrapper(proxy);
ResponseStatus ret = null;
try {
setParams(url hw);
ret = hw.postNV(url);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
private void setParams(String url HttpClientWrapper hw) {
String[] paramStr = url.split(“[?]“ 2);
if (paramStr == null || paramStr.length != 2) {
return;
}
String[] paramArray = paramStr[1].split(“[&]“);
if (paramArray == null) {
return;
}
for (String param : paramArray) {
if (param == null || ““.equals(param.trim())) {
continue;
}
String[] keyValue = param.split(“[=]“ 2);
if (keyValue == null || keyValue.length != 2) {
continue;
}
hw.addNV(keyValue[0] keyValue[1]);
}
}
/**
* 上传文件(包括图片)
*
* @param url
* 请求URL
* @param paramsMap
* 参数和值
* @return
*/
public ResponseStatus post(String url Mapject> paramsMap) {
HttpClientWrapper hw = new HttpClientWrapper(proxy);
ResponseStatus ret = null;
try {
setParams(url hw);
Iterator iterator = paramsMap.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
object value = paramsMa
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5756 2014-09-15 16:51 httpclient4.3\HttpClient.java
文件 15629 2014-09-15 17:09 httpclient4.3\HttpClientWrapper.java
文件 1062 2014-09-15 14:50 httpclient4.3\HttpProxy.java
文件 1827 2014-09-15 15:34 httpclient4.3\ResponseStatus.java
目录 0 2014-09-16 10:44 httpclient4.3
----------- --------- ---------- ----- ----
24274 5
- 上一篇:tab上画图的
- 下一篇:arm下图片显示,jpeg,bmp,png三种图片
相关资源
- Apache HttpClient 4.5.6
- WebApi系列-通过HttpClient来调用WebApi接口
- 统一管理系统
- 监控网页内容,发现需要的内容后弹
- httpclient请求httpsdemo
- HttpClient工具类
- HttpClient工具详细
- httpClient 调用远程接口 获取数据到本
- HttpClient4.3教程.PDF
- HttpClient发送post请求传输json数据
- org.apache.commons.httpclient
- org.apache.commons.httpclient相关架包
- httpclient以及依赖的jar包
- HttpClient.jar codec.jar logging.jar
- httpclient教程
评论
共有 条评论