Java调用HTTP接口

Java调用rest接口

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * 
 * @author 
 *
 */
public class InterfaceUtil {
	private static final Logger LOGGER = Logger.getLogger(InterfaceUtil.class);
	/**
	 * 获取url可用性
	 * @param url URL
	 * @param timeout 测试url可用性的超时时间(毫秒),该值设置的过短的话会照成实际可用的url也返回不可用状态,参考值为5000毫秒
	 * @return 当url调用异常时返回-1,否则返回http标准响应状态值
	 * @throws MalformedURLException 当传入的url本身格式不正确时抛出该异常
	 */
	public static int getUrlResponseCode(String url, int timeout) throws MalformedURLException {
		URL urlO = new URL(url);
		HttpURLConnection urlConnection;
		try {
			urlConnection = (HttpURLConnection) urlO.openConnection();
			urlConnection.setConnectTimeout(timeout);
			int responseCode = urlConnection.getResponseCode();
			return responseCode;
		} catch (IOException e) {
			LOGGER.error("URL不可用", e);
			return -1;
		}
	}
	/**
	 * 调用HTTP接口
	 * @param url URL
	 * @param charset 字符编码集
	 * @return 接口返回的字节集合
	 * @throws IOException
	 */
	public static byte[] callHttp(String url) throws IOException {
		return callHttp(new URL(url));
	}
	public static String callHttp(String url, String charsetName) throws IOException {
		return new String(callHttp(url), charset);
	}
	public static String callHttp(URL url, String charset) throws IOException {
		return new String(callHttp(url), charset);
	}
	
	public static String callHttpByPost(String url,byte[] content, String charset) throws IOException {
		return new String(callHttpByPost(new URL(url),content),charset);
	}

	
	/**
	 * GET方式调用HTTP接口
	 * @param url URL
	 * @return 接口返回的字节集合
	 * @throws IOException
	 */
	public static byte[] callHttp(URL url) throws IOException {
		URLConnection urlConnection = url.openConnection();
		InputStream is = null;
		byte[] b = null;
		try {
			is = urlConnection.getInputStream();
			//分次获取返回信息 
			List<byte[]> byteList = new ArrayList<byte[]>();
			byte[] bt = new byte[1024];
			int readNum = -1;
			int totalNum = 0;
			while((readNum = is.read(bt)) >= 0) {
				if (readNum > 0) {
					byte[] bb = new byte[readNum];
					System.arraycopy(bt, 0, bb, 0, readNum);
					byteList.add(bb);
					totalNum += readNum;
				}
			}
			// 将分次获取的返回信息合并
			b = new byte[totalNum];
			int indexT = 0;
			for (byte[] btt: byteList) {
				System.arraycopy(btt, 0, b, indexT, btt.length);
				indexT += btt.length;
			}
		} finally {
			if (is != null) {
				is.close();
			}
		}
		return b;
	}
	
	/**
	 * POST方式调用HTTP接口
	 * @param url URL
	 * @param content 请求参数
	 * @return 接口返回的字节集合
	 * @throws IOException
	 */
	public static byte[] callHttpByPost(URL url,byte[] content) throws IOException {
		URLConnection urlConnection = url.openConnection();
		InputStream is = null;
		OutputStream out = null;
		byte[] b = null;
		try {
			//post提交
			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			
			out = urlConnection.getOutputStream();
			out.write(content);	//写入请求参数
			out.flush();
			
			is = urlConnection.getInputStream();
			// 分次获取返回信息
			List<byte[]> byteList = new ArrayList<byte[]>();
			byte[] bt = new byte[1024];
			int readNum = -1;
			int totalNum = 0;
			while((readNum = is.read(bt)) >= 0) {
				if (readNum > 0) {
					byte[] bb = new byte[readNum];
					System.arraycopy(bt, 0, bb, 0, readNum);
					byteList.add(bb);
					totalNum += readNum;
				}
			}
			// 将分次获取的返回信息合并
			b = new byte[totalNum];
			int indexT = 0;
			for (byte[] btt: byteList) {
				System.arraycopy(btt, 0, b, indexT, btt.length);
				indexT += btt.length;
			}
		} finally {
			if (is != null) {
				is.close();
			}
			if(out != null) {
				out.close();
			}
		}
		return b;
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值