/**
* Http 请求访问工具类
*
* @author admin
* @see HttpAccessUtil
* @since
*/
public class HttpAccessUtil
{
/**
* 发起http请求并获取结果 UTF-8
*
* @param requestUrl 请求地址
* @param mimeTypes enum MimeTypes
* @param requestMethod 请求方式(GET、POST)
* @param connTimeoutMills 设置连接主机超时,单位:毫秒
* @param readTimeoutMills 设置从主机读取数据超时,单位:毫秒
* @param attachments 附加提交的数据,可以是单字符串{"json":"value"} 或者 多个参数遵循 A=a&B=b格式
*
* @return remoteHttp 返回的结果
*/
public static String httpRequest(String requestUrl, String mimeTypes, String requestMethod,
int connTimeoutMills, int readTimeoutMills, String attachments)
throws Exception
{
return httpRequest(requestUrl, "UTF-8", "text/plain;charset=UTF-8", requestMethod, connTimeoutMills,
readTimeoutMills, attachments);
}
/**
* 发起http请求并获取结果
*
* @param requestUrl 请求地址
* @param chartSet 字符集
* @param mimeTypes enum MimeTypes
* @param requestMethod 请求方式(GET、POST)
* @param connTimeoutMills 设置连接主机超时,单位:毫秒
* @param readTimeoutMills 设置从主机读取数据超时,单位:毫秒
* @param attachments 附加提交的数据,可以是单字符串{"json":"value"} 或者 多个参数遵循 A=a&B=b格式
*
* @return remoteHttp 返回的结果
*/
public static String httpRequest(String requestUrl, String chartSet, String mimeTypes,
String requestMethod, int connTimeoutMills,
int readTimeoutMills, String attachments)
throws Exception
{
HttpURLConnection httpUrlConn = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
// PS:其实亦可以统一使用ObjectOutputStream,因为String也是impl Serializable
OutputStream outputStream = null;
StringBuilder buffer = new StringBuilder();
try
{
URL url = new URL(requestUrl);
httpUrlConn = (HttpURLConnection)url.openConnection();
// 设置content_type
httpUrlConn.setRequestProperty("Content-Type", "text/plain;charset=UTF-8");
httpUrlConn.setConnectTimeout(connTimeoutMills);
httpUrlConn.setReadTimeout(readTimeoutMills);
// 设置是否向httpUrlConn输出,因为兼容post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false
httpUrlConn.setDoOutput(true);
// 设置是否从httpUrlConn读入,默认情况下是true
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST),默认是GET
httpUrlConn.setRequestMethod(requestMethod);
// 完成相关配置后,进行connect操作,实际上只是建立了一个与服务器的tcp连接,并没有实际发送http请求
httpUrlConn.connect();
// 当有额外数据需要提交时
if (StringUtils.isNotBlank(attachments))
{
// 此处getOutputStream会隐含的进行connect,即:如同调用上面的connect()方法,
// 所以在开发中不调用上述的connect()也可以,不过建议最好显式调用
outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(attachments.getBytes(chartSet));
outputStream.flush();
// outputStream不是一个网络流,充其量是个字符串流,往里面写入的东西不会立即发送到网络,
// 而是存在于内存缓冲区中,待outputStream流关闭时,根据输入的内容生成http正文。所以这里的close是必须的
outputStream.close();
}
// 将返回的输入流转换成字符串
// 无论是post还是get,http请求实际上直到HttpURLConnection的getInputStream()这个函数里面才正式发送出去
inputStream = httpUrlConn.getInputStream();//注意,实际发送请求的代码段就在这里
inputStreamReader = new InputStreamReader(inputStream, chartSet);
bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null)
{
buffer.append(str);
}
}
catch (Exception e)
{
log.error(requestMethod + " " + requestUrl + " error ", e);
throw e;
}
finally
{
try
{
IOUtils.closeQuietly(bufferedReader);
IOUtils.closeQuietly(inputStreamReader);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
if (httpUrlConn != null)
{
httpUrlConn.disconnect();
}
}
catch (Exception e)
{
log.error(e);
}
}
return buffer.toString();
}
/**
* 采取post方式提交序列化后的object对象 </br>
* 另请参考:java.io.ObjectInputStream/ObjectOutputStream
* @param requestUrl 请求地址
* @param connTimeoutMills 设置连接主机超时,单位:毫秒
* @param readTimeoutMills 设置从主机读取数据超时,单位:毫秒
* @param serializedObject 序列化后的object对象
*
* @return remoteHttp返回的结果
*/
public static String httpPostSerialObject(String requestUrl, int connTimeoutMills,
int readTimeoutMills, Object serializedObject)
throws Exception
{
HttpURLConnection httpUrlConn = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
ObjectOutputStream oos = null;
StringBuilder buffer = new StringBuilder();
try
{
URL url = new URL(requestUrl);
httpUrlConn = (HttpURLConnection)url.openConnection();
// 设置content_type=SERIALIZED_OBJECT
// 如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException
httpUrlConn.setRequestProperty("Content-Type",
"text/plain;charset=UTF-8");
httpUrlConn.setConnectTimeout(connTimeoutMills);
httpUrlConn.setReadTimeout(readTimeoutMills);
// 设置是否向httpUrlConn输出,因为是post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false
httpUrlConn.setDoOutput(true);
// 设置是否从httpUrlConn读入,默认情况下是true
httpUrlConn.setDoInput(true);
// 不使用缓存
httpUrlConn.setUseCaches(false);
// 设置请求方式,默认是GET
httpUrlConn.setRequestMethod("POST");
httpUrlConn.connect();
if (serializedObject != null)
{
// 此处getOutputStream会隐含的进行connect,即:如同调用上面的connect()方法,
// 所以在开发中不调用上述的connect()也可以,不过建议最好显式调用
// write object(impl Serializable) using ObjectOutputStream
oos = new ObjectOutputStream(httpUrlConn.getOutputStream());
oos.writeObject(serializedObject);
oos.flush();
// outputStream不是一个网络流,充其量是个字符串流,往里面写入的东西不会立即发送到网络,
// 而是存在于内存缓冲区中,待outputStream流关闭时,根据输入的内容生成http正文。所以这里的close是必须的
oos.close();
}
// 将返回的输入流转换成字符串
// 无论是post还是get,http请求实际上直到HttpURLConnection的getInputStream()这个函数里面才正式发送出去
inputStream = httpUrlConn.getInputStream();//注意,实际发送请求的代码段就在这里
inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null)
{
buffer.append(str);
}
}
catch (Exception e)
{
log.error(requestUrl + " error ", e);
throw e;
}
finally
{
try
{
IOUtils.closeQuietly(bufferedReader);
IOUtils.closeQuietly(inputStreamReader);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(oos);
if (httpUrlConn != null)
{
httpUrlConn.disconnect();
}
}
catch (Exception e)
{
log.error(e);
}
}
return buffer.toString();
}
public static void main(String[] args)
throws Exception
{
//传递的参数可以用String拼接,或是map
//方法一:
StringBuffer buffer = new StringBuffer();
buffer.append("{");
buffer.append("\"uniqueCode\":\"asdagrthrtjretgwevxcnhyjyt4576434\",");
buffer.append("\"data\":[" +
"{\"param1\":param1value,\"param2\":\"param2value\",\"param3\":\"param3value\",\"param4\":param4value,\"param5\":param5value\"}," +
"]");
buffer.append("}");
System.out.println(buffer.toString());
//测试URL
System.out.println(httpRequest("http://", "text/plain;charset=UTF-8", "POST",
100000, 100000,buffer.toString()));
//方法二:
Map<String,Object> paraMap=new HashMap<String,Object>();
paraMap.put(key1,value1);
paraMap.put(key2,value2);
//测试URL
System.out.println(httpRequest("http://", "text/plain;charset=UTF-8", "POST",
100000, 100000,JsonObeject.fromObject(paraMap).toString()));
}
}
* Http 请求访问工具类
*
* @author admin
* @see HttpAccessUtil
* @since
*/
public class HttpAccessUtil
{
/**
* 发起http请求并获取结果 UTF-8
*
* @param requestUrl 请求地址
* @param mimeTypes enum MimeTypes
* @param requestMethod 请求方式(GET、POST)
* @param connTimeoutMills 设置连接主机超时,单位:毫秒
* @param readTimeoutMills 设置从主机读取数据超时,单位:毫秒
* @param attachments 附加提交的数据,可以是单字符串{"json":"value"} 或者 多个参数遵循 A=a&B=b格式
*
* @return remoteHttp 返回的结果
*/
public static String httpRequest(String requestUrl, String mimeTypes, String requestMethod,
int connTimeoutMills, int readTimeoutMills, String attachments)
throws Exception
{
return httpRequest(requestUrl, "UTF-8", "text/plain;charset=UTF-8", requestMethod, connTimeoutMills,
readTimeoutMills, attachments);
}
/**
* 发起http请求并获取结果
*
* @param requestUrl 请求地址
* @param chartSet 字符集
* @param mimeTypes enum MimeTypes
* @param requestMethod 请求方式(GET、POST)
* @param connTimeoutMills 设置连接主机超时,单位:毫秒
* @param readTimeoutMills 设置从主机读取数据超时,单位:毫秒
* @param attachments 附加提交的数据,可以是单字符串{"json":"value"} 或者 多个参数遵循 A=a&B=b格式
*
* @return remoteHttp 返回的结果
*/
public static String httpRequest(String requestUrl, String chartSet, String mimeTypes,
String requestMethod, int connTimeoutMills,
int readTimeoutMills, String attachments)
throws Exception
{
HttpURLConnection httpUrlConn = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
// PS:其实亦可以统一使用ObjectOutputStream,因为String也是impl Serializable
OutputStream outputStream = null;
StringBuilder buffer = new StringBuilder();
try
{
URL url = new URL(requestUrl);
httpUrlConn = (HttpURLConnection)url.openConnection();
// 设置content_type
httpUrlConn.setRequestProperty("Content-Type", "text/plain;charset=UTF-8");
httpUrlConn.setConnectTimeout(connTimeoutMills);
httpUrlConn.setReadTimeout(readTimeoutMills);
// 设置是否向httpUrlConn输出,因为兼容post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false
httpUrlConn.setDoOutput(true);
// 设置是否从httpUrlConn读入,默认情况下是true
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST),默认是GET
httpUrlConn.setRequestMethod(requestMethod);
// 完成相关配置后,进行connect操作,实际上只是建立了一个与服务器的tcp连接,并没有实际发送http请求
httpUrlConn.connect();
// 当有额外数据需要提交时
if (StringUtils.isNotBlank(attachments))
{
// 此处getOutputStream会隐含的进行connect,即:如同调用上面的connect()方法,
// 所以在开发中不调用上述的connect()也可以,不过建议最好显式调用
outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(attachments.getBytes(chartSet));
outputStream.flush();
// outputStream不是一个网络流,充其量是个字符串流,往里面写入的东西不会立即发送到网络,
// 而是存在于内存缓冲区中,待outputStream流关闭时,根据输入的内容生成http正文。所以这里的close是必须的
outputStream.close();
}
// 将返回的输入流转换成字符串
// 无论是post还是get,http请求实际上直到HttpURLConnection的getInputStream()这个函数里面才正式发送出去
inputStream = httpUrlConn.getInputStream();//注意,实际发送请求的代码段就在这里
inputStreamReader = new InputStreamReader(inputStream, chartSet);
bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null)
{
buffer.append(str);
}
}
catch (Exception e)
{
log.error(requestMethod + " " + requestUrl + " error ", e);
throw e;
}
finally
{
try
{
IOUtils.closeQuietly(bufferedReader);
IOUtils.closeQuietly(inputStreamReader);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
if (httpUrlConn != null)
{
httpUrlConn.disconnect();
}
}
catch (Exception e)
{
log.error(e);
}
}
return buffer.toString();
}
/**
* 采取post方式提交序列化后的object对象 </br>
* 另请参考:java.io.ObjectInputStream/ObjectOutputStream
* @param requestUrl 请求地址
* @param connTimeoutMills 设置连接主机超时,单位:毫秒
* @param readTimeoutMills 设置从主机读取数据超时,单位:毫秒
* @param serializedObject 序列化后的object对象
*
* @return remoteHttp返回的结果
*/
public static String httpPostSerialObject(String requestUrl, int connTimeoutMills,
int readTimeoutMills, Object serializedObject)
throws Exception
{
HttpURLConnection httpUrlConn = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
ObjectOutputStream oos = null;
StringBuilder buffer = new StringBuilder();
try
{
URL url = new URL(requestUrl);
httpUrlConn = (HttpURLConnection)url.openConnection();
// 设置content_type=SERIALIZED_OBJECT
// 如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException
httpUrlConn.setRequestProperty("Content-Type",
"text/plain;charset=UTF-8");
httpUrlConn.setConnectTimeout(connTimeoutMills);
httpUrlConn.setReadTimeout(readTimeoutMills);
// 设置是否向httpUrlConn输出,因为是post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false
httpUrlConn.setDoOutput(true);
// 设置是否从httpUrlConn读入,默认情况下是true
httpUrlConn.setDoInput(true);
// 不使用缓存
httpUrlConn.setUseCaches(false);
// 设置请求方式,默认是GET
httpUrlConn.setRequestMethod("POST");
httpUrlConn.connect();
if (serializedObject != null)
{
// 此处getOutputStream会隐含的进行connect,即:如同调用上面的connect()方法,
// 所以在开发中不调用上述的connect()也可以,不过建议最好显式调用
// write object(impl Serializable) using ObjectOutputStream
oos = new ObjectOutputStream(httpUrlConn.getOutputStream());
oos.writeObject(serializedObject);
oos.flush();
// outputStream不是一个网络流,充其量是个字符串流,往里面写入的东西不会立即发送到网络,
// 而是存在于内存缓冲区中,待outputStream流关闭时,根据输入的内容生成http正文。所以这里的close是必须的
oos.close();
}
// 将返回的输入流转换成字符串
// 无论是post还是get,http请求实际上直到HttpURLConnection的getInputStream()这个函数里面才正式发送出去
inputStream = httpUrlConn.getInputStream();//注意,实际发送请求的代码段就在这里
inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null)
{
buffer.append(str);
}
}
catch (Exception e)
{
log.error(requestUrl + " error ", e);
throw e;
}
finally
{
try
{
IOUtils.closeQuietly(bufferedReader);
IOUtils.closeQuietly(inputStreamReader);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(oos);
if (httpUrlConn != null)
{
httpUrlConn.disconnect();
}
}
catch (Exception e)
{
log.error(e);
}
}
return buffer.toString();
}
public static void main(String[] args)
throws Exception
{
//传递的参数可以用String拼接,或是map
//方法一:
StringBuffer buffer = new StringBuffer();
buffer.append("{");
buffer.append("\"uniqueCode\":\"asdagrthrtjretgwevxcnhyjyt4576434\",");
buffer.append("\"data\":[" +
"{\"param1\":param1value,\"param2\":\"param2value\",\"param3\":\"param3value\",\"param4\":param4value,\"param5\":param5value\"}," +
"]");
buffer.append("}");
System.out.println(buffer.toString());
//测试URL
System.out.println(httpRequest("http://", "text/plain;charset=UTF-8", "POST",
100000, 100000,buffer.toString()));
//方法二:
Map<String,Object> paraMap=new HashMap<String,Object>();
paraMap.put(key1,value1);
paraMap.put(key2,value2);
//测试URL
System.out.println(httpRequest("http://", "text/plain;charset=UTF-8", "POST",
100000, 100000,JsonObeject.fromObject(paraMap).toString()));
}
}