java 封装httpclient 的get 和post 请求

时间:2023-03-08 17:19:30
 import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair; public class HttpUtil { /**
* 以Post方法访问
* @param url 请求地址
* @param paramsMap 携带的参数
* @return String 返回结果
* @throws Exception
*/
@SuppressWarnings("deprecation")
public static String postMethod(String url, Map<String, Object> paramsMap){
String result = "SUCCESS";
try {
byte[] dataByte = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(setHttpParams(paramsMap), "UTF-8");
httpPost.setEntity(encodedFormEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if (!checkNetwork(httpResponse)) {
result = "LINK FAILURE!";
return result;
}
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
byte[] responseBytes = getData(httpEntity);
dataByte = responseBytes;
httpPost.abort();
}
result = bytesToString(dataByte);
} catch (Exception e) {
result = "FAILURE";
e.printStackTrace();
}
return result;
} /**
* 以Get方法访问
* @param url 请求地址
*/
public static String GETMethod(String url,Map<String, Object> paramsMap){
String result = "SUCCESS";
try{
byte[] dataByte = null;
HttpClient httpClient = new DefaultHttpClient();
//为GET请求链接构造参数
url = formatGetParameter(url,paramsMap);
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
if (!checkNetwork(httpResponse)) {
result = "LINK FAILURE!";
return result;
}
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
byte[] responseBytes = getData(httpEntity);
dataByte = responseBytes;
httpGet.abort();
}
result = bytesToString(dataByte);
}catch(Exception e){
result = "LINK FAILURE!";
e.printStackTrace();
}
return result;
} /**
* 以Put方法访问
* @param url 请求地址
*/
public static String PUTMethod(String url,Map<String, Object> paramsMap){
String result = "SUCCESS";
try {
byte[] dataByte = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(url);
UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(setHttpParams(paramsMap), "UTF-8");
httpPut.setEntity(encodedFormEntity);
HttpResponse httpResponse = httpClient.execute(httpPut);
if (!checkNetwork(httpResponse)) {
result = "LINK FAILURE!";
return result;
}
// 获取返回的数据
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
byte[] responseBytes = getData(httpEntity);
dataByte = responseBytes;
httpPut.abort();
}
result = bytesToString(dataByte);
} catch (Exception e) {
result = "LINK FAILURE!";
e.printStackTrace();
}
return result;
} /**
* 构造GET请求地址的参数拼接
* @param url
* @param paramsMap
* @return String
*/
private static String formatGetParameter(String url, Map<String, Object> paramsMap) {
if (paramsMap != null && !paramsMap.isEmpty()) {
if (url != null && url.length() > 0) {
if (!url.endsWith("?")) {
url = url + "?";
}
Set<Entry<String, Object>> entrySet = paramsMap.entrySet();
Iterator<Entry<String, Object>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();
if (entry != null) {
String key = entry.getKey();
String value = (String) entry.getValue();
url = url + key + "=" + value;
if (iterator.hasNext()) {
url = url + "&";
}
}
}
}
}
return url;
} /**
* 获取数据
* @param httpEntity
*/
private static byte[] getData(HttpEntity httpEntity) throws Exception{
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bufferedHttpEntity.writeTo(byteArrayOutputStream);
byte[] responseBytes = byteArrayOutputStream.toByteArray();
return responseBytes;
} /**
* 设置HttpPost请求参数
* @param paramsMap
* @return BasicHttpParams
*/
private static List<BasicNameValuePair> setHttpParams(Map<String, Object> paramsMap){
List<BasicNameValuePair> nameValuePairList = new ArrayList<BasicNameValuePair>();
if (paramsMap!=null && !paramsMap.isEmpty()) {
Set<Entry<String, Object>> set = paramsMap.entrySet();
Iterator<Entry<String, Object>> iterator = set.iterator();
while(iterator.hasNext()){
Entry<String, Object> entry = iterator.next();
BasicNameValuePair basicNameValuePair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
nameValuePairList.add(basicNameValuePair);
}
}
return nameValuePairList;
} /**
* 将字节数组转换成字符串
* @param bytes
*/
private static String bytesToString(byte[] bytes) throws UnsupportedEncodingException{
if (bytes!=null) {
String returnStr = new String(bytes,"utf-8");
return returnStr;
}
return null;
} /**
* 判断网络连接是否成功
*/
public static boolean checkNetwork(HttpResponse httpResponse){
if (httpResponse.getStatusLine().getStatusCode() == 200) {
return true;
}
return false;
}
}