分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

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

发布时间:2023-09-06 02:33责任编辑:彭小芳关键词:http

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) {}
}

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

原文地址:https://www.cnblogs.com/shuyichao/p/10384728.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved