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工具的更多相关文章

  1. HTTP Client工具类

    HTTP Client工具类: import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.ht ...

  2. HttpClient4.5 SSL访问工具类

    要从网上找一个HttpClient SSL访问工具类太难了,原因是HttpClient版本太多了,稍有差别就不能用,最后笔者干脆自己封装了一个访问HTTPS并绕过证书工具类. 主要是基于新版本Http ...

  3. 使用java开源工具httpClient及jsoup抓取解析网页数据

    今天做项目的时候遇到这样一个需求,需要在网页上展示今日黄历信息,数据格式如下 公历时间:2016年04月11日 星期一 农历时间:猴年三月初五 天干地支:丙申年 壬辰月 癸亥日 宜:求子 祈福 开光 ...

  4. HttpClient 工具

    什么是httpclient HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 ja ...

  5. Android Studio 插件开发详解二:工具类

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78112856 本文出自[赵彦军的博客] 在插件开发过程中,我们按照开发一个正式的项 ...

  6. http和https工具类 (要注意httpclient版本号和log4j的版本号)

    1 工具类 package dd.com; import java.io.IOException; import java.security.cert.CertificateException; im ...

  7. 高德地图web端笔记;发送http请求的工具类

    1.查询所有电子围栏 package com.skjd.util; import java.io.BufferedReader; import java.io.InputStream; import ...

  8. HttpClientUtil 工具类

    /* * * * FileName: s.java * * Description:TODO(用一句话描述该文件做什么) * * Created: jiangzhanghong 2017年11月14日 ...

  9. 带SSL证书的httpclient 远程接口工具类

    package com.iups.wx.util; import java.io.IOException; import java.io.UnsupportedEncodingException; i ...

随机推荐

  1. python装饰实现线程同步

    import threading def tryfinally(finallyf):   u"returns a decorator that adds try/finally behavi ...

  2. hdu2660 Accepted Necklace (DFS)

    Problem Description I have N precious stones, and plan to use K of them to make a necklace for my mo ...

  3. IOS的工程目录结构和生命周期

    IOS的工程目录结构和生命周期 ·simple table文件夹:工程相关源代码和配置文件 BIDAppDelegate :    委托的声明和实现 BIDViewController:    视图控 ...

  4. 使用 Xcode 和 Android Studio 管理 iOS 和 Android 项目版本

    在移动应用开发和运营的过程中,版本管理是一个老生常谈的基础问题,一些版本的基本概念也常常会困扰我们的研发和运营人员.同时,手动管理软件版本,也常常会因为不小心导致后续的发布和更新问题. 这里,我准备了 ...

  5. android开发之调试技巧 分类: android 学习笔记 2015-07-18 21:30 140人阅读 评论(0) 收藏

    我们都知道,android的调试打了断点之后运行时要使用debug as->android application 但是这样的运行效率非常低,那么我们有没有快速的方法呢? 当然有. 我们打完断点 ...

  6. iOS UIKit:view

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  7. Android Studio安装及首次运行遇到的问题

    Android Studio,下载地址:http://developer.android.com/sdk/index.html.需要注意的是Android Studio需要JDK 1.7+才可以安装, ...

  8. 怎么让自己的java系统使用支付接口

    昨天花了好久的时间学习了支付接口的教,我看了前7集,就够用了,大家上网搜索一下传智播客在线支付还不错. 1.一开始有一个form表单 2.这个表单是他帮你写好的,有很多银行,银行的name都是特定的 ...

  9. java中的类集框架

    1.什么是类集框架 1.是一组类和接口 2.位于java.util包当中 3.主要用于用户存储和管理对象 4.主要分为三大类——集合.列表和映射 2.类集框架图 虚线框的表示接口,实线框的表示实现类 ...

  10. SDK Manager.exe 无法启动,一闪而过的解决办法

    删掉 C:\Windows\system32\ 下的 java.exe.javaw.exe.javaws.exe 即可解决.(转载)