Commit 0abb62e8 authored by yanzg's avatar yanzg

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

parent 589646f2
...@@ -11,6 +11,7 @@ import java.net.URLConnection; ...@@ -11,6 +11,7 @@ import java.net.URLConnection;
/** /**
* HTTP请求工具类 * HTTP请求工具类
*
* @author 颜佐光 * @author 颜佐光
*/ */
public class HttpHelper { public class HttpHelper {
...@@ -65,8 +66,7 @@ public class HttpHelper { ...@@ -65,8 +66,7 @@ public class HttpHelper {
// 设置通用属性 // 设置通用属性
httpConn.setRequestProperty("Accept", "*/*"); httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Connection", "Keep-Alive"); httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent", httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
// 设置POST方式 // 设置POST方式
httpConn.setDoInput(true); httpConn.setDoInput(true);
httpConn.setDoOutput(true); httpConn.setDoOutput(true);
...@@ -77,7 +77,7 @@ public class HttpHelper { ...@@ -77,7 +77,7 @@ public class HttpHelper {
/** /**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串 * 发送POST请求,当请求失败时,抛出异常或返回空字符串
* *
* @param httpConn 链接信息 * @param httpConn 链接信息
* @param jsonString 请求参数,json字符串。 * @param jsonString 请求参数,json字符串。
* @return 远程响应结果 * @return 远程响应结果
*/ */
...@@ -88,7 +88,6 @@ public class HttpHelper { ...@@ -88,7 +88,6 @@ public class HttpHelper {
BufferedReader in = null; BufferedReader in = null;
PrintWriter out = null; PrintWriter out = null;
// 处理请求参数 // 处理请求参数
// StringBuffer sb = new StringBuffer();
String params = ""; String params = "";
try { try {
params = jsonString; params = jsonString;
...@@ -98,14 +97,10 @@ public class HttpHelper { ...@@ -98,14 +97,10 @@ public class HttpHelper {
out.write(params); out.write(params);
// flush输出流的缓冲 // flush输出流的缓冲
out.flush(); out.flush();
// 定义BufferedReader输入流来读取URL的响应,设置编码方式 in = readStream(httpConn.getInputStream(), result);
in = new BufferedReader(new InputStreamReader(httpConn } catch (Exception ex) {
.getInputStream(), "UTF-8")); in = readStream(httpConn.getErrorStream(), result);
String line; throw new RuntimeException(ex.getMessage() + " 结果:" + result.toString(), ex);
// 读取返回的内容
while ((line = in.readLine()) != null) {
result.append(line);
}
} finally { } finally {
if (out != null) { if (out != null) {
out.close(); out.close();
...@@ -117,6 +112,25 @@ public class HttpHelper { ...@@ -117,6 +112,25 @@ public class HttpHelper {
return result.toString(); 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