我的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 ...
随机推荐
- Qt绘图之QGraphicsScene QGraphicsView QGraphicsItem详解
Graphics View提供了一个界面,它既可以管理大数量的定制2D graphical items,又可与它们交互,有一个view widget可以把这些项绘制出来,并支持旋转与缩放.这个柜架也包 ...
- GNU GRUB version 0.97 (630K lower /2053824K upper memory)
昨天把老板的IBM X61笔记本拿过来多系统,结果本以为很容易,直接ghost,结果悲剧发生啦,开机之后提示GNU GRUB version 0.97 (630K lower /2053824K up ...
- 模拟dispatch_once
dispatch_once dispatch_once可以保证一段代码只被执行一次,因此出现之后使用最多的场景就是实现单例.本文来模拟实现dispatch_once的功能. 模拟dispatch_ ...
- href与src的区别
src是source的缩写,指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在位置:在请求src资源时会将其指向的资源下载并应用到文档内,例如js脚本,img图片和frame等元素. href ...
- 自己做的demo--关于HashMap
package com.pb.collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map ...
- C#使用Process类调用外部程序(转)
在程序开发中,一个程序经常需要去调用其他的程序,C#中Process类正好提供了这样的功能.它提供对本地和远程进程的访问并使您能够启动和停止本地系统进程.一.启动进程实例 Process myProc ...
- NetAdvantage webdatagrid 控件的一些属性
属性: 1 behaviors 行为下的属性集合 Row Selectors 主要用于设置行选择样式与形为的集合 Enable 属性表示是否启用 Row Selectors下的属性设置 RowNumB ...
- android SDK 代理配置(东北大学)
启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口: 在『Andro ...
- 项目中常用SQL语句总结
1.项目中常常需要修改字段长度,但需要保留数据--增加业务受理 项目名称 字段长度alter table t_ywsl add aa varchar2(200);update t_ywsl set a ...
- java_设计模式_工厂模式_Factory Pattern(2016-08-04)
工厂模式主要是为创建对象提供了接口.工厂模式按照<Java与模式>中的提法分为三类: (1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory ...