文章插图
在开发微信公众号时,需要不时请求URL和数据封装 。为了不做重复的工作 。提取公共部分进行封装 。产生了相应的公众类 。今天先来写下请求类,代码如下:
public class HttpRequestProxy {
/**
* 连接超时
*/
private static int connectTimeOut = 5000;
/**
* 读取数据超时
*/
private static int readTimeOut = 10000;
/**
* 发送GET的HTTP请求
* @param reqUrl HTTP请求URL
* @param parameters 参数映射表
* @param recvEncoding 请求编码
* @return HTTP响应的字符串
*/
public static String doGet(String reqUrl, Map<String,String> parameters,
String recvEncoding) {
HttpURLConnection url_con = null;
String responseContent = null;
try {
//请求URL的封装
url_con = doHttpGetConnection(reqUrl,parameters);
InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in,
recvEncoding));
String tempLine = rd.readLine();
StringBuffer temp = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null) {
temp.Append(tempLine);
temp.append(crlf);
tempLine = rd.readLine();
}
responseContent = temp.toString();
rd.close();
in.close();
} catch (IOException e) {
} finally {
if (url_con != null) {
url_con.disconnect();
}
}
return responseContent;
}
/**
* 请求URL的封装
* @param reqUrl HTTP请求URL
* @param parameters 参数映射表
* @return URL的封装
* @throws IOException
*/
private static HttpURLConnection doHttpGetConnection(String reqUrl,
Map<String,String> parameters) throws IOException {
HttpURLConnection url_con = null;
String params = getMapParamsToStr(parameters,WeiXinConstant.REQUEST_ENCODING);
URL url = new URL(reqUrl);
url_con = (HttpURLConnection) url.openConnection();
url_con.setRequestMethod(WeiXinConstant.REQUEST_GET);
url_con.setConnectTimeout(
HttpRequestProxy.connectTimeOut);// (单位:毫秒)
url_con.setReadTimeout(
HttpRequestProxy.readTimeOut);// (单位:毫秒)
url_con.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(url_con.getOutputStream(),WeiXinConstant.REQUEST_ENCODING);
out.write(params);
out.flush();
out.close();
return url_con;
}
/**
* 发送POST的HTTP请求
*
* @param reqUrl HTTP请求URL
* @param parameters 参数映射表
* @param recvEncoding
* @return HTTP响应的字符串
*/
【使用java开发微信公众号系列-公共类】public static String doPost(String reqUrl, Map<String, String> parameters, String recvEncoding) {
HttpURLConnection url_con = null;
String responseContent = null;
try {
URL url = new URL(reqUrl);
url_con = (HttpURLConnection) url.openConnection();
url_con.setConnectTimeout(
HttpRequestProxy.connectTimeOut);// (单位:毫秒)
url_con.setReadTimeout(
HttpRequestProxy.readTimeOut);// (单位:毫秒)
url_con.setRequestMethod(WeiXinConstant.REQUEST_POST);
if (parameters!=null) {
url_con.setDoOutput(true);
String params = getMapParamsToStr(parameters, WeiXinConstant.REQUEST_ENCODING);
OutputStreamWriter out = new OutputStreamWriter(url_con.getOutputStream(),
WeiXinConstant.REQUEST_ENCODING);
out.write(params);
out.flush();
out.close();
}
InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in, recvEncoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null) {
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
} catch (IOException e) {
} finally {
if (url_con != null) {
url_con.disconnect();
}
}
return responseContent;
}
/**
* 发送POST的JSON HTTP请求
*
* @param reqUrl HTTP请求URL
* @param parameters 参数映射表
* @param recvEncoding
推荐阅读
- SpringBoot的可视化接口开发工具
- BI数据可视化工具使用技巧
- 浅谈java中重写和重载的区别
- 网络扫描神器Nmap常用操作方法详解以及使用脚本爆破telnet密码
- 详解Java反序列化漏洞
- 你开发的系统到底可以支撑多少并发访问?
- JavaScript 如何检测文件的类型?
- 用 C# 开发自己的语音识别程序
- 可以在线看视频和文档的开源网盘系统kiftd部署和使用教程
- 使用 IDEA 解决 Java8 的数据流问题,用过的都说好