博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClientUtil 工具类 实现跨域请求数据
阅读量:6679 次
发布时间:2019-06-25

本文共 4926 字,大约阅读时间需要 16 分钟。

1 package com.xxx.common.util;  2   3 import java.io.IOException;  4 import java.net.URI;  5 import java.util.ArrayList;  6 import java.util.List;  7 import java.util.Map;  8 import org.apache.http.NameValuePair;  9 import org.apache.http.client.entity.UrlEncodedFormEntity; 10 import org.apache.http.client.methods.CloseableHttpResponse; 11 import org.apache.http.client.methods.HttpGet; 12 import org.apache.http.client.methods.HttpPost; 13 import org.apache.http.client.utils.URIBuilder; 14 import org.apache.http.entity.ContentType; 15 import org.apache.http.entity.StringEntity; 16 import org.apache.http.impl.client.CloseableHttpClient; 17 import org.apache.http.impl.client.HttpClients; 18 import org.apache.http.message.BasicNameValuePair; 19 import org.apache.http.util.EntityUtils; 20  21 public class HttpClientUtil { 22  23     public static String doGet(String url, Map
param) { 24 // 创建Httpclient对象 25 CloseableHttpClient httpclient = HttpClients.createDefault(); 26 String resultString = ""; 27 CloseableHttpResponse response = null; 28 try { 29 // 创建uri 30 URIBuilder builder = new URIBuilder(url); 31 if (param != null) { 32 for (String key : param.keySet()) { 33 builder.addParameter(key, param.get(key)); 34 } 35 } 36 URI uri = builder.build(); 37 38 // 创建http GET请求 39 HttpGet httpGet = new HttpGet(uri); 40 41 // 执行请求 42 response = httpclient.execute(httpGet); 43 // 判断返回状态是否为200 44 if (response.getStatusLine().getStatusCode() == 200) { 45 resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); 46 } 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } finally { 50 try { 51 if (response != null) { 52 response.close(); 53 } 54 httpclient.close(); 55 } catch (IOException e) { 56 e.printStackTrace(); 57 } 58 } 59 return resultString; 60 } 61 62 public static String doGet(String url) { 63 return doGet(url, null); 64 } 65 66 public static String doPost(String url, Map
param) { 67 // 创建Httpclient对象 68 CloseableHttpClient httpClient = HttpClients.createDefault(); 69 CloseableHttpResponse response = null; 70 String resultString = ""; 71 try { 72 // 创建Http Post请求 73 HttpPost httpPost = new HttpPost(url); 74 // 创建参数列表 75 if (param != null) { 76 List
paramList = new ArrayList<>(); 77 for (String key : param.keySet()) { 78 paramList.add(new BasicNameValuePair(key, param.get(key))); 79 } 80 // 模拟表单 81 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8"); 82 httpPost.setEntity(entity); 83 } 84 // 执行http请求 85 response = httpClient.execute(httpPost); 86 resultString = EntityUtils.toString(response.getEntity(), "utf-8"); 87 } catch (Exception e) { 88 e.printStackTrace(); 89 } finally { 90 try { 91 response.close(); 92 } catch (IOException e) { 93 e.printStackTrace(); 94 } 95 } 96 return resultString; 97 } 98 99 public static String doPost(String url) {100 return doPost(url, null);101 }102 103 /**104 * 请求的参数类型为json105 * @param url106 * @param json107 * @return108 * {username:"",pass:""}109 */110 public static String doPostJson(String url, String json) {111 // 创建Httpclient对象112 CloseableHttpClient httpClient = HttpClients.createDefault();113 CloseableHttpResponse response = null;114 String resultString = "";115 try {116 // 创建Http Post请求117 HttpPost httpPost = new HttpPost(url);118 // 创建请求内容119 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);120 httpPost.setEntity(entity);121 // 执行http请求122 response = httpClient.execute(httpPost);123 resultString = EntityUtils.toString(response.getEntity(), "utf-8");124 } catch (Exception e) {125 e.printStackTrace();126 } finally {127 try {128 response.close();129 } catch (IOException e) {130 e.printStackTrace();131 }132 }133 return resultString;134 }135 }

 

转载于:https://www.cnblogs.com/bignew/p/6715671.html

你可能感兴趣的文章
mysql-binlog日志恢复数据库
查看>>
python之使用单元测试框架unittest执行自动化测试
查看>>
java反射学习笔记
查看>>
day10-多进程的基本语法
查看>>
凡客和锤子
查看>>
设计模式(5)--单例模式
查看>>
VS2015 RTM与ASP.NET 5 RC1之坑
查看>>
@RequestMapping的Ant风格URL
查看>>
pitch yaw roll是什么
查看>>
python生成器 Generator
查看>>
Daily scrum[2013.12.09]
查看>>
mysql 切换数据库方案
查看>>
深浅copy
查看>>
网络osi
查看>>
WINREG.H 编译出错
查看>>
Detours的使用准备
查看>>
xfs 文件系统损坏修复 fscheck
查看>>
Hibernate之一级缓存
查看>>
Python基础之定义有默认参数的函数
查看>>
443. String Compression - Easy
查看>>