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;
}
}
05-11 20:22