【整理】Asp.net HttpWebRequest和HttpWebResponse發送和接受任何類型資料

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   使用   sp   for   on   

【整理】Asp.net HttpWebRequest和HttpWebResponse發送和接受任何類型資料

發送資料

//當前頁面地址

            string currentUrl = Request.Url.ToString();

            string fileName = "複製檔案";

            string url = currentUrl.Substring(0, currentUrl.LastIndexOf(‘/‘)) + "/Default2.aspx?id=" + fileName;   //發送到的頁面的地址

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

 

            //讀取一個檔案

            FileStream fs = new FileStream(Server.MapPath("程式更新說明書.doc"), System.IO.FileMode.Open, System.IO.FileAccess.Read);

            byte[] filecontent = new byte[fs.Length];

            fs.Read(filecontent, 0, filecontent.Length);

            fs.Close();

            fs.Dispose();

 

            //將圖片轉換成base64編碼的流

            string a = Convert.ToBase64String(filecontent);

 

            //讀取base64編碼流,發送

            byte[] requestBytes = System.Text.Encoding.Default.GetBytes(a);

 

            req.Method = "POST";

            req.ContentType = "application/x-www-form-urlencoded";

            req.ContentLength = requestBytes.Length;

            Stream requestStream = req.GetRequestStream();

            requestStream.Write(requestBytes, 0, requestBytes.Length);

            requestStream.Close();

 

            //接收返回參數,到string backstr

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

            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);

            string backstr = sr.ReadToEnd();

            sr.Close();

            res.Close();

            //輸出參數

            Response.Write(backstr);

接受資料

//接收到的參數

            string bb = Request.QueryString["id"];

            Encoding myEncoding = Encoding.GetEncoding("utf-8");

 

            //接收傳遞過來的資料流

            Stream resStream = Request.InputStream;

 

            byte[] filecontent = new byte[resStream.Length];

            //將資料流讀入byte數組

            resStream.Read(filecontent, 0, filecontent.Length);

            //數群組轉換為string以便轉換base64使用

            string a = myEncoding.GetString(filecontent);

            //將string讀取base64解密到byte數組

            byte[] filecontent2 = Convert.FromBase64String(a);

            //寫入目錄

            File.WriteAllBytes(Server.MapPath(bb + ".doc"), filecontent2);

            //傳回值

            Response.Write("ok");

            Response.End();

【整理】Asp.net HttpWebRequest和HttpWebResponse發送和接受任何類型資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.