前言:因公司項(xiàng)目的需求,需要調(diào)用外部系統(tǒng)接口恩商。參考了論壇上的各個(gè)文章变逃。決定自己寫個(gè)筆記,以便更好的學(xué)習(xí)怠堪,記錄揽乱。
廢話不多說(shuō):
需要的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);
}
}
由于項(xiàng)目只是簡(jiǎn)單的涉及到get post請(qǐng)求名眉, 暫時(shí)未完善, 后期會(huì)根據(jù)情況進(jìn)行修改凰棉,若有不足之處望諒解损拢。