C# 如何检索发布的数据(字节[]格式)?

C# 如何检索发布的数据(字节[]格式)?,c#,web-applications,http-post,bytearray,C#,Web Applications,Http Post,Bytearray,我有一个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 (postDa

我有一个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[]
格式)?

注意:我假设您正在询问如何在第二个应用程序中检索发布的数据,并且您可以访问第二个应用程序的代码

然后,如果它是一个webform应用程序,那么只需在page_load事件上,您就可以获得文件名和文件本身,如下所示:

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

如果这不是要求,则编辑您的问题并添加更多详细信息。

编辑:更新答案,以包括请求和服务器端。服务器端将Base64字符串转换为
字节[]

如果您要发布读入字节[]的二进制数据,则必须在请求端将其转换为Base64字符串才能发布

客户/请求方:

byte[] byteData = ReadSomeData();

string postData = Convert.ToBase64String(byteData);
然后在服务器端,使用
HttpContext
从请求属性获取InputStream。然后可以使用
StreamReader
及其ReadToEnd()方法读入数据。然后将发布的Base64字符串转换为
字节[]

大概是这样的:

string postData = string.Empty;

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

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

它是什么样的应用程序?它是一个web应用程序。从哪个名称空间可以获得StringUtility类?我是否需要添加任何dll以及从何处添加?对不起,我的错误。。请检查更新的答案。您只需访问带有索引的文件即可。当您在请求中发布1个文件时,您可以使用
request.Files[0]
访问它;使用此代码字符串时,strFileName=Request.Files[0].FileName;HttpPostedFile filesToSave=Request.Files[0];“获取错误”索引超出范围。必须为非负数且小于集合的大小。参数名称:index'ohh。。当您按照@dblood的建议尝试从InputStream获取它时会发生什么?试试看,然后告诉我发生了什么。我使用了以下代码:HttpContext=HttpContext.Current;Stream=context.Request.InputStream;然后在流中,我得到了与上面代码发送的相同的长度字节[]。在这里,它是一个流对象。如何转换为字节[]?初始长度、偏移量和计数是多少?在reader.Read(缓冲区、偏移量、计数)中,缓冲区应为字符[]对???@Sudha-我更新了答案,使之更具体,并且我认为这更清楚地回答了您的问题。@dblood-当我尝试此代码时,出现了如下错误:“输入不是有效的Base-64字符串,因为它包含一个非Base-64字符、两个以上的填充字符或填充字符中的一个非空白字符。”数据=Convert.FromBase64String(postData);Line64在发布请求端的数据之前,您是否按照我在示例中显示的那样对其进行了编码?