发送rest请求获取返回值的方法

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();
			}
		}
		
	}	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值