网站开发要知道的事——关于post和get的区别

来源:互联网 发布:手机淘宝外卖 编辑:程序博客网 时间:2024/05/01 08:43

最简单的理解:

       GET的方式是把参数数据在地址栏中明文的形式发送,可以在浏览器的地址栏中输入网址的方式直接访问网页:例如:http://localhost/test.html?id=123;其中?id=123就是get数据,整个uri长度限制在1024个字。
       POST则不能直接在浏览器中输入地址访问,而是把数据放到http请求中,一般采用form表单隐藏域提交。浏览器地址栏不能够看到参数数据。例如:同样传递id=123,地址栏只能看到http://localhost/test.html,参数数据看不到。想看到post数据可以用一些抓包工具,如Wireshark等

以下是对这两个方式区别的具体概括:

1. GET是从服务器上获取数据,POST要求被请求服务器接受附在请求后面的数据常用于提交表单。(例如与银行交互时提交订单支付信息)
2. GET是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。POST是通过HTTPPOST机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
3. 对于GET方式,服务器端用Request.QueryString获取变量的值,对于POST方式,服务器端用Request.Form获取提交的数据。
4. GET传送的数据量较小,不能大于2KB。POST传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。
5. GET安全性非常低,POST安全性较高。但是执行效率却比POST方法好。

建议:
1、GET方式的安全性较POST方式要差些,包含机密信息的话,建议用POST数据提交方式;
2、在做数据查询时,建议用GET方式;而在做数据添加、修改或删除时,建议用POST方式;

关于接口调用的GET和POST方法

以下是C#的以GET方式调用接口:

  public static string SendAndGetInterfaceData(string strInterfaceUrl)    {        string strBackData = "";          //返回数据        HttpWebRequest req;        HttpWebResponse res = null;        Stream requestStream;        StreamReader sr = null;        //-----------------------------------------        //往接口发送数据        //-----------------------------------------        req = (HttpWebRequest)WebRequest.Create(strInterfaceUrl);        req.ReadWriteTimeout = 6000;                            //设置超时        req.Method = "GET";        req.ContentType = "application/x-www-form-urlencoded";        try        {            //------------------------------------------                        //接收接口数据                        //------------------------------------------                        res = (HttpWebResponse)req.GetResponse();            sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("utf-8"));            strBackData = sr.ReadToEnd();        }        catch (WebException ex)        {            //strBackData = ex.Message;            strBackData = "";            throw ex;        }        finally        {            if (sr != null)                sr.Close();            if (res != null)                res.Close();        }        return strBackData;    }

以下是C#的以POST方式调用接口:

public static string SendAndGetInterfaceDataByPost(string strInterfaceUrl, string strDataXml)    {        string strBackData;          //返回数据        byte[] byArrRequest;        HttpWebRequest req;        HttpWebResponse res = null;        Stream requestStream;        StreamReader sr = null;        DataTable dt;        Encoding gb2312 = Encoding.GetEncoding("utf-8");        //-----------------------------------------        //往接口发送数据        //-----------------------------------------        req = (HttpWebRequest)WebRequest.Create(strInterfaceUrl + "?" + strDataXml);        req.ReadWriteTimeout = 6000;                                    //设置超时        byArrRequest = gb2312.GetBytes(strDataXml);        req.Method = "POST";        req.ContentType = "application/x-www-form-urlencoded";        req.ContentLength = byArrRequest.Length;        try        {            requestStream = req.GetRequestStream();            requestStream.Write(byArrRequest, 0, byArrRequest.Length);            requestStream.Close();            //------------------------------------------            //接收接口数据            //------------------------------------------            res = (HttpWebResponse)req.GetResponse();            sr = new StreamReader(res.GetResponseStream(), gb2312);            strBackData = sr.ReadToEnd();        }        catch (WebException ex)        {            strBackData = ex.Message;            //strBackData = "";            throw ex;        }        finally        {            if (sr != null)                sr.Close();            if (res != null)                res.Close();        }        return strBackData;    }


 

 

原创粉丝点击