最近工作遇到的問題,

撰寫一支ashx擔任外網APP與內網NAS檔案串接的橋梁,

接收到NAS的Binary Stream後要再輸出給APP接收,

sample code如下 :

 

String NAS_API_URL = Utility.GetNASURL();
String requestQuery = context.Request.Url.Query;

if (!String.IsNullOrEmpty(NAS_API_URL) && !String.IsNullOrEmpty(requestQuery))
{
   String sendToNAS=NAS_API_URL+"OOOOO.cgi"+requestQuery;
   HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sendToNAS); //send to NAS Web API
   req.Method = "GET";
   using (var httpWebResponse = (HttpWebResponse)req.GetResponse())
   {
     var responseStream = httpWebResponse.GetResponseStream();//取得Response Stream

     var streamReader = new StreamReader(responseStream);
     var ms = new MemoryStream();
     streamReader.BaseStream.CopyTo(ms);//使用StreamReader寫到MemoryStream

     context.Response.BinaryWrite(ms.ToArray());//把Binary檔案回傳給Client接收
   }
}
創作者介紹
創作者 Kenny Lin 的頭像
Kenny Lin

Kenny的程式筆記

Kenny Lin 發表在 痞客邦 留言(0) 人氣()