Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.alibaba.rocketmq.common.utils;
import com.alibaba.rocketmq.common.MQVersion;
import com.alibaba.rocketmq.common.MixAll;
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;
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", 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);
}
/**
*
* @param url
* @param headers
* @param paramValues
* @param encoding
* @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(MixAll.DEFAULT_CHARSET));
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;
}
}
}