标签:lte inpu get comm spm apach import ati 组织
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.log4j.Logger;
import com.sekorm.autoscan.common.persistence.ResponseMessage;
public class HttpUtils {
public static Logger log = Logger.getLogger(HttpUtils.class);
@SuppressWarnings("rawtypes")
public static ResponseMessage sendRequest(String requestUrl,
Map<String, Object> requestParamsMap) {
ResponseMessage rspMessage = new ResponseMessage();
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
StringBuffer responseResult = new StringBuffer();
StringBuffer params = new StringBuffer();
HttpURLConnection httpURLConnection = null;
try {
// 组织请求参数
Iterator it = requestParamsMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry element = (Map.Entry) it.next();
params.append(element.getKey());
params.append("=");
params.append(element.getValue());
params.append("&");
}
if (params.length() > 0) {
params.deleteCharAt(params.length() - 1);
}
URL realUrl = new URL(requestUrl);
// 打开和URL之间的连接
httpURLConnection = (HttpURLConnection) realUrl.openConnection();
httpURLConnection.setRequestMethod("POST");
// 设置通用的请求属性
httpURLConnection.setRequestProperty("accept", "*/*");
httpURLConnection.setRequestProperty("connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Content-Length",
String.valueOf(params.length()));
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
printWriter.write(params.toString());
// flush输出流的缓冲
printWriter.flush();
// 根据ResponseCode判断连接是否成功
int responseCode = httpURLConnection.getResponseCode();
rspMessage.setStatusCode(responseCode);
// 定义BufferedReader输入流来读取URL的ResponseData
bufferedReader = new BufferedReader(new InputStreamReader(
httpURLConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
responseResult.append(line);
}
rspMessage.setContent(responseResult.toString());
} catch (Exception e) {
log.error("send post request error!" + e);
} finally {
httpURLConnection.disconnect();
try {
if (printWriter != null) {
printWriter.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return rspMessage;
}
@SuppressWarnings("rawtypes")
public static ResponseMessage sendHttpsRequest(String requestUrl,
Map<String, Object> requestParamsMap) {
ResponseMessage rspMessage = new ResponseMessage();
HttpsUtils util = HttpsUtils.getInstance();
HttpResponse resp;
try {
// 组织请求参数
Map<String, String> queryParams = new HashMap<String, String>();
Iterator it = requestParamsMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry element = (Map.Entry) it.next();
queryParams.put(String.valueOf(element.getKey()),
String.valueOf(element.getValue()));
}
resp = util.doPost(requestUrl, null, queryParams);
rspMessage.setStatusCode(resp.getStatusLine().getStatusCode());
rspMessage.setContent(HttpsUtils.readStream(resp.getEntity()
.getContent(), HttpsUtils.defaultEncoding));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return rspMessage;
}
}
标签:lte inpu get comm spm apach import ati 组织
原文地址:http://www.cnblogs.com/albert-think/p/7700019.html