中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C# 通過post發(fā)送和接收數(shù)據(jù)的范例代碼

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
public partial class Post_Server : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
        string type = "";
        string Re = "";
        Re += "數(shù)據(jù)傳送方式:";
        if (Request.RequestType.ToUpper() == "POST")
        {
            type = "POST";
            Re += type + "<br/>參數(shù)分別是:<br/>";
            SortedList table = Param();
            if (table != null)
            {
                foreach (DictionaryEntry De in table) { Re += "參數(shù)名:" + De.Key + " 值:" + De.Value + "<br/>"; }
            }
            else
            { Re = "你沒有傳遞任何參數(shù)過來!"; }
        }
        else
        {
            type = "GET";
            Re += type + "<br/>參數(shù)分別是:<br/>";
            NameValueCollection nvc = GETInput();
            if (nvc.Count != 0)
            {
                for (int i = 0; i < nvc.Count; i++) { Re += "參數(shù)名:" + nvc.GetKey(i) + " 值:" + nvc.GetValues(i)[0] + "<br/>"; }
            }
            else
            { Re = "你沒有傳遞任何參數(shù)過來!"; }
        }
        Response.Write(Re);
 
    }
 
    //獲取GET返回來的數(shù)據(jù)
    private NameValueCollection GETInput()
    { return Request.QueryString; }
    // 獲取POST返回來的數(shù)據(jù)
    private string PostInput()
    {
        try
        {
            System.IO.Stream s = Request.InputStream;
            int count = 0;
            byte[] buffer = new byte[1024];
            StringBuilder builder = new StringBuilder();
            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();
            return builder.ToString();
        }
        catch (Exception ex)
        { throw ex; }
    }
    private SortedList Param()
    {
        string POSTStr = PostInput();
        SortedList SortList = new SortedList();
        int index = POSTStr.IndexOf("&");
        string[] Arr = { };
        if (index != -1) //參數(shù)傳遞不只一項(xiàng)
        {
            Arr = POSTStr.Split('&');
            for (int i = 0; i < Arr.Length; i++)
            {
                int equalindex = Arr[i].IndexOf('=');
                string paramN = Arr[i].Substring(0, equalindex);
                string paramV = Arr[i].Substring(equalindex + 1);
                if (!SortList.ContainsKey(paramN)) //避免用戶傳遞相同參數(shù)
                { SortList.Add(paramN, paramV); }
                else //如果有相同的,一直刪除取最后一個(gè)值為準(zhǔn)
                { SortList.Remove(paramN); SortList.Add(paramN, paramV); }
            }
        }
        else //參數(shù)少于或等于1項(xiàng)
        {
            int equalindex = POSTStr.IndexOf('=');
            if (equalindex != -1)
            { //參數(shù)是1項(xiàng)
                string paramN = POSTStr.Substring(0, equalindex);
                string paramV = POSTStr.Substring(equalindex + 1);
                SortList.Add(paramN, paramV);
 
            }
            else //沒有傳遞參數(shù)過來
            { SortList = null; }
        }
        return SortList;
    }
 
 
}
 
protected void Button1_Click(object sender, EventArgs e)
    {
        Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
        byte[] arrB = encode.GetBytes("aa=aa&bb=好飛");
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://localhost:11626/MyTest/Post_Server.aspx");
        myReq.Method = "POST";
        myReq.ContentType = "application/x-www-form-urlencoded";
        myReq.ContentLength = arrB.Length;
        Stream outStream = myReq.GetRequestStream();
        outStream.Write(arrB, 0, arrB.Length);
        outStream.Close();
 
 
        //接收HTTP做出的響應(yīng)
        WebResponse myResp = myReq.GetResponse();
        Stream ReceiveStream = myResp.GetResponseStream();
        StreamReader readStream = new StreamReader(ReceiveStream, encode);
        Char[] read = new Char[256];
        int count = readStream.Read(read, 0, 256);
        string str = null;
        while (count > 0)
        {
            str += new String(read, 0, count);
            count = readStream.Read(read, 0, 256);
        }
        readStream.Close();
        myResp.Close();
 
        Response.Write(str);
    }

標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:C#實(shí)現(xiàn)的算24點(diǎn)游戲的算法

下一篇:C#實(shí)現(xiàn)圖片生成縮略圖