httpclient4 实现http协议post、get类型接口调用

package RuleEngine.comme;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
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.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.json.JSONTokener;

public class HttpRequest
{
private static Logger log = Logger.getLogger(HttpRequest.class);

public JSONObject dopost(String url, JSONObject jsonobject)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(url);
JSONObject response = null;
try
{
StringEntity sEntity = new StringEntity(jsonobject.toString());
sEntity.setContentEncoding("UTF-8");
sEntity.setContentType("application/json");
httpost.setEntity(sEntity);
HttpResponse httpResponse = httpclient.execute(httpost);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
response = new JSONObject(new JSONTokener(new InputStreamReader(httpResponse.getEntity().getContent())));
log.info(response);
}
else
{
log.info(Integer.valueOf(httpResponse.getStatusLine().getStatusCode()));
}
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return response;
}

public JSONObject dopost(String url, JSONObject jsonobject, Map<String, Object> params)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = null;
JSONObject response = null;
Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
try
{
if (params.size() == 0) {
return response;
}
if (params.size() == 1)
{
Map.Entry<String, Object> entry = (Map.Entry)it.next();
url = url + "?" + (String)entry.getKey() + "=" + entry.getValue().toString();
httpost = new HttpPost(url);
}
else
{
url = url + "?";
while (it.hasNext())
{
Map.Entry<String, Object> entry = (Map.Entry)it.next();
url = url + (String)entry.getKey() + "=" + entry.getValue().toString() + "&";
}
url = url.substring(0, new StringBuffer(url).length() - 1);
httpost = new HttpPost(url);
}
}
catch (ParseException e)
{
e.printStackTrace();
}
try
{
StringEntity sEntity = new StringEntity(jsonobject.toString());
sEntity.setContentEncoding("UTF-8");
sEntity.setContentType("application/json");
httpost.setEntity(sEntity);
HttpResponse httpResponse = httpclient.execute(httpost);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
response = new JSONObject(new JSONTokener(new InputStreamReader(httpResponse.getEntity().getContent())));
log.info(response);
}
else
{
log.info(Integer.valueOf(httpResponse.getStatusLine().getStatusCode()));
}
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return response;
}

public JSONObject dopost(String url, Map<String, Object> params)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = null;
JSONObject response = null;
Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
try
{
if (params.size() == 0) {
return response;
}
if (params.size() == 1)
{
Map.Entry<String, Object> entry = (Map.Entry)it.next();
url = url + "?" + (String)entry.getKey() + "=" + entry.getValue().toString();
httpost = new HttpPost(url);
}
else
{
url = url + "?";
while (it.hasNext())
{
Map.Entry<String, Object> entry = (Map.Entry)it.next();
url = url + (String)entry.getKey() + "=" + entry.getValue().toString() + "&";
}
url = url.substring(0, new StringBuffer(url).length() - 1);
httpost = new HttpPost(url);
}
}
catch (ParseException e)
{
e.printStackTrace();
}
try
{
StringEntity sEntity = new StringEntity("");
sEntity.setContentEncoding("UTF-8");
sEntity.setContentType("application/json");
httpost.setEntity(sEntity);
HttpResponse httpResponse = httpclient.execute(httpost);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
response = new JSONObject(new JSONTokener(new InputStreamReader(httpResponse.getEntity().getContent())));
log.info(response);
}
else
{
log.info(Integer.valueOf(httpResponse.getStatusLine().getStatusCode()));
}
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return response;
}

public String doget(String url, Map<String, Object> params)
{
CloseableHttpClient httpClient = HttpClients.createDefault();
Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
HttpResponse response = null;
HttpGet httpGet = null;
String json = null;
try
{
if (params.size() == 0)
{
httpGet = new HttpGet(url);
}
else if (params.size() == 1)
{
Map.Entry<String, Object> entry = (Entry<String, Object>)it.next();
url = url + "?" + (String)entry.getKey() + "=" + entry.getValue().toString();
httpGet = new HttpGet(url);
}
else
{
url = url + "?";
while (it.hasNext())
{
Map.Entry<String, Object> entry = (Entry<String, Object>)it.next();
url = url + (String)entry.getKey() + "=" + entry.getValue().toString() + "&";
}
url = url.substring(0, new StringBuffer(url).length() - 1);
httpGet = new HttpGet(url);
}
log.info(httpGet.getURI());
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null)
{
json=EntityUtils.toString(response.getEntity(), "utf-8");
}
}
catch (ParseException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return json.substring(4, json.length());
}

public static void main(String[] args) {}
}

猜你喜欢

转载自www.cnblogs.com/shuyichao/p/10384728.html