【问题标题】:How to retrieve the posted data (in byte[] format)?如何检索发布的数据(以 byte[] 格式)?
【发布时间】:2013-04-04 10:27:51
【问题描述】:

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

【问题讨论】:

  • 是什么样的应用程序?
  • 它是一个网络应用程序

标签: c# web-applications http-post bytearray


【解决方案1】:

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

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

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

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

【讨论】:

  • 谢谢你。我可以从哪个命名空间获得 StringUtility 类?我是否需要添加任何 dll 以及从哪里添加?
  • 对不起,我的错.. 请检查更新的答案。您可以简单地访问带有索引的文件。当您在请求中发布 1 个文件时,您可以使用 Request.Files[0] 访问它;
  • 当我使用这个代码字符串时 strFileName = Request.Files[0].FileName; HttpPostedFile filesToSave = Request.Files[0];得到一个错误'索引超出范围。必须是非负数且小于集合的大小。参数名称:index'
  • ohh.. 当您尝试按照@dblood 的建议从 InputStream 获取它时会发生什么?试试看,然后告诉你会发生什么。
  • 我用过这段代码:HttpContext context = HttpContext.Current;流流 = context.Request.InputStream;然后在流中,我得到了与上面代码发送的相同的长度字节 []。在这里,它作为 Stream obj 获得。我怎样才能转换成字节[]?
【解决方案2】:

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

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

客户端/请求方:

byte[] byteData = ReadSomeData();

string postData = Convert.ToBase64String(byteData);

然后在服务器端,使用HttpContext 从 Request 属性中获取 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);

【讨论】:

  • 初始长度、偏移量和计数可以是多少?
  • 在 reader.Read(buffer, offset, count) 中缓冲区应该是 char[] 对吧??
  • @Sudha - 我更新了更具体的答案,我认为更清楚地回答了你的问题。
  • @dblood- 当我尝试此代码时出现如下错误:'输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或非- 填充字符中的空白字符。 in byte[] data = Convert.FromBase64String(postData);行
  • 您是否按照我在示例中显示的那样在请求端对数据进行了 Base 64 编码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 2017-12-06
  • 1970-01-01
相关资源
最近更新 更多