前言:因公司项目的需求,需要调用外部系统接口。参考了论坛上的各个文章。决定自己写个笔记,以便更好的学习,记录。
废话不多说:
需要的jar包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
代码
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by hshibao01 on 2016/12/28.
*/
public class HttpClientUtils {
private String charsert = "UTF-8";
private List params = new ArrayList();
private String url;
private HttpClient httpClient;
public HttpClientUtils setCharsert(String charsert) {
this.charsert = charsert;
return this;
}
public HttpClientUtils setParam(String name, String value) {
this.params.add(new BasicNameValuePair(name, value));
return this;
}
public HttpClientUtils setParam(Map<String, String> params) {
for (Map.Entry<String, String> entry : params.entrySet()) {
this.params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
return this;
}
public HttpClientUtils() {
httpClient = HttpClientBuilder.create().build();
}
public HttpClient getHttpClient() {
return httpClient;
}
public HttpClientUtils setUrl(String url) {
this.url = url;
return this;
}
public String post() {
if (params != null) {
url = url + "?" + URLEncodedUtils.format(this.params, this.charsert);
System.out.println(url);
}
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse httpResponse = this.getHttpClient().execute(httpPost);
//连接成功
if (200 == httpResponse.getStatusLine().getStatusCode()) {
HttpEntity httpEntity = httpResponse.getEntity();
return convertStreamToString(httpEntity.getContent(), this.charsert);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String get() {
if (params != null) {
url = url + "?" + URLEncodedUtils.format(this.params, this.charsert);
System.out.println(url);
}
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse httpResponse = this.getHttpClient().execute(httpGet);
//连接成功
if (200 == httpResponse.getStatusLine().getStatusCode()) {
HttpEntity httpEntity = httpResponse.getEntity();
return convertStreamToString(httpEntity.getContent(), this.charsert);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String convertStreamToString(InputStream is, String charset) {
StringBuilder sb1 = new StringBuilder();
byte[] bytes = new byte[4096];
int size = 0;
try {
while ((size = is.read(bytes)) > 0) {
String str = new String(bytes, 0, size, charset);
sb1.append(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb1.toString();
}
public static void main(String[] args) {
HttpClientUtils httpClientUtils = new HttpClientUtils();
String retu = httpClientUtils.setUrl("http://192.168.10.65:7979/erp/t")
.setParam("param", "中文")
.setCharsert("UTF-8").get();
System.out.println(retu);
}
}
由于项目只是简单的涉及到get post请求, 暂时未完善, 后期会根据情况进行修改,若有不足之处望谅解。