項目中會遇到需要發送請求獲取不同服務器中的資源,此時不能使用轉發或者重定向,而使用httpclient可以實現。下面介紹httpclient中的get請求和post請求:
GET方法:
public static String doGet() {
String result = null;
//請求地址
String url = "";
//獲取請求參數
List<NameValuePair> parame = new ArrayList<NameValuePair>();
parame.add(new BasicNameValuePair("參數名", 參數值);
// 獲取httpclient
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String parameStr = null;
try {
parameStr = EntityUtils.toString(new UrlEncodedFormEntity(parame));
//拼接參數
StringBuffer sb = new StringBuffer();
sb.append(url);
sb.append("?");
sb.append(parameStr);
//創建get請求
HttpGet httpGet = new HttpGet(sb.toString());
// 設置請求和傳輸超時時間
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(2000).setConnectTimeout(2000).build();
httpGet.setConfig(requestConfig);
// 提交參數發送請求
response = httpclient.execute(httpGet);
// 得到響應信息
int statusCode = response.getStatusLine().getStatusCode();
// 判斷響應信息是否正確
if (statusCode != HttpStatus.SC_OK) {
// 終止並拋出異常
httpGet.abort();
throw new RuntimeException("HttpClient,error status code :" + statusCode);
}
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
EntityUtils.consume(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//關閉所有資源連接
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result.toString();
}
POST方法:
public static String doPost(){
String result = null;
//請求地址
String url = "";
List<NameValuePair> parameForToken = new ArrayList<NameValuePair>();
parameForToken.add("參數名", 參數值);
// 獲取httpclient
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
//創建post請求
HttpPost httpPost = new HttpPost(url);
// 設置請求和傳輸超時時間
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(2000).setConnectTimeout(2000).build();
httpPost.setConfig(requestConfig);
// 提交參數發送請求
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(parame);
httpPost.setEntity(urlEncodedFormEntity);
response = httpclient.execute(httpPost);
// 得到響應信息
int statusCode = response.getStatusLine().getStatusCode();
// 判斷響應信息是否正確
if (statusCode != HttpStatus.SC_OK) {
// 終止並拋出異常
httpPost.abort();
throw new RuntimeException("HttpClient,error status code :" + statusCode);
}
HttpEntity entity = response.getEntity();
if (entity != null) {
//result = EntityUtils.toString(entity);//不進行編碼設置
result = EntityUtils.toString(entity, "UTF-8");
}
EntityUtils.consume(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//關閉所有資源連接
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
方法中主要用到的jar包: