- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Webclient用于将指令文件(最多 1 兆字节)上传到服务器,并以二进制数据形式接收该操作指令集作为响应(最多 1 兆字节)。
我可以上传二进制文件并下载二进制文件,但无法使用相同的请求/响应命令来执行此操作。这意味着,不同的网络客户端可以做到这一点。在这种情况下,它会丢失对服务器上设置的流的引用。
如何在一个序列中写入和读取二进制数据?
.
服务器脚本
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/
我正在编写一个类库来在我无法控制的站点上执行操作。该网站正在接受表单帖子作为输入。 谁能告诉我这两种方法除了上传数据的形式之外是否有区别? System.Net.WebClient.Uploa
用于工作的代码。 有问题的网址是 https://yobit.net/api/3/info 它适用于 IE。它曾经与 webclient 一起使用。它现在在 webclient 中不起作用。我想知道问
因此,我将我的 WebClient 包装在一个 using 语句中。但是我突然想知道,如果我的对象实现了 IDisposable 并且包装在 using 语句中,我是否需要取消订阅事件? 下面是我当前
我正在 VS15 测试版中工作并尝试使用 WebClient。虽然 System.Net 被引用,并且智能感知建议 WebClient 类可用,但在构建时我收到以下错误: The type or na
我想知道是否可以将 cookie 从一个 Web 客户端复制到另一个 Web 客户端。 原因 我正在使用并行 Web 请求,它会在每个新线程上创建 Web 客户端的新实例。 问题 信息敏感,需要使用p
我正在尝试使用 WebClient,但它给我错误,所以我检查了几个论坛(包括这个),他们告诉我把它放在哪里 在文件的顶部: using System.Net 在我想使用 WebClient 的地方之后
我正在尝试使用 WebClient 实现以下场景。使用 RestTemplate 很简单,但我不能再这样做了。 伪java代码中Spring Controller 的相关部分: Mono t1 = w
我正在使用 Spring WebClient 调用休息服务。如下所述的 post 调用代码。 Mono response = client.post()
正在尝试使用 WebClient在 Blazor 项目中。 得到以下错误: 在 blazor.webassembly.js:1 WASM: System.Net.WebException: An ex
我正在使用 ASP.NET Core 并尝试将文件下载到绝对路径。 但我遇到的问题是文件总是被下载到项目目录,文件名本身得到整个路径的名称。 我的代码: string path = @"C:\User
我需要自动化涉及使用登录表单的网站的流程。我需要在登录页面之后的页面中捕获一些数据。 我知道如何对普通页面进行屏幕抓取,但不知道如何抓取安全站点背后的页面。 这可以通过 .NET WebClient
我正在尝试逐步下载一系列序列化数据。目标是从服务器发送一个大块,并在下载时在客户端对其进行部分处理。 我正在使用 System.Net.WebClient 类并将其 AllowReadStreamBu
我在 Windows 桌面应用程序上使用此代码来获取组合框的值,之后我需要选择哪个组合框将使用 JavaScript 使用新信息更新页面 private WebBrowser withEventsFi
我正在尝试通过 C# 代码获取网站的 HTML 源代码。当我使用 Windows 身份验证访问站点时,以下代码有效: using (WebClient client = new WebClient()
我只是使用WebClient.DownloadString(),但速度慢得惊人。最大速度为 40kbs 我尝试将 WebClient.Proxy 设置为 null,但这不起作用,而且我还没有达到最大互
为了利用新的 WebClient API,我在我的 Intellij 项目中包含了 spring-webflux。 dependencies { implementation 'org.spr
我已经开始使用 WebClient,并使用过滤器方法添加请求/响应日志记录: WebClient.builder() .baseUrl(properties.getEndpoint())
我正在使用 WebClient.DownloadFile 将图像下载到本地存储库,如下所示: WebClient myWC = new WebClient();
我尝试使用网络客户端非阻塞方法验证验证码响应。所以它的工作,但我需要我的方法返回 boolean 值而不是异常。我如何从订阅返回值? webClient
这个问题已经有答案了: What does a "Cannot find symbol" or "Cannot resolve symbol" error mean? (18 个回答) 已关闭 3 年
我是一名优秀的程序员,十分优秀!