Post+Get方式接口测试代码编写

 

详细代码如下 

  1 package testproject;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 import java.io.PrintWriter;
  7 import java.net.URL;
  8 import java.net.URLConnection;
  9 import java.util.List;
 10 import java.util.Map;
 11 import org.junit.Test;
 12 import net.sf.json.JSONObject;
 13 
 14 /**
 15  * @author jinzy
 16  * @version 创建时间:2017年8月3日
 17  * 类说明:GET,POST请求接口测试
 18  */
 19 public class HttpInterfaceTest {
 20     /**
 21      * 向指定URL发送GET方法的请求
 22      * 
 23      * @param url
 24      *            发送请求的URL
 25      * @param param
 26      *            请求参数,请求参数应该是name1=value1&name2=value2的形式。
 27      * @return URL所代表远程资源的响应
 28      */
 29 
 30     public static String sendGet(String url, String param) {
 31         String result = "";
 32         BufferedReader in = null;
 33         try {
 34             String urlName = url + "?" + param;
 35             System.out.println("Get请求接口:" + urlName);
 36             URL realUrl = new URL(urlName);
 37             // 打开和URL之间的连接
 38             URLConnection conn = realUrl.openConnection();
 39             // 设置通用的请求属性
 40             conn.setRequestProperty("accept", "*/*");
 41             conn.setRequestProperty("connection", "Keep-Alive");
 42             conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
 43             // 建立实际的连接
 44             conn.connect();
 45             // 获取所有响应头字段
 46             Map<String, List<String>> map = conn.getHeaderFields();
 47             // 遍历所有的响应头字段
 48             for (String key : map.keySet()) {
 49                 System.out.println(key + "--->" + map.get(key));
 50             }
 51             // 定义BufferedReader输入流来读取URL的响应
 52             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
 53             String line;
 54             while ((line = in.readLine()) != null) {
 55                 result += "\n" + line;
 56             }
 57         } catch (Exception e) {
 58             System.out.println("发送GET请求出现异常!" + e);
 59             e.printStackTrace();
 60         }
 61         // 使用finally块来关闭输入流
 62         finally {
 63             try {
 64                 if (in != null) {
 65                     in.close();
 66                 }
 67             } catch (IOException ex) {
 68                 ex.printStackTrace();
 69             }
 70         }
 71         return result;
 72     }
 73 
 74     /**
 75      * 向指定URL发送POST方法的请求
 76      * 
 77      * @param url
 78      *            发送请求的URL
 79      * @param param
 80      *            请求参数,请求参数应该是name1=value1&name2=value2的形式或者是json。
 81      * @return URL所代表远程资源的响应
 82      */
 83     public static String sendPost(String url, String param) {
 84         PrintWriter out = null;
 85         BufferedReader in = null;
 86         String result = "";
 87         try {
 88             URL realUrl = new URL(url);
 89             // 打开和URL之间的连接
 90             URLConnection conn = realUrl.openConnection();
 91             // 设置通用的请求属性
 92             conn.setRequestProperty("accept", "*/*");
 93             conn.setRequestProperty("connection", "Keep-Alive");
 94             conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
 95             conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
 96             // 发送POST请求必须设置如下两行
 97             conn.setDoOutput(true);
 98             conn.setDoInput(true);
 99             // 获取URLConnection对象对应的输出流
100             out = new PrintWriter(conn.getOutputStream());
101             // 发送请求参数
102             out.print(param);
103             // flush输出流的缓冲
104             out.flush();
105             // 定义BufferedReader输入流来读取URL的响应
106             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
107             String line;
108             while ((line = in.readLine()) != null) {
109                 result += "\n" + line;
110             }
111         } catch (Exception e) {
112             System.out.println("发送POST请求出现异常!" + e);
113             e.printStackTrace();
114         }
115         // 使用finally块来关闭输出流、输入流
116         finally {
117             try {
118                 if (out != null) {
119                     out.close();
120                 }
121                 if (in != null) {
122                     in.close();
123                 }
124             } catch (IOException ex) {
125                 ex.printStackTrace();
126             }
127         }
128         return result;
129     }
130 
131     // 调用天气预报接口请求地址
132     String getUrl = "http://api.ip138.com/weather";
133     // 调用天气预报接口请求参数
134     String getParams = "&code=350229" + "&callback=find" + "&type=1"
135             + "&token=cc87f3c77747bccbaaee35006da1ebb65e0bad57";
136 
137     // 调用天气预报接口请求参数方式一
138     String postUrl = "http://op.juhe.cn/onebox/weather/query";
139     String postParamsOne = "&cityname=上海市" + "&key=1234567890";
140     // 调用天气预报接口请求参数方式二
141     String postParamsTwo = "{'cityname':'上海市'," + "'key':'1234567890'}";
142     JSONObject jsonPostParamsTwo = JSONObject.fromObject(postParamsTwo);
143     
144     // 提供主方法,测试发送GET请求和POST请求
145     @Test
146     public void test() {
147         // 发送GET请求
148         String getResult = HttpInterfaceTest.sendGet(getUrl, getParams);
149         System.out.println("GET请求参数:" + postParamsOne);
150         System.out.println("GET请求响应结果:" + getResult);
151         // 发送POST请求
152         String postResultOne = HttpInterfaceTest.sendPost(postUrl, postParamsOne);
153         System.out.println("POST请求参数一:" + postParamsOne);
154         System.out.println("POST请求响应结果:" + postResultOne);
155         // 发送POST请求
156         String postResultTwo = HttpInterfaceTest.sendPost(postUrl, jsonPostParamsTwo.toString());
157         System.out.println("POST请求参数二:" + jsonPostParamsTwo);
158         System.out.println("POST请求响应结果:" + postResultTwo);
159     }
160 }

 

 测试结果如下

 

Post+Get方式接口测试代码编写