2013-04-04 53 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

相关问题