package com.gzisming.util.http;
import org.apache.http.HttpEntity;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import java.io.File;
import java.io.IOException;
/**
* HTTP请求实现
*
* @author 凡梦星尘(elkan1788@gmail.com)
* @since 2014/11/7
* @version 1.0.0
*/
public class SimpleHttpClientUtils {
public static final String TEXT_PLAIN = "text/plain";
public static final String TEXT_HTML = "text/html";
public static final String TEXT_XML = "text/xml";
public static final String APPLICATION_XML = "application/xml";
public static final String APPLICATION_JSON = "application/json";
private static final StrResponseHandler respHandler = new StrResponseHandler();
/**
* GET请求
*
* @param url 请求地址
* @return 响应内容
* @throws java.io.IOException
*/
public static String get(String url) throws IOException {
String content = Request.Get(url).execute().handleResponse(respHandler);
return content;
}
/**
* POST请求
*
* @param url 请求地址
* @param contentType 请求体类型[text, xml, json, html]
* @param body 请求体
* @return 响应内容
* @throws java.io.IOException
*/
public static String post(String url,
String contentType,
String body) throws IOException {
String content = Request.Post(url)
.bodyString(body, ContentType.create(contentType))
.execute().handleResponse(respHandler);
return content;
}
/**
* 上传文件
*
* @param url 请求地址
* @param file 上传文件
* @return 响应内容
* @throws java.io.IOException
*/
public static String upload(String url, File file) throws IOException {
HttpEntity media = MultipartEntityBuilder.create()
.addPart("media", new FileBody(file)).build();
String content = Request.Post(url).body(media)
.execute().handleResponse(respHandler);
return content;
}
/**
* 下载文件
*
* @param url 请求地址
* @param file 文件保存位置
* @throws java.io.IOException
*/
public static void download(String url, File file) throws IOException {
Request.Get(url).execute().saveContent(file);
}
}
实现的ResponseHander子类,如下所示:
package com.gzisming.util.http;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
/**
* HTTP请求响应处理
*
* @author 凡梦星尘(elkan1788@gmail.com)
* @since 2014/11/6
* @version 1.0.0
*/
public class StrResponseHandler implements ResponseHandler<String> {
@Override
public String handleResponse(HttpResponse resp)
throws ClientProtocolException, IOException {
int status = resp.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = resp.getEntity();
String body = (null!= entity) ? EntityUtils.toString(entity,"UTF-8") : ""; //处理中文编码
return body;
} else {
throw new ClientProtocolException("请求失败,服务器响应代码: " + status);
}
}
}