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