package com.example.demo.jiahao; import com.alibaba.fastjson.JSON; import net.sf.json.JSONObject; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; @RestController public class DemoController { @RequestMapping("/get") public Object get1() { Object postHTTP_1 = postHTTP_1("tslj", "123456"); return postHTTP_1; } public Object postHTTP_1(String username, String password) { /* *第一种方式post请求 */ JSONObject result = new JSONObject(); JSONObject jsons = new JSONObject(); jsons.put("key", "value"); com.alibaba.fastjson.JSONObject jsonss = null; try { URL url = new URL("http://192.168.0.129:8089/health/login"); //打开和url之间的连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); PrintWriter out = null; //请求方式 这里设置你需要请求的方式 //conn.setRequestMethod("POST"); 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); //这里设置请求头 conn.setRequestProperty("Content-Type", "application/json;charset=utf-8"); //设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 //最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, //post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。 conn.setDoOutput(true); conn.setDoInput(true); //获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); //设置请求参数 Map map = new HashMap(); map.put("username", username); map.put("password", password); String toJSONString = JSON.toJSONString(map); //发送请求参数即数据 out.print(toJSONString); //缓冲数据 out.flush(); //获取URLConnection对象对应的输入流 InputStream is = conn.getInputStream(); //构造一个字符流缓存 BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = ""; while ((str = br.readLine()) != null) { jsonss = com.alibaba.fastjson.JSONObject.parseObject(str); } System.out.println("获取到的报文数据为:" + jsonss); //关闭流 is.close(); //断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被 其他线程使用就不切断。 //固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上 disconnect后正常一些。 conn.disconnect(); System.out.println("结束"); } catch (Exception e) { e.printStackTrace(); } return jsonss; } public Object getHTTP_1() { JSONObject result = new JSONObject(); JSONObject jsons = new JSONObject(); jsons.put("key", "value"); com.alibaba.fastjson.JSONObject jsonss = null; try { /* *第二种方式,get请求 */ JSONObject json = new JSONObject(); json.put("orderId", "2019061015241733858019"); String urls = "http://www.baidu.com?vehicleCode=236521904334965"; URL url = new URL(urls); //打开和url之间的连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); PrintWriter out = null; //请求方式 conn.setRequestMethod("GET"); //设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "application/json;charset=utf-8"); //设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 //最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, //post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。get请求不需要设置 //conn.setDoOutput(true); //conn.setDoInput(true); //获取URLConnection对象对应的输出流 conn.connect(); //获取URLConnection对象对应的输入流 InputStream is = conn.getInputStream(); //构造一个字符流缓存 BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = ""; while ((str = br.readLine()) != null) { jsonss = com.alibaba.fastjson.JSONObject.parseObject(str); } System.out.println("获取到的报文数据为:" + jsonss); //关闭流 is.close(); //断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。 //固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些。 conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } System.out.println("结束"); return jsonss; } public Object postHTTP() throws IOException { /* *第一种方式post请求 */ JSONObject result = new JSONObject(); JSONObject jsons = new JSONObject(); jsons.put("key", "value"); com.alibaba.fastjson.JSONObject jsonss = null; try { URL url = new URL("这里写你需要调用接口的地址"); //打开和url之间的连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); PrintWriter out = null; //请求方式 这里设置你需要请求的方式 //conn.setRequestMethod("POST"); 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); //这里设置请求头 conn.setRequestProperty("Content-Type", "application/json;charset=utf-8"); //设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 //最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, //post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。 conn.setDoOutput(true); conn.setDoInput(true); //获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); //发送请求参数即数据 out.print(result); //缓冲数据 out.flush(); //获取URLConnection对象对应的输入流 InputStream is = conn.getInputStream(); //构造一个字符流缓存 BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = ""; while ((str = br.readLine()) != null) { jsonss = com.alibaba.fastjson.JSONObject.parseObject(str); } System.out.println("获取到的报文数据为:" + jsonss); //关闭流 is.close(); //断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被 其他线程使用就不切断。 //固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上 disconnect后正常一些。 conn.disconnect(); System.out.println("结束"); } catch (Exception e) { e.printStackTrace(); } return jsonss; } public Object getHTTP() { JSONObject result = new JSONObject(); JSONObject jsons = new JSONObject(); jsons.put("key", "value"); com.alibaba.fastjson.JSONObject jsonss = null; try { /* *第二种方式,get请求 */ JSONObject json = new JSONObject(); json.put("orderId", "2019061015241733858019"); String urls = "http://www.baidu.com?vehicleCode=236521904334965"; URL url = new URL(urls); //打开和url之间的连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); PrintWriter out = null; //请求方式 conn.setRequestMethod("GET"); //设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "application/json;charset=utf-8"); //设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 //最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, //post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。get请求不需要设置 //conn.setDoOutput(true); //conn.setDoInput(true); //获取URLConnection对象对应的输出流 conn.connect(); //获取URLConnection对象对应的输入流 InputStream is = conn.getInputStream(); //构造一个字符流缓存 BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = ""; while ((str = br.readLine()) != null) { jsonss = com.alibaba.fastjson.JSONObject.parseObject(str); } System.out.println("获取到的报文数据为:" + jsonss); //关闭流 is.close(); //断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。 //固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些。 conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } System.out.println("结束"); return jsonss; } } POM文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>MySprigBoot_01</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!--阿里JSON--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.41</version> </dependency> <!--阿里JSON--> <!-- JSONObject --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>