我的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 ...
随机推荐
- OpenCV与QT联合编译 分类: Eye_Detection ZedBoard OpenCV shell ubuntu 2014-11-08 18:54 143人阅读 评论(0) 收藏
问题1:首先参考rainysky的博客,发现qmake时发生找不到目录,文件的错误,又找不到 qmake.conf 文件的写法.所以开始按照网上的程序修改 XXX.pro 文件. 问题2:使用QT C ...
- Linq中Union与Contact方法用法对比
文章一开始,我们来看看下面这个简单的实例. 代码片段1: int[] ints1 = { 2, 4, 9, 3, 0, 5, 1, 7 }; int[] ints2 = { 1, 3, 6, 4, 4 ...
- [RxJS] Transformation operator: map and mapTo
We made our first operator called multiplyBy, which looks a bit useful, but in practice we don't nee ...
- 常用Content-type汇总
Content-Type,内容类型,用于定义网络文件的类型和网页的编码,决定浏览器将以什么形式.什么编码读取这个文件.这里汇总一下常用的,所有资料来源于网络,未经测试: 文件后缀 处理方式 .* ...
- 亲测linux 上安装php
亲测安装php1.tar zvxf php-5.3.8.tar.gz 2.cd php-5.3.83../configure \ --prefix=/usr/local/php \--with-mys ...
- JAVA 安装与配置
JDK是整个java的核心,包括java的运行环境.java工具和java基础类库. 一.安装JDK 获得JDK,登录oracle网站http://www.oracle.com/technetwork ...
- mysql - 初探
1,查询所有数据库名称: show databases; 2,查询所有表: use database_name; show tables; 3,查询表中的所有字段: desc table_name;
- 【转】iOS开发之各种动画各种页面切面效果
原文: http://www.cnblogs.com/ludashi/p/4160208.html?utm_source=tuicool 因工作原因,有段时间没发表博客了,今天就发表篇博客给大家带来一 ...
- WPF里面的常用笔刷
程序运行效果 <Window x:Class="This_brush.MainWindow" xmlns="http://schemas.microsoft.com ...
- C#入门经典(第五版)学习笔记(四)
---------------集合.比较和转换--------------- C#中的数组是作为System.Array类的实例实现的,它们是集合类(Collection Classes)中的一种类型 ...