用HttpClient发送get,post请求的工具类
来源:互联网 发布:vissim仿真软件 编辑:程序博客网 时间:2024/06/07 11:15
public class HttpRequestUtils { private static Logger log = LoggerFactory.getLogger(HttpRequestUtils.class); //返回对象的json public static JSONObject httpGet(String url){ JSONObject jsonResult = null; try { DefaultHttpClient client =new DefaultHttpClient(); //发送get请求 HttpGet request = new HttpGet(url); HttpResponse response=client.execute(request); //请求发送并得到响应 if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){ //读取服务器返回的json字符串数据 HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(is,"UTF-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null){ buffer.append(line); } jsonResult=JSONObject.fromObject(buffer.toString()); } }catch (Exception e){ log.info("get请求失败:"+ url); } return jsonResult; } //返回数组的json public static JSONArray httpGetArray(String url){ JSONArray jsonResult = null; try { DefaultHttpClient client =new DefaultHttpClient(); //发送get请求 HttpGet request = new HttpGet(url); HttpResponse response=client.execute(request); //请求发送并得到响应 if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){ //读取服务器返回的json字符串数据 HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(is,"UTF-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null){ buffer.append(line); } jsonResult= JSON.parseArray(buffer.toString()); } }catch (Exception e){ log.info("get请求失败:"+ url); } return jsonResult; } //发送post请求 public static JSONObject httpPost(String url,Map<String,String>params) { JSONObject jsonResult = null; try { DefaultHttpClient client = new DefaultHttpClient(); //发送post请求 HttpPost httpPost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<>(); if (params != null) { Iterator<String> keys = params.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); nvps.add(new BasicNameValuePair(key, params.get(key))); } //URL格式编码 httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); } HttpResponse response = client.execute(httpPost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } jsonResult = JSONObject.fromObject(buffer.toString()); } } catch (Exception e) { log.info("get请求失败:"+ url); } return jsonResult; }}
阅读全文
1 0
- 用HttpClient发送get,post请求的工具类
- HttpClient发送get和post工具类
- java发送GET/POST请求工具类
- java发送http的get、post请求工具类
- [Socket]利用Android下的HttpClient发送GET && POST请求
- android内置的HttpClient发送get、post请求用法
- java使用HttpClient模拟发送http的get、post请求
- android HttpClient get请求与post请求工具类
- java httpclient发送post get 请求
- HttpClient发送get和post请求
- 使用Apache HttpClient发送get、post请求
- 利用HttpClient发送Get和Post请求
- 利用HttpClient发送Get和Post请求
- 利用HttpClient发送Get和Post请求
- HttpClient发送Get、Post请求简单实践
- 利用HttpClient发送Get和Post请求
- HttpClient发送Get和Post请求
- 使用HttpClient发送GET和POST请求
- 什么是动态页面与静态页面
- SSH与SSM比较与选型
- 蓝桥杯模拟题 计蒜客 转载自AC_Coding
- 二进制求和
- 代理模式之静态代理与动态代理
- 用HttpClient发送get,post请求的工具类
- c&c++动态内存管理
- 存储过程使用sp_xml_preparedocument处理XML文档的方法
- Activity的生命周期
- 排序算法之快速排序
- python中np.genfromtxt一个简单例子
- Java异常之自定义异常
- Linux 使用wget 命令下载JDK的方法
- 设计模式之装饰模式