打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
JAVA通过HTTP访问:Post+Get方式
  1. public class TestGetPost {  
  2.     /** 
  3.      * 向指定URL发送GET方法的请求 
  4.      * @param url 发送请求的URL 
  5.      * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式。 
  6.      * @return URL所代表远程资源的响应 
  7.      */  
  8.   
  9.     public static String sendGet(String url, String param) {  
  10.         String result = "";  
  11.         BufferedReader in = null;  
  12.         try {  
  13.             String urlName = url + "?" + param;  
  14.             URL realUrl = new URL(urlName);  
  15.             //打开和URL之间的连接  
  16.             URLConnection conn = realUrl.openConnection();  
  17.             //设置通用的请求属性  
  18.             conn.setRequestProperty("accept""*/*");  
  19.             conn.setRequestProperty("connection""Keep-Alive");  
  20.             conn.setRequestProperty("user-agent",  
  21.                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");  
  22.             //建立实际的连接  
  23.             conn.connect();  
  24.             //获取所有响应头字段  
  25.             Map < String, List < String >> map = conn.getHeaderFields();  
  26.             //遍历所有的响应头字段  
  27.             for (String key: map.keySet()) {  
  28.                 System.out.println(key + "--->" + map.get(key));  
  29.             }  
  30.             //定义BufferedReader输入流来读取URL的响应  
  31.             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
  32.             String line;  
  33.             while ((line = in .readLine()) != null) {  
  34.                 result += "/n" + line;  
  35.             }  
  36.         } catch (Exception e) {  
  37.             System.out.println("发送GET请求出现异常!" + e);  
  38.             e.printStackTrace();  
  39.         }  
  40.         //使用finally块来关闭输入流  
  41.         finally {  
  42.             try {  
  43.                 if ( in != null) { in .close();  
  44.                 }  
  45.             } catch (IOException ex) {  
  46.                 ex.printStackTrace();  
  47.             }  
  48.         }  
  49.         return result;  
  50.     }  
  51.     /**  
  52.      * 向指定URL发送POST方法的请求  
  53.      * @param url 发送请求的URL  
  54.      * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式。  
  55.      * @return URL所代表远程资源的响应  
  56.      */  
  57.     public static String sendPost(String url, String param) {  
  58.         PrintWriter out = null;  
  59.         BufferedReader in = null;  
  60.         String result = "";  
  61.         try {  
  62.             URL realUrl = new URL(url);  
  63.             //打开和URL之间的连接  
  64.             URLConnection conn = realUrl.openConnection();  
  65.             //设置通用的请求属性  
  66.             conn.setRequestProperty("accept""*/*");  
  67.             conn.setRequestProperty("connection""Keep-Alive");  
  68.             conn.setRequestProperty("user-agent",  
  69.                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");  
  70.             //发送POST请求必须设置如下两行  
  71.             conn.setDoOutput(true);  
  72.             conn.setDoInput(true);  
  73.             //获取URLConnection对象对应的输出流  
  74.             out = new PrintWriter(conn.getOutputStream());  
  75.             //发送请求参数  
  76.             out.print(param);  
  77.             //flush输出流的缓冲  
  78.             out.flush();  
  79.             //定义BufferedReader输入流来读取URL的响应  
  80.             in = new BufferedReader(  
  81.                 new InputStreamReader(conn.getInputStream()));  
  82.             String line;  
  83.             while ((line = in .readLine()) != null) {  
  84.                 result += "/n" + line;  
  85.             }  
  86.         } catch (Exception e) {  
  87.             System.out.println("发送POST请求出现异常!" + e);  
  88.             e.printStackTrace();  
  89.         }  
  90.         //使用finally块来关闭输出流、输入流  
  91.         finally {  
  92.             try {  
  93.                 if (out != null) {  
  94.                     out.close();  
  95.                 }  
  96.                 if ( in != null) { in .close();  
  97.                 }  
  98.             } catch (IOException ex) {  
  99.                 ex.printStackTrace();  
  100.             }  
  101.         }  
  102.         return result;  
  103.     }  
  104.   
  105.   
  106.     //提供主方法,测试发送GET请求和POST请求  
  107.     public static void main(String args[]) {  
  108.         //发送GET请求  
  109.         String s = TestGetPost.sendGet("http://localhost:8888/abc/login.jsp"null);  
  110.         System.out.println(s);  
  111.         //发送POST请求  
  112.         String s1 = TestGetPost.sendPost(http: //localhost:8888/abc/a.jsp ,  
  113.             "user=李刚&pass=abc");  
  114.         System.out.println(s1);  
  115.     }  
  116. }  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java发送http的get、post请求
Java发送http get/post请求,调用接口/方法
java微信 客服接口
短信验证码登录的实现流程
java 发送post请求
Java爬虫的底层获取模块,构造POC和漏洞检测时常用爬虫抓取或发送测试代码查询目标站 ...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服