小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

詳細(xì)GET和POST方法

 靜女奇姝 2016-05-13
Get是用來(lái)從服務(wù)器上獲得數(shù)據(jù),而Post是用來(lái)向服務(wù)器上傳遞數(shù)據(jù).

HTTP同步請(qǐng)求

        // get請(qǐng)求是查詢字符串直接跟在URL后面
        
//post是把消息體包含查詢字符串,滿足把大量數(shù)據(jù)傳遞給服務(wù)器

        
//發(fā)送請(qǐng)求,"url"為訪問(wèn)的地址
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("url");
        //ContentType為數(shù)據(jù)類型
        
//get請(qǐng)求ContentType為空,post請(qǐng)求ContentType為application/x-www-form-urlencoded
        req.ContentType = "";
        req.Method = "get";  //請(qǐng)求方法為get和post
        
//content消息體,get請(qǐng)求content為空,post請(qǐng)求為要傳遞的參數(shù),如“AcctID=1
        string content = "";
        req.ContentLength=content.Length;
        Stream s;

        s = req.GetRequestStream();//獲取請(qǐng)求數(shù)據(jù)

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

        sw.Write(content);

        sw.Close();

        //得到HTTP請(qǐng)求

       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異步請(qǐng)求
        //發(fā)送請(qǐng)求,"url"為訪問(wèn)的地址
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("url");
        //ContentType為數(shù)據(jù)類型
        
//get請(qǐng)求ContentType為空,post請(qǐng)求ContentType為application/x-www-form-urlencoded
        req.ContentType = "";
        req.Method = "get";  //請(qǐng)求方法為get和post
        
//content消息體,get請(qǐng)求content為空,post請(qǐng)求為要傳遞的參數(shù),如“AcctID=1
        string content = "";
        req.ContentLength=content.Length;
        Stream s;

        s = req.GetRequestStream();//獲取請(qǐng)求數(shù)據(jù)

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

        sw.Write(content);

        sw.Close();
        Handler h = new Handler();
        AsyncCallback callback = new AsyncCallback(h.Callback); //方法
        
//將請(qǐng)求對(duì)象作為狀態(tài)對(duì)象傳遞
        req.BeginGetResponse(callback, req);
回調(diào)函數(shù),用類來(lái)表示
    public class Handler
    {
        public void Callback(IAsyncResult ar)
        {
            //將Requeststate對(duì)象強(qiáng)制轉(zhuǎn)化為webRequest對(duì)象
            HttpWebRequest req = (HttpWebRequest)ar.AsyncState;

            //得到與這個(gè)請(qǐng)求相關(guān)的響應(yīng)對(duì)象
            HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(ar);

            //開(kāi)始從響應(yīng)流中讀取數(shù)據(jù)
            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);

        }
    }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多