我的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 ...
随机推荐
- const char*, char const* and char *const 分类: C/C++ OpenCV 2014-11-08 18:10 114人阅读 评论(0) 收藏
const char*, char const*, char*const的区别问题几乎是C++面试中每次都会有的题目. 事实上这个概念谁都有只是三种声明方式非常相似很容易记混. Bjarne在他的 ...
- MySQL Replication主从复制
MySQL Replication:NySQL复制,MySQL的复制默认为异步工作模式 mysql的复制功能是mysql内置的,装上它之后就具备了这个功能,而mysql复制是mysql实现大规模高 ...
- android 14 进度条和拖动条
进度条: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:l ...
- jQuery日期联动插件
此版本为网上的日期联动插件修改版,加入了修改后事件 /* * jQuery Date Selector Plugin * 日期联动选择插件 * * Demo: $("#calendar&qu ...
- warning:This application is modifying the autolayout engine from a background thread
警告提示:This application is modifying the autolayout engine from a background thread, which can lead to ...
- hadoop集群环境搭建之zookeeper集群的安装部署
关于hadoop集群搭建有一些准备工作要做,具体请参照hadoop集群环境搭建准备工作 (我成功的按照这个步骤部署成功了,经实际验证,该方法可行) 一.安装zookeeper 1 将zookeeper ...
- Matlab使用xlsread, xlswrite函数导致excel进程无法终止的问题
系统版本:Win7 64位 Matlab版本:R2015b 问题描述:使用excel的操作函数,比如xlsread,xlswrite,导致excel进程无法终止,任务管理器中仍残留excel进程,打开 ...
- Java开发工程师必会做试题
一.单选题 (共19道题,每题5分) 1.下面有关java的一些细节问题,描述错误的是? A.构造方法不需要同步化 B.一个子类不可以覆盖掉父类的同步方法 C.定义在接口中的方法默认是publ ...
- VS2015 Cordova Ionic移动开发(二)
一.创建空白Cordova应用 打开VS,选择[新建项目],选择其它语言JavaScript或者TypeScript,语言的话就按个人喜好,喜欢JS就用JS,喜欢TS就用TS,推荐使用TS书写,代码结 ...
- C++ 语法规则
C++ 中的布尔类型:布尔类型只占用一个bit ,但是如果连续定义多个布尔类型时,编译器可能会多个布尔类型定义在一起.true 编译器用1来表示.false 编译器用0来表示. 将一个其他类型的数 ...