HTTP请求
2021-06-27 03:04
标签:apach length close request params err public tco text 一:工具类HttpUtil HTTP请求 标签:apach length close request params err public tco text 原文地址:http://www.cnblogs.com/fdzfd/p/7149357.htmlpackage com.bw30.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
public class HttpUtil {
private static final int CONNECT_OUTTIME = 10000;
private static final int READ_OUTTIME = 10000;
private static final Logger logger = Logger.getLogger(HttpUtil.class);
private static final String SERVLET_POST = "POST";
private static final String SERVLET_GET = "GET";
private static final String SERVLET_DELETE = "DELETE";
private static final String SERVLET_PUT = "PUT";
public static String getRequetString(HttpServletRequest req)
throws IOException {
String returnStr = "";
BufferedInputStream bis = null;
ByteArrayOutputStream bos = null;
try {
bis = new BufferedInputStream(req.getInputStream());
bos = new ByteArrayOutputStream();
int b = bis.read();
while (b >= 0) {
bos.write(b);
b = bis.read();
}
returnStr = new String(bos.toByteArray(), "UTF-8");
bos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return returnStr.trim();
}
/***
* 发送get请求
* @param url
* @param enc
* @return
*/
public static String getUrl(String serverUrl, String enc) {
HttpURLConnection conn = null;
BufferedReader br = null;
StringBuffer response = new StringBuffer();
logger.info("Get请求:" + serverUrl);
if (CommonTool.isStrEmpty(enc)) {
enc = "UTF-8";
}
try {
URL url = new URL(serverUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(CONNECT_OUTTIME);
conn.setReadTimeout(READ_OUTTIME);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod(SERVLET_GET);
conn.setRequestProperty("Content-Type", "text/html; charset=" + enc);
conn.connect();
logger.info("响应代码:" + conn.getResponseCode());
br = new BufferedReader(new InputStreamReader(conn.getInputStream(), enc));
String line;
while ((line = br.readLine()) != null) {
response.append(line + "\r\n");
}
if (response.length() > 1) {
response.delete(response.length() - 2, response.length());
}
} catch (MalformedURLException e1) {
logger.error("执行HTTP Get请求" + serverUrl + "时,发生MalformedURLException异常!", e1);
} catch (ProtocolException e1) {
logger.error("执行HTTP Get请求" + serverUrl + "时,发生ProtocolException异常!", e1);
} catch (IOException e1) {
logger.error("执行HTTP Get请求" + serverUrl + "时,发生IOException异常!", e1);
} finally {
try {
if (br != null) {
br.close();
br = null;
}
if (conn != null) {
conn.disconnect();
conn = null;
}
} catch (IOException e) {
}
}
logger.info("响应内容:" + response.toString());
return response.toString();
}
public static String postUrl(String serverUrl, String content, String enc) {
HttpURLConnection conn = null;
BufferedOutputStream out = null;
BufferedReader in = null;
StringBuffer response = new StringBuffer();
if (content == null) {
content = "";
}
logger.info("Post请求:" + serverUrl);
if (!content.isEmpty()) {
logger.info("请求参数: " + content);
}
if (CommonTool.isStrEmpty(enc)) {
enc = "UTF-8";
}
try {
URL url = new URL(serverUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Length", Integer
.toString(content.getBytes(enc).length));
// conn.setRequestProperty("Content-type",
// "application/x-java-serialized-object");// application/x-www-form-urlencoded
conn.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
conn.setRequestMethod(SERVLET_POST);
conn.setConnectTimeout(CONNECT_OUTTIME);
conn.setReadTimeout(READ_OUTTIME);
conn.setDoOutput(true);
conn.setDoInput(true);
// @post xml
out = new BufferedOutputStream(conn.getOutputStream());
StringBuffer buffer = new StringBuffer(content.length());
buffer.append(content);
out.write(buffer.toString().getBytes(enc));
out.flush();
// @make xml
logger.info("响应代码:" + conn.getResponseCode());
in = new BufferedReader(new InputStreamReader(
conn.getInputStream(), enc));
String line;
while ((line = in.readLine()) != null) {
response.append(line + "\r\n");
}
if (response.length() > 1) {
response.delete(response.length() - 2, response.length());
}
} catch (MalformedURLException e1) {
logger.error("执行HTTP Post请求" + serverUrl + "时,发生MalformedURLException异常!", e1);
} catch (UnsupportedEncodingException e1) {
logger.error("执行HTTP Post请求" + serverUrl + "时,发生UnsupportedEncodingException异常!", e1);
} catch (ProtocolException e1) {
logger.error("执行HTTP Post请求" + serverUrl + "时,发生ProtocolException异常!", e1);
} catch (IOException e1) {
logger.error("执行HTTP Post请求" + serverUrl + "时,发生IOException异常!", e1);
} catch (Exception e1) {
logger.error("执行HTTP Post请求" + serverUrl + "时,发生Exception异常!", e1);
} finally {
try {
if (out != null) {
out.close();
out = null;
}
if (in != null) {
in.close();
in = null;
}
if (conn != null) {
conn.disconnect();
conn = null;
}
} catch (IOException e) {
}
}
logger.info("响应内容:" + response.toString());
return response.toString();
}
public static String postXml(String serverUrl, String content, String enc) {
HttpURLConnection conn = null;
BufferedOutputStream out = null;
BufferedReader in = null;
StringBuffer response = new StringBuffer();
if (content == null) {
content = "";
}
logger.info("Post请求:" + serverUrl);
if (!content.isEmpty()) {
logger.info("请求参数: " + content);
}
if (CommonTool.isStrEmpty(enc)) {
enc = "UTF-8";
}
try {
URL url = new URL(serverUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Length", Integer.toString(content.getBytes(enc).length));
conn.setRequestProperty("Content-type", "text/html; charset=" + enc);
conn.setRequestMethod(SERVLET_POST);
conn.setConnectTimeout(CONNECT_OUTTIME);
conn.setReadTimeout(READ_OUTTIME);
conn.setDoOutput(true);
conn.setDoInput(true);
// @post xml
out = new BufferedOutputStream(conn.getOutputStream());
StringBuffer buffer = new StringBuffer(content.length());
buffer.append(content);
out.write(buffer.toString().getBytes(enc));
out.flush();
// @make xml
logger.info("响应代码:" + conn.getResponseCode());
in = new BufferedReader(new InputStreamReader(
conn.getInputStream(), enc));
String line;
while ((line = in.readLine()) != null) {
response.append(line + "\r\n");
}
if (response.length() > 1) {
response.delete(response.length() - 2, response.length());
}
} catch (MalformedURLException e1) {
logger.error("执行HTTP Post请求" + serverUrl + "时,发生MalformedURLException异常!", e1);
} catch (UnsupportedEncodingException e1) {
logger.error("执行HTTP Post请求" + serverUrl + "时,发生UnsupportedEncodingException异常!", e1);
} catch (ProtocolException e1) {
logger.error("执行HTTP Post请求" + serverUrl + "时,发生ProtocolException异常!", e1);
} catch (IOException e1) {
logger.error("执行HTTP Post请求" + serverUrl + "时,发生IOException异常!", e1);
} finally {
try {
if (out != null) {
out.close();
out = null;
}
if (in != null) {
in.close();
in = null;
}
if (conn != null) {
conn.disconnect();
conn = null;
}
} catch (IOException e) {
}
}
logger.info("响应内容:" + response.toString());
return response.toString();
}
/**
*
* 执行一个HTTP POST 请求,返回响应字符串
*
* @param url
* 请求的URL地址
* @param params
* 请求参数
* @return 返回响应字符串
* @throws UnsupportedEncodingException
*/
// public static String doPost(String url, Map