作用
- 后端向后端发请求
- 可以让后端跨内外网
比如:一个前端项目a,一个后端项目b,一个后端项目c
a,b,c都处于内网,现在把a,b做一个外网代理,c不做
此时a,b都已经不能访问c了,然而,如果b用httpcliet发请求,它就可以访问到c了
pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!-- JSONObject对象依赖的jar包 开始 -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier>
<!-- jdk版本 -->
</dependency>
<!-- Json依赖架包下载结束 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
工具类代码
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@Component
public class HttpUtils {
public String doGetJson(String url, String token) throws Exception, IOException {
//初始化httpClient
DefaultHttpClient client = new DefaultHttpClient();
//用Get方式进行提交
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("X-AUTH-TOKEN", token);
//发送请求
HttpResponse response = client.execute(httpGet);
//获取数据
HttpEntity entity = response.getEntity();
//格式转换
String result = null;
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
//释放链接
httpGet.releaseConnection();
return result;
}
public JSONObject doGetJson(String url) throws Exception, IOException {
JSONObject jsonObject=null;
//初始化httpClient
DefaultHttpClient client = new DefaultHttpClient();
//用Get方式进行提交
HttpGet httpGet = new HttpGet(url);
//发送请求
HttpResponse response = client.execute(httpGet);
//获取数据
HttpEntity entity = response.getEntity();
//格式转换
String result = null;
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
jsonObject=JSONObject.fromObject(result);
}
//释放链接
httpGet.releaseConnection();
return jsonObject;
}
public String doPostJson(String url) throws Exception, IOException {
//初始化httpClient
DefaultHttpClient client = new DefaultHttpClient();
//用Get方式进行提交
HttpPost httpPost = new HttpPost(url);
//发送请求
HttpResponse response = client.execute(httpPost);
//获取数据
HttpEntity entity = response.getEntity();
//格式转换
String result = null;
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
//释放链接
httpPost.releaseConnection();
return result;
}
public String doPostJson(String url, String token) throws Exception, IOException {
//初始化httpClient
DefaultHttpClient client = new DefaultHttpClient();
//用Get方式进行提交
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("X-AUTH-TOKEN", token);
//发送请求
HttpResponse response = client.execute(httpPost);
//获取数据
HttpEntity entity = response.getEntity();
//格式转换
String result = null;
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
//释放链接
httpPost.releaseConnection();
return result;
}
public String doPostJson(String url,JSONObject param, String token) throws Exception, IOException {
//初始化httpClient
DefaultHttpClient client = new DefaultHttpClient();
//用Get方式进行提交
StringEntity stringEntity=new StringEntity(param.toString(), StandardCharsets.UTF_8);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(stringEntity);
httpPost.setHeader("X-AUTH-TOKEN", token);
//设置请求头
httpPost.setHeader("Content-Type","application/json");
httpPost.setHeader("Charset","UTF-8");
//发送请求
HttpResponse response = client.execute(httpPost);
//获取数据
HttpEntity entity = response.getEntity();
//格式转换
String result = null;
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
//释放链接
httpPost.releaseConnection();
return result;
}
}