HTTP请求的使用以及POST与GET的区别

来源:互联网 发布:苹果手机怎么看mac地址 编辑:程序博客网 时间:2024/05/22 03:17


        在软件开发中我们经常使用C/S设计模式,而在C/S模式下客户端与服务器之间的通信至关重要。
在这里我用URL类来实现通信,具体实现如下。

Java实现方法
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;class Http_Request {    /**     * 向指定URL发送GET方法的请求     *      * @param url     *            发送请求的URL     * @param param     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。     * @return URL 所代表远程资源的响应结果     */    public String sendGet(String url, String param) {        String result = "";        BufferedReader in = null;        try {            String urlNameString = url + "?" + param;            URL realUrl = new URL(urlNameString);            // 打开和URL之间的连接            URLConnection connection = realUrl.openConnection();            connection.connect();            // 定义 BufferedReader输入流来读取URL的响应            in = new BufferedReader(new InputStreamReader(                    connection.getInputStream()));            String line="";            while ((line = in.readLine()) != null) {                result += line;            }        } catch (Exception e) {            System.out.println("发送GET请求出现异常!" + e);            e.printStackTrace();        }        // 使用finally块来关闭输入流        finally {            try {                if (in != null) {                    in.close();                }            } catch (Exception ee) {                ee.printStackTrace();            }        }        return result;    }    /**     * 向指定 URL 发送POST方法的请求     *      * @param url     *            发送请求的 URL     * @param param     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。     * @return 所代表远程资源的响应结果     */    public String sendPost(String url, String param) {        PrintWriter out = null;        BufferedReader in = null;        String result = "";        try {            URL realUrl = new URL(url);            // 打开和URL之间的连接            URLConnection conn = realUrl.openConnection();            conn.setDoOutput(true);            conn.setDoInput(true);            // 获取URLConnection对象对应的输出流            out = new PrintWriter(conn.getOutputStream());            // 发送请求参数            out.print(param);            // flush输出流的缓冲            out.flush();            // 定义BufferedReader输入流来读取URL的响应            in = new BufferedReader(                    new InputStreamReader(conn.getInputStream()));            String line;            while ((line = in.readLine()) != null) {                result += line;            }        } catch (Exception e) {            System.out.println("发送 POST 请求出现异常!"+e);            e.printStackTrace();        }        //使用finally块来关闭输出流、输入流        finally{            try{                if(out!=null){                    out.close();                }                if(in!=null){                    in.close();                }            }catch(IOException ee){                ee.printStackTrace();            }        }        return result;    }    }public class HttpRequest {public static void main(String[] args) {Http_Request hr=new Http_Request();String str=hr.sendGet("http://localhost/test/test.php","name=holif");System.out.println(str);}}


Python实现方法
import urllib2req=urllib2.Request("http://localhost/test/test.php","name=holif")response=urllib2.urlopen(req)res=response.read()print res


作为例示,服务端的脚本返回的仅仅是一个字符串,在实际开发中通常以json或xml格式返回请求结果。


附:POST与GET的区别:
GET - 从指定的资源请求数据。
POST - 向指定的资源提交要被处理的数据。
 GETPOST后退按钮/刷新无害数据会被重新提交(浏览器应该告知用户数据会被重新提交)。书签可收藏为书签不可收藏为书签缓存能被缓存不能缓存
编码类型
application/x-www-form-urlencodedapplication/x-www-form-urlencoded 或 multipart/form-data。为二进制数据使用多重编码。历史参数保留在浏览器历史中。参数不会保存在浏览器历史中。
对数据长度的限制限制。当发送数据时,GET 方法向 URL 添加数据;URL 的长度是受限制的(URL 的最大长度是 2048 个字符)。
无限制。对数据类型的限制只允许 ASCII 字符。没有限制。也允许二进制数据。

安全性

与 POST 相比,GET 的安全性较差,因为所发送的数据是 URL 的一部分。在发送密码或其他敏感信息时绝不要使用 GET !


POST 比 GET 更安全,因为参数不会被保存在浏览器历史或 web 服务器日志中。可见性数据在 URL 中对所有人都是可见的。数据不会显示在 URL 中。
其他HTTP请求方法:
方法描述HEAD与 GET 相同,但只返回 HTTP 报头,不返回文档主体。PUT上传指定的 URI 表示。DELETE删除指定资源。OPTIONS返回服务器支持的 HTTP 方法。CONNECT把请求连接转换到透明的 TCP/IP 通道。

POST与GET的对比参考:W3SCHOOL 
0 0
原创粉丝点击