package com.gzydt.license.util;
import java.io.File;
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.DeleteMethod;
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;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
/**
* 发送rest请求工具类
*
*
*/
public class RestClientUtil {
/**
* 发送post的创建请求
*
* @param uri
* @param sendContent
*/
public static byte[] sendPost(String uri, String sendContent) {
String result = null;
PostMethod postMethod = null;
HttpClient httpClient = null;
byte[] responseBody = 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) {
}
responseBody = postMethod.getResponseBody();
//result = new String(responseBody, "utf-8");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放连接
if (postMethod != null) {
postMethod.releaseConnection();
}
}
return responseBody;
}
/**
* 发送post的创建请求
*
* @param uri
* @param sendContent
*/
public static String sendPostF(String uri, String sendContent) {
String result = null;
PostMethod postMethod = null;
HttpClient httpClient = null;
byte[] responseBody = 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) {
}
responseBody = postMethod.getResponseBody();
result = new String(responseBody, "utf-8");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放连接
if (postMethod != null) {
postMethod.releaseConnection();
}
}
return result;
}
public static String sendGet(String uri) {
String result = null;
GetMethod getMethod = null;
HttpClient httpClient = null;
try {
getMethod = new GetMethod(uri);
httpClient = new HttpClient();
getMethod.addRequestHeader("Accept", MediaType.APPLICATION_JSON+ ";charset=UTF-8");
int statusCode = httpClient.executeMethod(getMethod);
// 如果相应成功
if (statusCode != HttpStatus.SC_OK) {
}
byte[] responseBody = getMethod.getResponseBody();
result = new String(responseBody, "utf-8");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放连接
if (getMethod != null) {
getMethod.releaseConnection();
}
}
return result;
}
public static String sendPut(String uri, String updateContent) {
String result = null;
PutMethod putMethod = null;
HttpClient httpClient = null;
try {
putMethod = new PutMethod(uri);
httpClient = new HttpClient();
RequestEntity entity = new StringRequestEntity(updateContent,
MediaType.APPLICATION_JSON + ";charset=UTF-8", null);
putMethod.addRequestHeader("Accept", MediaType.APPLICATION_JSON+ ";charset=UTF-8");
putMethod.setRequestEntity(entity);
int statusCode = httpClient.executeMethod(putMethod);
// 如果相应成功
if (statusCode != HttpStatus.SC_OK) {
return "error";
}
byte[] responseBody = putMethod.getResponseBody();
result = new String(responseBody, "utf-8");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放连接
if (putMethod != null) {
putMethod.releaseConnection();
}
}
return "success";
}
public static String sendDelete(String uri) {
String result = null;
DeleteMethod deleteMethod = null;
HttpClient httpClient = null;
try {
deleteMethod = new DeleteMethod(uri);
httpClient = new HttpClient();
deleteMethod.addRequestHeader("Accept", MediaType.APPLICATION_JSON+ ";charset=UTF-8");
int statusCode = httpClient.executeMethod(deleteMethod);
// 如果相应成功
if (statusCode != HttpStatus.SC_OK) {
}
byte[] responseBody = deleteMethod.getResponseBody();
result = new String(responseBody, "utf-8");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放连接
if (deleteMethod != null) {
deleteMethod.releaseConnection();
}
}
return result;
}
public static void sendUpload(String uri, File uploadFile, String uploadPath) {
PostMethod postMethod = null;
HttpClient httpClient = null;
try {
postMethod = new PostMethod(uri);
httpClient = new HttpClient();
StringPart part = new StringPart("path", uploadPath, "utf-8");
FilePart filePart = new FilePart(uploadFile.getName(), uploadFile);
postMethod.getParams().setContentCharset("utf-8");
RequestEntity entity = new MultipartRequestEntity(new Part[] {
part, filePart }, postMethod.getParams());
postMethod.setRequestEntity(entity);
int statusCode = httpClient.executeMethod(postMethod);
// 如果相应成功
if (statusCode != HttpStatus.SC_OK) {
}
byte[] responseBody = postMethod.getResponseBody();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放连接
if (postMethod != null) {
postMethod.releaseConnection();
}
}
}
}
发送rest请求获取返回值的方法
最新推荐文章于 2024-01-12 00:10:44 发布