2013-04-04 52 views
1

我有一個C#.net Web應用程序。我試圖從後一個應用程序二進制數據到另一個使用此代碼如何檢索發佈的數據(以字節[]格式)?

string url = "path to send the data"; 

    string result=null; 
    string postData = "This is a test that posts this string to a Web server."; 
    byte[] fileData = Encoding.UTF8.GetBytes (postData); 

    // Create a request using a URL that can receive a post. 
    WebRequest request = WebRequest.Create (url); 
    // Set the Method property of the request to POST. 
    request.Method = "POST"; 
    // Create POST data and convert it to a byte array.  

    // Set the ContentType property of the WebRequest. 
    request.ContentType = "multipart/form-data"; 
    // Set the ContentLength property of the WebRequest. 
    request.ContentLength = fileData.Length; 
    // Get the request stream. 
    Stream dataStream = request.GetRequestStream(); 
    // Write the data to the request stream. 
    dataStream.Write (fileData, 0, fileData.Length); 
    // Close the Stream object. 
    dataStream.Close(); 
    // Get the response. 
    WebResponse response = request.GetResponse(); 
    // Display the status. 

    result = ((HttpWebResponse)response).StatusDescription; 
    // Get the stream containing content returned by the server. 
    dataStream = response.GetResponseStream(); 
    // Open the stream using a StreamReader for easy access. 
    StreamReader reader = new StreamReader (dataStream); 
    // Read the content. 
    string responseFromServer = reader.ReadToEnd(); 
    // Display the content. 

    result = result + responseFromServer; 
    // Clean up the streams. 
    reader.Close(); 
    dataStream.Close(); 
    response.Close(); 

通過上面的代碼,我送byte[]到第二個應用程序。如何在第二個應用程序中檢索發佈的數據(格式爲byte[])?

+0

什麼樣的應用呢? – 2013-04-04 10:29:02

+0

其網絡應用程序 – Sudha 2013-04-04 10:37:51

回答

1

注意:我假設您正在詢問如何檢索第二個應用程序中發佈的數據,並且您還可以訪問第二個應用程序的代碼。

然後,如果它是一個Web窗體應用程序,然後只需在Page_Load事件中你可以得到的文件名和文件本身:

string strFileName = Request.Files[0].FileName; 
HttpPostedFileBase filesToSave = Request.Files[0]; 

如果這不是要求,然後編輯你的問題,並添加更多的細節。

+0

謝謝gaurav。從哪個名稱空間我可以獲得StringUtility類?我需要添加任何DLL和從哪裏? – Sudha 2013-04-04 11:49:20

+0

對不起我的壞..請檢查更新的答案。您可以簡單地訪問帶有索引的文件。當您在請求中發佈1個文件時,您可以使用Request.Files [0]來訪問它; – gaurav 2013-04-04 12:28:34

+0

當我使用這個代碼字符串strFileName = Request.Files [0] .FileName; HttpPostedFile filesToSave = Request.Files [0];出現錯誤'索引超出範圍。必須是非負數且小於集合的大小。 參數名稱:index' – Sudha 2013-04-04 12:37:48

1

編輯:更新的答案包括請求和服務器端。服務器端將Base64字符串轉換爲byte[]

如果您要發佈讀取到二進制數據的字節[],您必須將其轉換爲請求端的Base64字符串以發佈它。

客戶端/委託方:

byte[] byteData = ReadSomeData(); 

string postData = Convert.ToBase64String(byteData); 

然後在服務器端,使用HttpContext獲得來自請求屬性的InputStream。然後,您可以使用StreamReader及其ReadToEnd()方法讀取數據。然後,您將發佈的Base64字符串轉換爲byte[]

事情是這樣的:

string postData = string.Empty; 

using (StreamReader reader = new StreamReader(context.Request.InputStream)) 
{ 
    postData = inputStreamReader.ReadToEnd(); 
} 

byte[] data = Convert.FromBase64String(postData); 
+0

什麼可以是initialLength,offset和count? – Sudha 2013-04-09 04:59:22

+0

在reader.Read(緩衝區,偏移量,計數)緩衝區應該是char []對嗎? – Sudha 2013-04-09 05:02:16

+0

@Sudha - 我更新了答案,以便更具體,並且我更清楚地回答你的問題。 – dblood 2013-04-12 13:28:20

相關問題