我的HttpClients工具
1 import java.io.IOException; 2 3 import javax.ws.rs.core.MediaType; 4 5 import org.apache.commons.httpclient.HttpClient; 6 import org.apache.commons.httpclient.HttpException; 7 import org.apache.commons.httpclient.HttpStatus; 8 import org.apache.commons.httpclient.methods.GetMethod; 9 import org.apache.commons.httpclient.methods.PostMethod; 10 import org.apache.commons.httpclient.methods.PutMethod; 11 import org.apache.commons.httpclient.methods.RequestEntity; 12 import org.apache.commons.httpclient.methods.StringRequestEntity; 13 14 /** 15 * 16 * @author Administrator 17 * 18 */ 19 public class RestClientUtils { 20 21 /** 22 * 发送POST请求 23 * @param url 24 * @param object 25 */ 26 public static String post(String uri, Object content) { 27 28 String sendContent = content.toString(); 29 String result = null; 30 PostMethod postMethod = null; 31 HttpClient httpClient = null; 32 try { 33 postMethod = new PostMethod(uri); 34 httpClient = new HttpClient(); 35 RequestEntity entity = new StringRequestEntity(sendContent, 36 MediaType.APPLICATION_JSON + ";charset=UTF-8", null);//解决中文乱码问题的方法 37 postMethod.addRequestHeader("Accept", MediaType.APPLICATION_JSON 38 + ";charset=UTF-8"); 39 postMethod.setRequestEntity(entity); 40 int statusCode = httpClient.executeMethod(postMethod); 41 //如果相应成功 42 if (statusCode != HttpStatus.SC_OK) { 43 System.err.println(" Method failed: " 44 + postMethod.getStatusLine()); 45 return "failed"; 46 } 47 byte[] responseBody = postMethod.getResponseBody(); 48 result = new String(responseBody, "utf-8"); 49 System.err.println("===================发送POST格式数据请求=================="); 50 System.err.println("返回状态:"+statusCode); 51 System.err.println("返回结果:"); 52 System.err.println(result); 53 System.err.println("=============================================="); 54 } catch (HttpException e) { 55 e.printStackTrace(); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } finally { 59 // 释放连接 60 if (postMethod != null) { 61 postMethod.releaseConnection(); 62 } 63 } 64 return result; 65 66 } 67 68 /** 69 * 发送GET请求 70 * @return 71 */ 72 public static String get(String uri) { 73 HttpClient httpclient = new HttpClient(); 74 GetMethod getMethod = new GetMethod(uri); 75 String result = ""; 76 int statusCode = 0; 77 try { 78 statusCode = httpclient.executeMethod(getMethod); 79 result = getMethod.getResponseBodyAsString(); 80 System.err.println("===================发送GET请求=================="); 81 System.err.println("返回状态:"+statusCode); 82 System.err.println("返回结果:"); 83 System.err.println(result); 84 System.err.println("=============================================="); 85 } catch (IOException e) { 86 // TODO Auto-generated catch block 87 e.printStackTrace(); 88 } finally { 89 // 释放连接 90 if (getMethod != null) { 91 getMethod.releaseConnection(); 92 } 93 } 94 95 return result; 96 } 97 98 /** 99 * 发送PUT请求 100 * @param uri 101 * @param content 102 * @return 103 */ 104 public static String put(String uri, Object content) { 105 String sendContent = content.toString(); 106 HttpClient httpclient = new HttpClient(); 107 PutMethod putMethod = new PutMethod(uri); 108 String result = ""; 109 try { 110 111 RequestEntity entity = new StringRequestEntity(sendContent, 112 MediaType.APPLICATION_JSON + ";charset=UTF-8", null);//解决中文乱码问题的方法 113 putMethod.addRequestHeader("Accept", MediaType.APPLICATION_JSON 114 + ";charset=UTF-8"); 115 putMethod.setRequestEntity(entity); 116 int statusCode = httpclient.executeMethod(putMethod); 117 byte[] responseBody = putMethod.getResponseBody(); 118 result = new String(responseBody, "utf-8"); 119 System.err.println("===================发送PUT请求=================="); 120 System.err.println("返回状态:"+statusCode); 121 System.err.println("返回结果:"); 122 System.err.println(result); 123 System.err.println("=============================================="); 124 } catch (Exception e) { 125 e.printStackTrace(); 126 } finally { 127 // 释放连接 128 if (null != putMethod) { 129 putMethod.releaseConnection(); 130 } 131 } 132 return result; 133 } 134 }