
本文共 6848 字,大约阅读时间需要 22 分钟。
���������������������������������������������
������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������logo������������������������������������������������������������������������������������������������������������������������������������
������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������API������������������������������������������������������������������������������������������������������������
������������
���������������������������������������������������
������������
���������������API���������������������������������������������������������������������������������������������������������������������������������
- ������������������������������APPID���SECRET������������
- ���������������������������������
- ������������������������������������������������
���������������������������������������������������������������������������������������
-
������������
- ���������������access_token���
- ���������������
-
���������������
-
Controller������
-
������������
- ���������������������������������������������������������������������������������������������
- ������������������������������������������������������
- ���������������������������������������������������������������������������������������������������������������������������������
-
������������������
-
������������������������������
-
.ResponseBody���������������
���������������API������������������������������������������������
���������������������������������������������
������������������������������������������������������
package com.haishi.webappdemo.utils;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;public class HttpRequestUtils { public static String sendGet(String url, String param) throws IOException { BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); URLConnection connection = realUrl.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.connect(); Map> map = connection.getHeaderFields(); for (String key : map.keySet()) { System.out.println(key + "---" + map.get(key)); } in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("������GET���������������������" + e); e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } public static String sendPost(String url, Map param, HttpServletResponse response) throws IOException { PrintWriter out = null; InputStream in = null; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(conn.getOutputStream()); out.print(param); out.flush(); in = conn.getInputStream(); OutputStream outputStream = response.getOutputStream(); byte[] bytes = new byte[1024]; while ((len = in.read(bytes)) != -1) { outputStream.write(bytes, 0, len); } outputStream.flush(); outputStream.close(); } catch (Exception e) { System.out.println("������ POST ���������������������" + e); e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } }
������������������������HttpServletResponse���������������������������������������������
������������������������������������
������������������������������������������Controller���������������RESTful���������������������������������������
package com.webappdemo.controller;import com.haishi.webappdemo.utils.HttpRequestUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;import java.util.HashMap;import java.util.Map;@RestControllerpublic class TestController { @RequestMapping(value = "/getQRcode.json", method = RequestMethod.GET) public void getQRcode(@RequestParam("url") String url, HttpServletResponse response) { Mapparam = new HashMap<>(); HttpRequestUtils.sendPost(url, param, response); }}
���������������������������������������������������������������������������������������������������������������HttpServletResponse���������������������������������������������������������������������������������������������������������������
������������������������������������������������
���������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
