【问题标题】:Bidirectional use of Webclient for binary双向使用Webclient进行二进制
【发布时间】:2013-04-05 13:53:57
【问题描述】:

Webclient 用于将指令文件(最大 1 兆字节)上传到服务器,并以二进制数据的形式接收此操纵的指令集作为响应(最大 1 兆字节)。

我能够上传二进制文件和下载二进制文件,但我无法使用相同的请求/响应命令来完成。意味着,不是同一个 webclient 可以做到这一点。在这种情况下,它会丢失对服务器上设置的流的引用。

如何在一个序列中写入和读取二进制数据?

.

服务器脚本

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

            'Setup data reader
            If cRead Is Nothing Then cRead = New ReadDataFromContext
            cRead.Read(context) 'read data from 'context.Request.InputStream'


            If cWrite Is Nothing Then cWrite = New WriteDataToContext
            cWrite.Write(context) 'write data to 'context.Response.OutputStream'


        End Sub

.

客户端类

Partial Public Class MainPage
    Inherits UserControl
    Private WithEvents WCUpload As WebClient
    'Private WithEvents WCDownload As WebClient
    Private Stream As IO.Stream

    Dim U As New Uri("http://localhost:51001/communicator.ashx", UriKind.Absolute)


    Public Sub New()
        InitializeComponent()
        WCUpload = New WebClient
        'WCDownload = New WebClient

    End Sub

    Private Sub btnTest_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnTest.Click
        WCUpload.OpenWriteAsync(U)
    End Sub


    'This methode is never called if using WCUpload (on WCDownload it works but WCDownload has no more data)
    'Private Sub WC_OpenReadCompleted(sender As Object, e As System.Net.OpenReadCompletedEventArgs) Handles WCDownload.OpenReadCompleted
    '    Dim D(e.Result.Length - 1) As Byte
    '    e.Result.Read(D, 0, D.Length)
    '    Me.btnTest.Content = System.Text.Encoding.UTF8.GetString(D, 0, D.Length)
    'End Sub

    Private Sub WC_OpenWriteCompleted(sender As Object, e As System.Net.OpenWriteCompletedEventArgs) Handles WCUpload.OpenWriteCompleted
        Me.Stream = e.Result

        Dim D() As Byte = System.Text.Encoding.UTF8.GetBytes("Hallo Timo")
        Me.Stream.Write(D, 0, D.Length)
        Me.Stream.Close()
    End Sub

    Private Sub WC_WriteStreamClosed(sender As Object, e As System.Net.WriteStreamClosedEventArgs) Handles WCUpload.WriteStreamClosed
        'WC.OpenReadAsync(U)

        'WCDownload.OpenReadAsync(U)

        Me.Stream.Position = 0 '<<--- ERROR, cannot access to disposed object
        Dim D(Me.Stream.Length - 1) As Byte
        Me.Stream.Read(D, 0, D.Length)
        Me.btnTest.Content = System.Text.Encoding.UTF8.GetString(D, 0, D.Length)

    End Sub

    Public Sub PushData(ByVal StreamIn As IO.Stream, ByVal StreamOut As IO.Stream)
        Dim Buffer(4096 - 1) As Byte
        Dim BytesRead As Integer
        On Error Resume Next

        'RaiseEvent Progress(Me, 0)

        Do
            BytesRead = StreamIn.Read(Buffer, 0, Buffer.Length)
            If BytesRead <= 0 Then Exit Do
            StreamOut.Write(Buffer, 0, BytesRead)
            'RaiseEvent Progress(Me, StreamOut.Length / StreamIn.Length * 99) 'max 99 to raise the event with 100%
        Loop

        'RaiseEvent Progress(Me, 100)
    End Sub
End Class

【问题讨论】:

  • 当您说“相同”的请求/响应时 - 您是否希望前后发送一系列消息?还是……?
  • 在服务器端我需要“操作”数据。而不是在服务器上创建我需要用客户端下载的新文件。只在内存而不是文件系统中工作。也许我描述得不好,您可以解释如何在不使用文件系统下载方法将操作文件返回客户端的情况下工作。只需请求/响应而不更改 url。
  • "download" 与文件毫无关系。如果您使用UploadData 发送byte[] 并获取byte[]。这不是你需要的一切吗?
  • 我就是一切。但是,如果我将字节 [] 从客户端发送到服务器,则 webclient 类现在无法从服务器下载任何内容。唯一的方法是创建任何第二个 Web 客户端。但是任何第二个 Web 客户端都无法处理来自第一个 Web 客户端的第一个请求。
  • 我会编辑我的答案来解释。

标签: asp.net .net silverlight httpwebrequest webclient


【解决方案1】:

在我看来,您只是在寻找UploadData 方法,该方法将byte[] 请求主体发送到服务器,并将byte[] 响应主体返回给客户端。简单如:

byte[] req = ...;
byte[] resp;
using(var client = new WebClient()) {
    resp = client.UploadData(address, req);
}

归根结底,http 是一种请求/响应协议——不过,您可以按顺序执行多个请求。如果您需要更大的灵活性,我建议您查看允许两端临时发送的 web-sockets。


对于 silverlight 的异步使用,请尝试 PCL 版本的 HttpClient - 类似的 api,但比 silverlight 提供的开箱即用的实现更完整:http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone.aspx

【讨论】:

  • 你能解释一下你在哪里找到了 UploadData 吗?以及如何管理这种异步?
  • @Nasenbaer: msdn.microsoft.com/en-GB/library/…(以及 .NET 4.5 中的 UploadDataAsync。)
猜你喜欢
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 2014-02-28
  • 2011-03-29
相关资源
最近更新 更多