asp.net post任何資料類型流到網站–噹噹網API傳XML檔案

來源:互聯網
上載者:User
下面的代碼可以post任何類型檔案,主要是把資料流統一轉換為base64格式.//------------------------------發送部分--------------------------------------------------        string url = "http://localhost/im/upfile?aa=5";//發送到的頁面的地址         HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);         //讀取一個圖片         FileStream fs = new FileStream(Server.MapPath("~/NET基礎知識.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["aa"];         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();

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.