com.alibaba.rocketmq.common.utils.HttpTinyClient Maven / Gradle / Ivy
package com.alibaba.rocketmq.common.utils;
import com.alibaba.rocketmq.common.MQVersion;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
/**
* HTTP 简易客户端
*
* @author manhong.yqd
*/
public class HttpTinyClient {
/**
* 发送GET请求。
*/
static public HttpResult httpGet(String url, List headers, List paramValues,
String encoding, long readTimeoutMs) throws IOException {
String encodedContent = encodingParams(paramValues, encoding);
url += (null == encodedContent) ? "" : ("?" + encodedContent);
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout((int) readTimeoutMs);
conn.setReadTimeout((int) readTimeoutMs);
setHeaders(conn, headers, encoding);
conn.connect();
int respCode = conn.getResponseCode(); // 这里内部发送请求
String resp = null;
if (HttpURLConnection.HTTP_OK == respCode) {
resp = IOTinyUtils.toString(conn.getInputStream(), encoding);
} else {
resp = IOTinyUtils.toString(conn.getErrorStream(), encoding);
}
return new HttpResult(respCode, resp);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
static private String encodingParams(List paramValues, String encoding)
throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
if (null == paramValues) {
return null;
}
for (Iterator iter = paramValues.iterator(); iter.hasNext(); ) {
sb.append(iter.next()).append("=");
sb.append(URLEncoder.encode(iter.next(), encoding));
if (iter.hasNext()) {
sb.append("&");
}
}
return sb.toString();
}
static private void setHeaders(HttpURLConnection conn, List headers, String encoding) {
if (null != headers) {
for (Iterator iter = headers.iterator(); iter.hasNext(); ) {
conn.addRequestProperty(iter.next(), iter.next());
}
}
conn.addRequestProperty("Client-Version", MQVersion.getVersionDesc(MQVersion.CurrentVersion));
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding);
// 其它
String ts = String.valueOf(System.currentTimeMillis());
conn.addRequestProperty("Metaq-Client-RequestTS", ts);
}
/**
* 发送POST请求。
*
* @param url
* @param headers
* 请求Header,可以为null
* @param paramValues
* 参数,可以为null
* @param encoding
* URL编码使用的字符集
* @param readTimeoutMs
* 响应超时
*
* @return
*
* @throws java.io.IOException
*/
static public HttpResult httpPost(String url, List headers, List paramValues,
String encoding, long readTimeoutMs) throws IOException {
String encodedContent = encodingParams(paramValues, encoding);
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(3000);
conn.setReadTimeout((int) readTimeoutMs);
conn.setDoOutput(true);
conn.setDoInput(true);
setHeaders(conn, headers, encoding);
conn.getOutputStream().write(encodedContent.getBytes());
int respCode = conn.getResponseCode(); // 这里内部发送请求
String resp = null;
if (HttpURLConnection.HTTP_OK == respCode) {
resp = IOTinyUtils.toString(conn.getInputStream(), encoding);
} else {
resp = IOTinyUtils.toString(conn.getErrorStream(), encoding);
}
return new HttpResult(respCode, resp);
} finally {
if (null != conn) {
conn.disconnect();
}
}
}
static public class HttpResult {
final public int code;
final public String content;
public HttpResult(int code, String content) {
this.code = code;
this.content = content;
}
}
}