gpt4 book ai didi

asp.net - Webclient 双向使用二进制文件

转载 作者:行者123 更新时间:2023-12-02 14:31:05 26 4
gpt4 key购买 nike

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

我可以上传二进制文件并下载二进制文件,但无法使用相同的请求/响应命令来执行此操作。这意味着,不同的网络客户端可以做到这一点。在这种情况下,它会丢失对服务器上设置的流的引用。

Webclient bidirectional binary example

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

.

服务器脚本

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

最佳答案

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

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

最终,http 是一种请求/响应协议(protocol) - 不过,您可以按顺序执行多个请求。如果您需要更大的灵 active ,我建议您查看网络套接字,它允许两端发送临时信息。

<小时/>

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

关于asp.net - Webclient 双向使用二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15981496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com