我的HttpClients工具
- import java.io.IOException;
- import javax.ws.rs.core.MediaType;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpException;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.methods.PutMethod;
- import org.apache.commons.httpclient.methods.RequestEntity;
- import org.apache.commons.httpclient.methods.StringRequestEntity;
- /**
- *
- * @author Administrator
- *
- */
- public class RestClientUtils {
- /**
- * 发送POST请求
- * @param url
- * @param object
- */
- public static String post(String uri, Object content) {
- String sendContent = content.toString();
- String result = null;
- PostMethod postMethod = null;
- HttpClient httpClient = null;
- try {
- postMethod = new PostMethod(uri);
- httpClient = new HttpClient();
- RequestEntity entity = new StringRequestEntity(sendContent,
- MediaType.APPLICATION_JSON + ";charset=UTF-8", null);//解决中文乱码问题的方法
- postMethod.addRequestHeader("Accept", MediaType.APPLICATION_JSON
- + ";charset=UTF-8");
- postMethod.setRequestEntity(entity);
- int statusCode = httpClient.executeMethod(postMethod);
- //如果相应成功
- if (statusCode != HttpStatus.SC_OK) {
- System.err.println(" Method failed: "
- + postMethod.getStatusLine());
- return "failed";
- }
- byte[] responseBody = postMethod.getResponseBody();
- result = new String(responseBody, "utf-8");
- System.err.println("===================发送POST格式数据请求==================");
- System.err.println("返回状态:"+statusCode);
- System.err.println("返回结果:");
- System.err.println(result);
- System.err.println("==============================================");
- } catch (HttpException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- // 释放连接
- if (postMethod != null) {
- postMethod.releaseConnection();
- }
- }
- return result;
- }
- /**
- * 发送GET请求
- * @return
- */
- public static String get(String uri) {
- HttpClient httpclient = new HttpClient();
- GetMethod getMethod = new GetMethod(uri);
- String result = "";
- int statusCode = 0;
- try {
- statusCode = httpclient.executeMethod(getMethod);
- result = getMethod.getResponseBodyAsString();
- System.err.println("===================发送GET请求==================");
- System.err.println("返回状态:"+statusCode);
- System.err.println("返回结果:");
- System.err.println(result);
- System.err.println("==============================================");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- // 释放连接
- if (getMethod != null) {
- getMethod.releaseConnection();
- }
- }
- return result;
- }
- /**
- * 发送PUT请求
- * @param uri
- * @param content
- * @return
- */
- public static String put(String uri, Object content) {
- String sendContent = content.toString();
- HttpClient httpclient = new HttpClient();
- PutMethod putMethod = new PutMethod(uri);
- String result = "";
- try {
- RequestEntity entity = new StringRequestEntity(sendContent,
- MediaType.APPLICATION_JSON + ";charset=UTF-8", null);//解决中文乱码问题的方法
- putMethod.addRequestHeader("Accept", MediaType.APPLICATION_JSON
- + ";charset=UTF-8");
- putMethod.setRequestEntity(entity);
- int statusCode = httpclient.executeMethod(putMethod);
- byte[] responseBody = putMethod.getResponseBody();
- result = new String(responseBody, "utf-8");
- System.err.println("===================发送PUT请求==================");
- System.err.println("返回状态:"+statusCode);
- System.err.println("返回结果:");
- System.err.println(result);
- System.err.println("==============================================");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- // 释放连接
- if (null != putMethod) {
- putMethod.releaseConnection();
- }
- }
- return result;
- }
- }
我的HttpClients工具的更多相关文章
- HTTP Client工具类
HTTP Client工具类: import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.ht ...
- HttpClient4.5 SSL访问工具类
要从网上找一个HttpClient SSL访问工具类太难了,原因是HttpClient版本太多了,稍有差别就不能用,最后笔者干脆自己封装了一个访问HTTPS并绕过证书工具类. 主要是基于新版本Http ...
- 使用java开源工具httpClient及jsoup抓取解析网页数据
今天做项目的时候遇到这样一个需求,需要在网页上展示今日黄历信息,数据格式如下 公历时间:2016年04月11日 星期一 农历时间:猴年三月初五 天干地支:丙申年 壬辰月 癸亥日 宜:求子 祈福 开光 ...
- HttpClient 工具
什么是httpclient HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 ja ...
- Android Studio 插件开发详解二:工具类
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78112856 本文出自[赵彦军的博客] 在插件开发过程中,我们按照开发一个正式的项 ...
- http和https工具类 (要注意httpclient版本号和log4j的版本号)
1 工具类 package dd.com; import java.io.IOException; import java.security.cert.CertificateException; im ...
- 高德地图web端笔记;发送http请求的工具类
1.查询所有电子围栏 package com.skjd.util; import java.io.BufferedReader; import java.io.InputStream; import ...
- HttpClientUtil 工具类
/* * * * FileName: s.java * * Description:TODO(用一句话描述该文件做什么) * * Created: jiangzhanghong 2017年11月14日 ...
- 带SSL证书的httpclient 远程接口工具类
package com.iups.wx.util; import java.io.IOException; import java.io.UnsupportedEncodingException; i ...
随机推荐
- gitlab一键安装
参考 https://about.gitlab.com/downloads/
- 查询,创建,扩充表空间&&impdp--------表空间大全
周六晚上还在办公室导入数据. 按schemas导入成功的关键是:导入的环境和源数据环境里面的表空间大小,表空间名字,需要一模一样 (当然,表空间大小创建到和源数据环境里面ues_size大小就可以了) ...
- UITableView beginUpdate和endUpdate使用的前提
转载地址:http://blog.csdn.net/vieri_ch/article/details/46893023 UITableView有两个方法,用于单元格动画变化的方法,beginUpdat ...
- Android(java)学习笔记201:网络图片浏览器的实现(ANR)
1.我们在Android下,实现使用http协议进行网络通信,请求网络数据.这里是获取网络上的图片信息,让它可以显示在手机上: 但是我们这个手机连接网络是很费时间,如果我们在主线程(UI线程)中写这个 ...
- javascript 高级程序设计(三)-数据类型
ECMAScript 中所有类型的值都有与这两个Boolean值等价的值 数据类型 转换为true的值 转换为false的值 Boolean true false( String ...
- oracle的安装与plsql的环境配置
1,首先得有oracle的安装包和plsql的安装包,安装包地址可见百度云 http://pan.baidu.com/s/1miTqhmg 2.解压下来进入0817账套,找到set.exe文件,双击安 ...
- CakePHP之请求与响应对象
请求与响应对象 请求与响应对象在 CakePHP 2.0 是新增加的.在之前的版本中,这两个对象是由数组表示的,而相关的方法是分散在RequestHandlerComponent,Router,Dis ...
- 委托、 Lambda表达式和事件——事件
/* * 由SharpDevelop创建. * 用户: David Huang * 日期: 2015/7/31 * 时间: 14:21 */ using System; namespace 事件 { ...
- ratingBar抢焦点问题
ratingBar抢viewpager焦点问题: 1)写一个类继承ratingBar,让onTouchevent或者dispatchTouchEvent返回false 2)设置ratingBar的属性 ...
- requirejs+anjularjs+express框架
1.目录 2.首页login.html如下: <!DOCTYPE html><html> <head> <title>登录界面</title> ...