package com.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;
public class HttpConnectionUtil {
// post请求
public static final String HTTP_POST = "POST";
// get请求
public static final String HTTP_GET = "GET";
// utf-8字符编码
public static final String CHARSET_UTF_8 = "utf-8";
// HTTP内容类型。如果未指定ContentType,默认为TEXT/HTML
public static final String CONTENT_TYPE_TEXT_HTML = "text/xml";
// HTTP内容类型。相当于form表单的形式,提交暑假
public static final String CONTENT_TYPE_FORM_URL = "application/x-www-form-urlencoded";
// 请求超时时间
public static final int SEND_REQUEST_TIME_OUT = 50000;
// 将读超时时间
public static final int READ_TIME_OUT = 50000;
/**
*
* @param requestType
* 请求类型
* @param urlStr
* 请求地址
* @param body
* 请求发送内容
* @return 返回内容
*/
public static String requestMethod(String requestType, String urlStr, String body) {
// 是否有http正文提交
boolean isDoInput = false;
if (body != null && body.length() > 0)
isDoInput = true;
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;
try {
// 统一资源
URL url = new URL(urlStr);
// 连接类的父类,抽象类
URLConnection urlConnection = url.openConnection();
// http的连接类
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
// 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true, 默认情况下是false;
if (isDoInput) {
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(body.length()));
}
// 设置是否从httpUrlConnection读入,默认情况下是true;
httpURLConnection.setDoInput(true);
// 设置一个指定的超时值(以毫秒为单位)
httpURLConnection.setConnectTimeout(SEND_REQUEST_TIME_OUT);
// 将读超时设置为指定的超时,以毫秒为单位。
httpURLConnection.setReadTimeout(READ_TIME_OUT);
// Post 请求不能使用缓存
httpURLConnection.setUseCaches(false);
// 设置字符编码
httpURLConnection.setRequestProperty("Accept-Charset", CHARSET_UTF_8);
// 设置内容类型
httpURLConnection.setRequestProperty("Content-Type", CONTENT_TYPE_FORM_URL);
// 设定请求的方法,默认是GET
httpURLConnection.setRequestMethod(requestType);
// 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
// 如果在已打开连接(此时 connected 字段的值为 true)的情况下调用 connect 方法,则忽略该调用。
httpURLConnection.connect();
if (isDoInput) {
outputStream = httpURLConnection.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(body);
outputStreamWriter.flush();// 刷新
}
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception(
"HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
resultBuffer.append("\n");
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {// 关闭流
try {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (inputStreamReader != null) {
inputStreamReader.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultBuffer.toString();
}
public static void main(String[] args) throws MalformedURLException {
System.out.println(requestMethod(HTTP_GET, "http://127.0.0.1:8080/test/TestHttpRequestServlet",
"username=123&password=我是谁"));
}
}
POST
public static String post(String reqUrl, String body) throws IOException { URL serverUrl = new URL(reqUrl); HttpURLConnection conn = null; InputStreamReader isr = null; BufferedReader br = null; StringBuffer buffer = new StringBuffer(); try { conn = (HttpURLConnection) serverUrl.openConnection(); conn.setConnectTimeout(CONNECTION_TIMEOUT); conn.setReadTimeout(READ_TIMEOUT); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.connect(); conn.getOutputStream().write(body.getBytes(CharEncoding.UTF_8)); isr = new InputStreamReader(conn.getInputStream(), CharEncoding.UTF_8); br = new BufferedReader(isr); for (String line = ""; (line = br.readLine()) != null;) { buffer.append(line); } } finally { if (null != br) { br.close(); } if (null != isr) { isr.close(); } if (null != conn) { conn.disconnect(); } } return buffer.toString(); }
GET
public static String get(String url) throws IOException { URL reqUrl = new URL(url); HttpURLConnection conn = null; InputStream inStream = null; InputStreamReader isr = null; BufferedReader br = null; StringBuffer buffer = new StringBuffer(1024); try { conn = (HttpURLConnection) reqUrl.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setAllowUserInteraction(false); conn.setUseCaches(false); conn.setRequestMethod("GET"); inStream = conn.getInputStream(); isr = new InputStreamReader(inStream, CharEncoding.UTF_8); br = new BufferedReader(isr); for (String line = ""; (line = br.readLine()) != null;) { buffer.append(line); } } finally { if (null != inStream) { inStream.close(); } if (null != br) { br.close(); } if (null != isr) { isr.close(); } if (null != conn) { conn.disconnect(); } } return buffer.toString(); }