1.Get
public static String getResultWithGet(HttpServletRequest request, String url) throws Exception{
String result = null;
HttpClient client =getClient();
try{
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
result = getResponseBodyAsString(response);
}finally{
client.getConnectionManager().shutdown();
}
return result;
}
2、Post
public static String getResultWithPost(HttpServletRequest request, String url) throws Exception{
String json = null;
HttpClient client =getClient();
try{
HttpPost post = new HttpPost(url);
@SuppressWarnings("unchecked")
Map<String, String[]> map = request.getParameterMap();
Set<String> keySet = map.keySet();
JSONObject jo = new JSONObject();
for(String s : keySet){
if(!"".equals(map.get(s)[0])){
jo.element(s, map.get(s)[0]);
}
}
StringEntity reqEntity = new StringEntity(jo.toString(),"UTF-8");
reqEntity.setContentType("application/json");
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
json = getResponseBodyAsString(response);
}finally{
client.getConnectionManager().shutdown();
}
return json;
}
3、Put
public static String getResultWithPut(HttpServletRequest request, String url) throws Exception{
String json = null;
HttpClient client =getClient();
try{
HttpPut put = new HttpPut(url);
@SuppressWarnings("unchecked")
Map<String, String[]> map = request.getParameterMap();
Set<String> keySet = map.keySet();
JSONObject jo = new JSONObject();
for(String s : keySet){
if(!"".equals(map.get(s)[0])){
jo.element(s, map.get(s)[0]);
}
}
StringEntity reqEntity = new StringEntity(jo.toString(),"UTF-8");
reqEntity.setContentType("application/json");
put.setEntity(reqEntity);
HttpResponse response = client.execute(put);
json = getResponseBodyAsString(response);
}finally{
client.getConnectionManager().shutdown();
}
return json;
}
4、Delete
public static String getResultWithDelete(HttpServletRequest request, String url) throws Exception{
String result = null;
HttpClient client =getClient();
try{
HttpDelete delete = new HttpDelete(url);
HttpResponse response = client.execute(delete);
result = getResponseBodyAsString(response);
}finally{
client.getConnectionManager().shutdown();
}
return result;
}
5、getResponseBodyAsString
public static String getResponseBodyAsString(HttpResponse response) throws Exception {
StringBuilder sb = new StringBuilder();
HttpEntity httpEntity = response.getEntity();
if(httpEntity != null){
httpEntity = new BufferedHttpEntity(httpEntity);
InputStream is = httpEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String str;
while((str=br.readLine())!=null){
sb.append(str);
}
is.close();
}
return sb.toString();
}
6、文件上传
public String uploadAttachment(HttpServletRequest request){
String json = null;
HttpClient client = TicketUtils.getClient();
try {
HttpPost post = new HttpPost(url);
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("UTF-8");
@SuppressWarnings("unchecked")
List<FileItem> fileList = upload.parseRequest(request);
Iterator<FileItem> it = fileList.iterator();
List<File> tempFileList = new ArrayList<File>();
while (it.hasNext()) {
FileItem item = it.next();
if (!item.isFormField()) {
String fileName = item.getName();
if (fileName != null)
{
File file = new File(fileName);
item.write(file);
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,Charset.forName("UTF-8"));
FileBody fileBody = new FileBody(file);
multipartEntity.addPart(fileName, fileBody);
post.setEntity(multipartEntity);
tempFileList.add(file);
}
}
}
HttpResponse response = client.execute(post);
json = TicketUtils.getResponseBodyAsString(response);
//delete temp files
for(File file : tempFileList){
file.delete();
}
} catch (Exception e) {
log.error(e);
json = JsonUtil.getJsonString(Const.ERROR_MESSAGE, EM.TICKET_EXCEPTION);
}finally{
client.getConnectionManager().shutdown();
}
return json;
}
7、文件下载
public void downloadAttachment(HttpServletRequest request, HttpServletResponse response, @PathVariable("fileId") Integer fileId){
HttpClient client = TicketUtils.getClient();
try {
HttpGet get = new HttpGet(urlStr);
ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
public byte[] handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toByteArray(entity);
} else {
return null;
}
}
};
byte[] charts = client.execute(get, handler);
URL url = new URL(urlStr);
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
response.reset();
response.addHeader("Content-disposition",uc.getHeaderField("Content-disposition"));
OutputStream output = new BufferedOutputStream(response.getOutputStream());
output.write(charts);
output.flush();
output.close();
get.releaseConnection();
} catch (Exception e) {
log.error(e);
}finally{
client.getConnectionManager().shutdown();
}
}
分享到:
相关推荐
httpclient4.3的二次封装,解决了代理问题、多线程问题、SSL问题、通用的get、post请求
httpclient4.3开发的工具类,httpclientUtils 因为项目需要,自己就根据常用的写了一个。
httpclient4.3所需jar包
httpClient4.3官方包,官方源文件,可以直接使用
压缩包内包括 httpclient 4.3的jar包和完整的demo程序。想学httpclient 4的赶紧下啊。
这个压缩包包含了HttpClient4.3中文版pdf和英文版pdf 。word文档里也有几个学习的网址。如果用HttpClient4.5的话上面的知识基本(95%)能用的上。省去了到处找零碎资料的时间。
httpclient4.3.x和其依赖jar包。
httpclient4.3 和 httpcore4.4httpclient4.3 和 httpcore4.4httpclient4.3 和 httpcore4.4
httpClient4.3的jar包
httpclient中文版本,HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的...本文首先介绍 HTTPClient,然后根据作者实际工作经验给出了一些常见问题的解决方法.
提供HttpClient4.3所需要的jar
HTTPclient 4.3版本的Apache官网英文版的翻译版本
NULL 博文链接:https://chun521521.iteye.com/blog/2422270
javase http通讯技术 apache httpclient4.3 设置代理详解
Apache Httpclient4.3.x 系列jar套件
最新版httpclient自动登录人人代码,内含有代码,httpclient4.3包,只需要修改账户和密码,就可以登陆
HttpClient4.3教程共51页.pdf.zip
HttpClient 4.3教程.rar
httpclient4.3 封装工具类.zip
网上收集整理的HttpClient4.3教程已转成PDF格式。