java http get、post请求

  1 package com.zpark.test;
  2 import org.junit.Test;
  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 /**
 12  * @author ceshi
 13  * @Title: HttpTest
 14  * @Package test
 15  * @Description: HttpTest
 16  * @date 2018/5/3011:37
 17  */
 18 public class JunitHttpTest {
 19     
 20     @Test
 21     public void test(){
 22         //发送 GET 请求
 23         String getStr=sendGet("http://localhost:8888/Study/logdin", "userName=test&passorld=123456");
 24         System.out.println(getStr);
 25 
 26         //发送 POST 请求
 27         String postStr=sendPost("http://localhost:8888/Study/logdin", "userName=test&passorld=123456");
 28         System.out.println(postStr);
 29 
 30     }
 31     
 32     /**
 33      * 向指定URL发送GET方法的请求
 34      * @param url 发送请求的URL
 35      * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 36      * @return URL 所代表远程资源的响应结果
 37      */
 38     public static String sendGet(String url, String param) {
 39         String result = "";
 40         BufferedReader in = null;
 41         try {
 42             String urlNameString = url + "?" + param;
 43             URL realUrl = new URL(urlNameString);
 44             // 打开和URL之间的连接
 45             URLConnection connection = realUrl.openConnection();
 46             // 设置通用的请求属性
 47             connection.setRequestProperty("accept", "*/*");
 48             connection.setRequestProperty("connection", "Keep-Alive");
 49             connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 50             // 建立实际的连接
 51             connection.connect();
 52             // 获取所有响应头字段
 53             Map<String, List<String>> map = connection.getHeaderFields();
 54             // 遍历所有的响应头字段
 55             for (String key : map.keySet()) {
 56                 System.out.println(key + "--->" + map.get(key));
 57             }
 58             // 定义 BufferedReader输入流来读取URL的响应
 59             in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
 60             String line;
 61             while ((line = in.readLine()) != null) {
 62                 result += line;
 63             }
 64         } catch (Exception e) {
 65             e.printStackTrace();
 66         }
 67         // 使用finally块来关闭输入流
 68         finally {
 69             try {
 70                 if (in != null) {
 71                     in.close();
 72                 }
 73             } catch (Exception e2) {
 74                 e2.printStackTrace();
 75             }
 76         }
 77         return result;
 78     }
 79 
 80     /**
 81      * 向指定 URL 发送POST方法的请求
 82      * @param url 发送请求的 URL
 83      * @param param  请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 84      * @return 所代表远程资源的响应结果
 85      */
 86     public static String sendPost(String url, String param) {
 87         PrintWriter out = null;
 88         BufferedReader in = null;
 89         String result = "";
 90         try {
 91             URL realUrl = new URL(url);
 92             // 打开和URL之间的连接
 93             URLConnection conn = realUrl.openConnection();
 94             // 设置通用的请求属性
 95             conn.setRequestProperty("accept", "*/*");
 96             conn.setRequestProperty("connection", "Keep-Alive");
 97             conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 98             // 发送POST请求必须设置如下两行
 99             conn.setDoOutput(true);
100             conn.setDoInput(true);
101             // 获取URLConnection对象对应的输出流
102             out = new PrintWriter(conn.getOutputStream());
103             // 发送请求参数
104             out.print(param);
105             // flush输出流的缓冲
106             out.flush();
107             // 定义BufferedReader输入流来读取URL的响应
108             in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
109             String line;
110             while ((line = in.readLine()) != null) {
111                 result += line;
112             }
113         } catch (Exception e) {
114             e.printStackTrace();
115         }
116         //使用finally块来关闭输出流、输入流
117         finally{
118             try{
119                 if(out!=null){
120                     out.close();
121                 }
122                 if(in!=null){
123                     in.close();
124                 }
125             }
126             catch(IOException ex){
127                 ex.printStackTrace();
128             }
129         }
130         return result;
131     }
132 }

猜你喜欢

转载自www.cnblogs.com/qinxu/p/9110101.html