zoukankan      html  css  js  c++  java
  • Java实现post和get请求

    GET请求:GET请求会向服务器发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改、增加数据,不会影响资源的内容,即该请求不会产生副作用。无论进行多少次操作,结果都是一样的。

     1 /** 
     2      * 向指定URL发送GET方法的请求 
     3      *  
     4      */  
     5     public static String get(String url) {  
     6         BufferedReader in = null;  
     7         try {  
     8             URL realUrl = new URL(url);  
     9             // 打开和URL之间的连接  
    10             URLConnection connection = realUrl.openConnection();  
    11             // 设置通用的请求属性  
    12             connection.setRequestProperty("accept", "*/*");  
    13             connection.setRequestProperty("connection", "Keep-Alive");  
    14             connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
    15             connection.setConnectTimeout(5000);  
    16             connection.setReadTimeout(5000);  
    17             // 建立实际的连接  
    18             connection.connect();  
    19             // 定义 BufferedReader输入流来读取URL的响应  
    20             in = new BufferedReader(new InputStreamReader(connection.getInputStream()));  
    21             StringBuffer sb = new StringBuffer();  
    22             String line;  
    23             while ((line = in.readLine()) != null) {  
    24                 sb.append(line);  
    25             }  
    26             return sb.toString();  
    27         } catch (Exception e) {  
    28             LOG.error("Exception occur when send http get request!", e);  
    29         }  
    30         // 使用finally块来关闭输入流  
    31         finally {  
    32             try {  
    33                 if (in != null) {  
    34                     in.close();  
    35                 }  
    36             } catch (Exception e2) {  
    37                 e2.printStackTrace();  
    38             }  
    39         }  
    40         return null;  
    41     }  

    post请求:POST是向服务器端发送数据的,但是该请求会改变数据的种类等资源,就像数据库的insert操作一样,会创建新的内容。几乎目前所有的提交操作都是用POST请求的。

     1 /** 
     2      * 发送HttpPost请求 
     3      *  
     4      * @param strURL 
     5      *            服务地址 
     6      * @param params 
     7      *  
     8      * @return 成功:返回json字符串<br/> 
     9      */  
    10     public static String jsonPost(String strURL, Map<String, String> params) {  
    11         try {  
    12             URL url = new URL(strURL);// 创建连接  
    13             HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
    14             connection.setDoOutput(true);  
    15             connection.setDoInput(true);  
    16             connection.setUseCaches(false);  
    17             connection.setInstanceFollowRedirects(true);  
    18             connection.setRequestMethod("POST"); // 设置请求方式  
    19             connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式  
    20             connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式  
    21             connection.connect();  
    22             OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码  
    23             out.append(JSONUtil.object2JsonString(params));  
    24             out.flush();  
    25             out.close();  
    26   
    27             int code = connection.getResponseCode();  
    28             InputStream is = null;  
    29             if (code == 200) {  
    30                 is = connection.getInputStream();  
    31             } else {  
    32                 is = connection.getErrorStream();  
    33             }  
    34   
    35             // 读取响应  
    36             int length = (int) connection.getContentLength();// 获取长度  
    37             if (length != -1) {  
    38                 byte[] data = new byte[length];  
    39                 byte[] temp = new byte[512];  
    40                 int readLen = 0;  
    41                 int destPos = 0;  
    42                 while ((readLen = is.read(temp)) > 0) {  
    43                     System.arraycopy(temp, 0, data, destPos, readLen);  
    44                     destPos += readLen;  
    45                 }  
    46                 String result = new String(data, "UTF-8"); // utf-8编码  
    47                 return result;  
    48             }  
    49   
    50         } catch (IOException e) {  
    51             LOG.error("Exception occur when send http post request!", e);  
    52         }  
    53         return "error"; // 自定义错误信息  
    54     }  
  • 相关阅读:
    ecplise maven springmvc工程搭建
    【转载】钩子函数与回调函数
    【转载】Vue项目中的文件/文件夹命名规范
    联想本win10 virtualbox 安装centos
    【vue】router-link 与 router-view
    【vue】import的使用
    【vue】父组件主动调用子组件 /// 非父子组件传值
    【vue】vue组件的自定义事件
    修改MongoDB密码
    用du查看文件详情
  • 原文地址:https://www.cnblogs.com/chuanyueinlife/p/9109318.html
Copyright © 2011-2022 走看看