Commit 0abb62e8 authored by yanzg's avatar yanzg

修改MQ请求尸体,防止出错

parent 589646f2
......@@ -11,6 +11,7 @@ import java.net.URLConnection;
/**
* HTTP请求工具类
*
* @author 颜佐光
*/
public class HttpHelper {
......@@ -65,8 +66,7 @@ public class HttpHelper {
// 设置通用属性
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
// 设置POST方式
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
......@@ -77,7 +77,7 @@ public class HttpHelper {
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
* @param httpConn 链接信息
* @param httpConn 链接信息
* @param jsonString 请求参数,json字符串。
* @return 远程响应结果
*/
......@@ -88,7 +88,6 @@ public class HttpHelper {
BufferedReader in = null;
PrintWriter out = null;
// 处理请求参数
// StringBuffer sb = new StringBuffer();
String params = "";
try {
params = jsonString;
......@@ -98,14 +97,10 @@ public class HttpHelper {
out.write(params);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应,设置编码方式
in = new BufferedReader(new InputStreamReader(httpConn
.getInputStream(), "UTF-8"));
String line;
// 读取返回的内容
while ((line = in.readLine()) != null) {
result.append(line);
}
in = readStream(httpConn.getInputStream(), result);
} catch (Exception ex) {
in = readStream(httpConn.getErrorStream(), result);
throw new RuntimeException(ex.getMessage() + " 结果:" + result.toString(), ex);
} finally {
if (out != null) {
out.close();
......@@ -117,6 +112,25 @@ public class HttpHelper {
return result.toString();
}
/**
* 读取数据流
*
* @param stream
* @param result
* @return
* @throws IOException
*/
private static BufferedReader readStream(InputStream stream, StringBuilder result) throws IOException {
// 定义BufferedReader输入流来读取URL的响应,设置编码方式
BufferedReader in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String line;
// 读取返回的内容
while ((line = in.readLine()) != null) {
result.append(line);
}
return in;
}
/**
* 下载文件
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment