Java实现GET和POST请求调用微信公众平台接口常用模板

??? 在做微信公众平台后台开发的时候,常常需要用代码实现模拟浏览器的GET和POST请求,
下面是用Java语言实现的GET和POST请求的一般写法:

??? 1.GET请求模拟(不带参数)
???
??? //由于这类方法经常被用到,因此建议写在一个工具类里面,设置为静态方法,方便调用。
??? //requestUrl表示请求链接
??? public static String sendGet(String requestUrl) {
??? ??? StringBuffer buffer = null;

??? ??? try {
??? ??? ??? // 建立连接
??? ??? ??? URL url = new URL(requestUrl);
??? ??? ??? HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
??? ??? ??? httpUrlConn.setDoInput(true);
??? ??? ??? httpUrlConn.setRequestMethod("GET");

??? ??? ??? // 获取输入流
??? ??? ??? InputStream inputStream = httpUrlConn.getInputStream();
??? ??? ??? InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
??? ??? ??? BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

??? ??? ??? // 读取返回结果
??? ??? ??? buffer = new StringBuffer();
??? ??? ??? String str = null;
??? ??? ??? while ((str = bufferedReader.readLine()) != null) {
??? ??? ??? ??? buffer.append(str);
??? ??? ??? }

??? ??? ??? // 释放资源
??? ??? ??? bufferedReader.close();
??? ??? ??? inputStreamReader.close();
??? ??? ??? inputStream.close();
??? ??? ??? httpUrlConn.disconnect();
??? ??? } catch (Exception e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? return buffer.toString();
??? }

??? 2.GET请求模拟(带参数)
???
??? //由于这类方法经常被用到,因此建议写在一个工具类里面,设置为静态方法,方便调用。
??? //url表示请求链接,param表示json格式的请求参数
??? public static String sendGet(String url, Object param) {
??? ??? String result = "";?????
??? ??? BufferedReader in = null;
??? ??? PrintWriter out = null;
??? ??? try {
??? ??? ??? URL realUrl = new URL(url);
??? ??? ??? // 打开和URL之间的连接
??? ??? ??? URLConnection connection = realUrl.openConnection();
??? ??? ??? connection.setDoOutput(true);
??? ??? ??? // 建立实际的连接
??? ??? ??? connection.connect();
??? ??? ??? out = new PrintWriter(connection.getOutputStream());
??? ??? ??? // 发送请求参数
??? ??? ??? out.print(param);
??? ??? ??? // 定义 BufferedReader输入流来读取URL的响应
??? ??? ??? in = new BufferedReader(new InputStreamReader(
??? ??? ??? ??? ??? connection.getInputStream(), "UTF-8"));
??? ??? ??? String line;
??? ??? ??? while ((line = in.readLine()) != null) {
??? ??? ??? ??? result += line;
??? ??? ??? }
??? ??? } catch (Exception e) {
??? ??? ??? System.out.println("发送GET请求出现异常!" + e);
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? // 使用finally块来关闭输入流
??? ??? finally {
??? ??? ??? try {
??? ??? ??? ??? if (in != null) {
??? ??? ??? ??? ??? in.close();
??? ??? ??? ??? }
??? ??? ??? } catch (Exception e2) {
??? ??? ??? ??? e2.printStackTrace();
??? ??? ??? }
??? ??? }
??? ??? System.out.println(result);
??? ??? return result;
??? }
???
???
???
??? 3.POST请求模拟
???
??? //由于这类方法经常被用到,因此建议写在一个工具类里面,设置为静态方法,方便调用。
??? //url表示请求链接,param表示json格式的请求参数
??? public static String sendPost(String url, Object param) {
??? ??? PrintWriter out = null;
??? ??? BufferedReader in = null;
??? ??? String result = "";
??? ??? try {
??? ??? ??? URL realUrl = new URL(url);
??? ??? ??? // 打开和URL之间的连接
??? ??? ??? URLConnection conn = realUrl.openConnection();
??? ??? ??? // 设置通用的请求属性 注意Authorization生成
??? ??? ??? // conn.setRequestProperty("Content-Type",
??? ??? ??? // "application/x-www-form-urlencoded");
??? ??? ??? // 发送POST请求必须设置如下两行
??? ??? ??? conn.setDoOutput(true);
??? ??? ??? conn.setDoInput(true);
??? ??? ??? // 获取URLConnection对象对应的输出流
??? ??? ??? out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8"));
??? ??? ??? // 发送请求参数
??? ??? ??? out.print(param);
??? ??? ??? // flush输出流的缓冲
??? ??? ??? out.flush();
??? ??? ??? // 定义BufferedReader输入流来读取URL的响应
??? ??? ??? in = new BufferedReader(
??? ??? ??? ??? ??? new InputStreamReader(conn.getInputStream(),"utf-8"));
??? ??? ??? String line;
??? ??? ??? while ((line = in.readLine()) != null) {
??? ??? ??? ??? result += line;
??? ??? ??? }
??? ??? ??? System.out.println(result);
??? ??? } catch (Exception e) {
??? ??? ??? System.out.println("发送 POST 请求出现异常!" + e);
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? // 使用finally块来关闭输出流、输入流
??? ??? finally {
??? ??? ??? try {
??? ??? ??? ??? if (out != null) {
??? ??? ??? ??? ??? out.close();
??? ??? ??? ??? }
??? ??? ??? ??? if (in != null) {
??? ??? ??? ??? ??? in.close();
??? ??? ??? ??? }
??? ??? ??? } catch (IOException ex) {
??? ??? ??? ??? ex.printStackTrace();
??? ??? ??? }
??? ??? }
??? ??? return result;
??? }
???
??? 以上方法里面所用到的包,这里没有引入,只是作为一个实例程序,说明一下模拟浏览器的GET和POST请求的常见写法。
??? 希望对朋友们有用!

Java实现GET和POST请求调用微信公众平台接口常用模板

上一篇:IE调试网页之七:使用探查器工具分析代码性能 (Windows)


下一篇:微信js sdk分享开发摘记java版