com.aliyun.angelia.rpc.tools.HttpTinyClient Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.aliyun.angelia.rpc.tools;
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;
/**
* @author xuanyin
*/
public class HttpTinyClient {
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", "V1.0.0");
conn.addRequestProperty("Content-com.alibaba.angelia.annotation.Type", "application/x-www-form-urlencoded;charset=" + encoding);
String ts = String.valueOf(System.currentTimeMillis());
conn.addRequestProperty("Metaq-Client-RequestTS", ts);
}
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("UTF-8"));
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;
}
}
}