打开APP
userphoto
未登录

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

开通VIP
详细GET和POST方法
Get是用来从服务器上获得数据,而Post是用来向服务器上传递数据.

HTTP同步请求

        // get请求是查询字符串直接跟在URL后面
        
//post是把消息体包含查询字符串,满足把大量数据传递给服务器

        
//发送请求,"url"为访问的地址
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("url");
        //ContentType为数据类型
        
//get请求ContentType为空,post请求ContentType为application/x-www-form-urlencoded
        req.ContentType = "";
        req.Method = "get";  //请求方法为get和post
        
//content消息体,get请求content为空,post请求为要传递的参数,如“AcctID=1
        string content = "";
        req.ContentLength=content.Length;
        Stream s;

        s = req.GetRequestStream();//获取请求数据

        StreamWriter sw = new StreamWriter(s, System.Text.Encoding.ASCII);

        sw.Write(content);

        sw.Close();

        //得到HTTP请求

       HttpWebResponse  res = (HttpWebResponse)req.GetResponse();
       s = res.GetResponseStream();

       StreamReader sr = new StreamReader(s, System.Text.Encoding.ASCII);
       System.Text.StringBuilder sb = new System.Text.StringBuilder();

       char[] data = new char[1024];

       int nBytes;

       do
       {
           nBytes = sr.Read(data, 0, (int)1024);
           sb.Append(data);
       } while (nBytes == 1024);
HTTP异步请求
        //发送请求,"url"为访问的地址
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("url");
        //ContentType为数据类型
        
//get请求ContentType为空,post请求ContentType为application/x-www-form-urlencoded
        req.ContentType = "";
        req.Method = "get";  //请求方法为get和post
        
//content消息体,get请求content为空,post请求为要传递的参数,如“AcctID=1
        string content = "";
        req.ContentLength=content.Length;
        Stream s;

        s = req.GetRequestStream();//获取请求数据

        StreamWriter sw = new StreamWriter(s, System.Text.Encoding.ASCII);

        sw.Write(content);

        sw.Close();
        Handler h = new Handler();
        AsyncCallback callback = new AsyncCallback(h.Callback); //方法
        
//将请求对象作为状态对象传递
        req.BeginGetResponse(callback, req);
回调函数,用类来表示
    public class Handler
    {
        public void Callback(IAsyncResult ar)
        {
            //将Requeststate对象强制转化为webRequest对象
            HttpWebRequest req = (HttpWebRequest)ar.AsyncState;

            //得到与这个请求相关的响应对象
            HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(ar);

            //开始从响应流中读取数据
            Stream s = res.GetResponseStream();

            StreamReader sr = new StreamReader(s, System.Text.Encoding.ASCII);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            char[] data = new char[1024];

            int nBytes;

            do
            {
                nBytes = sr.Read(data, 0, (int)1024);
                sb.Append(data);
            } while (nBytes == 1024);

        }
    }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# 实现HTTP不同方法的请求示例
angularjs中$http模块POST请求request payload转form data
C#中HttpWebRequest的用法详解
Post方式调用wcf服务
ASP.NET POST方式提交数据
C# HttpWebRequest提交数据方式浅析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服