C# 异步持久 WebClient 示例

发布于 2024-12-12 03:44:17 字数 1718 浏览 0 评论 0原文

我需要用 C# 创建一个简单的 http 客户端,它必须是异步的,并且必须支持与服务器的持久连接。所以我尝试使用 WebClient 类,但遇到了一些问题,我的代码是这样的:

void sendMessage()
{
  ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(bypassAllCertificateStuff);

  string loginRequest = @"{'IDENTIFIER':'patient1','PASSWORD':'asdasd','DEVICE_ID':'knt-01'}";

  client = new WebClient();         

  // add event handlers for completed and progress changed
  client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
  client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
  client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);

  // carry out the operation as normal

  client.UploadStringAsync(new Uri("Https://192.168.1.100/PaLogin"), "POST", loginRequest);
}

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
   Console.WriteLine("downloadProgressChanged");
}

void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
   // Console.WriteLine(e.ProgressPercentage);
   if (e.ProgressPercentage != 50)
   {
      Console.WriteLine("uploadProgressChanged");
   }
}

void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
    if (e.Result != null)
    {
       Console.WriteLine("Done");
    }
}

问题是我应该从服务器接收响应,但 client_UploadStringCompleted 和 client_DownloadProgressChanged 回调都没有被调用。 我在控制台上看到的唯一内容是: client_DownloadProgressChanged

所以基本上我想做的是:

1-我向服务器发送一些数据而不关闭连接 2-我收到服务器响应,但当我收到它时,连接必须仍然打开。

我缺少什么?

谢谢。 :-)

I need to make a simple http client in C# that must be asynchronous and must support a persistent connection to the server. So i'm trying to use the WebClient class, but i'm having some problems, my code is this:

void sendMessage()
{
  ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(bypassAllCertificateStuff);

  string loginRequest = @"{'IDENTIFIER':'patient1','PASSWORD':'asdasd','DEVICE_ID':'knt-01'}";

  client = new WebClient();         

  // add event handlers for completed and progress changed
  client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
  client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
  client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);

  // carry out the operation as normal

  client.UploadStringAsync(new Uri("Https://192.168.1.100/PaLogin"), "POST", loginRequest);
}

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
   Console.WriteLine("downloadProgressChanged");
}

void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
   // Console.WriteLine(e.ProgressPercentage);
   if (e.ProgressPercentage != 50)
   {
      Console.WriteLine("uploadProgressChanged");
   }
}

void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
    if (e.Result != null)
    {
       Console.WriteLine("Done");
    }
}

The problem is that i should receive a response from the server, but neither the client_UploadStringCompleted nor client_DownloadProgressChanged callbacks are ever called.
The only thing I see on the console is: client_DownloadProgressChanged

So basically what i'm trying to do is:

1- I send some data to the server without closing the connection
2- I receive the server response but the connection must still be open when i have received it.

What am I missing?

Thank you. :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹎☆浅夏丿初晴 2024-12-19 03:44:17

您在这里缺少整个 HTTP 协议。

HTTP 是一种无状态请求-响应协议。 HTTP 1.1 提供了可选指导方针,纯粹为了性能而保持连接打开 - 尽管对于请求响应范例而言,没有任何变化。 [但是我见过很多情况,客户端或服务器决定不尊重它并关闭连接。]它还提供分块编码以促进流式传输,但这就是 HTTP 的全部内容担心的。

所以基本上在 HTTP 中,客户端将等待回复(并保持连接打开),直到收到响应或超时。没有办法改变/改善这种行为。

现在,回到你的问题。
我认为连接到服务器时出现问题,因此您需要使用 Fiddler 来查看发生了什么情况。我的预感是它没有连接到服务器(防火墙、服务器关闭等),因为甚至没有调用证书检查。

You are missing the whole HTTP protocol here.

HTTP is a stateless request-response protocol. HTTP 1.1 provides optional guidelines for keeping connections open purely for the sake of performance - although as for the request response paradigm, there is no change. [Yet I have seen many cases where client or server have decided not to respect it and closed the connection.] It also provides chunked encoding to facilitate streaming, but that is all it is as far as HTTP is concerned.

So basically in HTTP, client will wait for a reply (and keep connection open) until it receives a response or timeout. There is no way to change/better this behaviour.

NOW, back to you problem.
I think something is going wrong with connecting to the server so you need to use Fiddler to see what is happening. My hunch is it does not connect to server (firewall, server down, etc) since the certificate check is not even called.

燃情 2024-12-19 03:44:17

Http服务器推送机制可以做到这一点。
看看这个:
http://en.wikipedia.org/wiki/Comet_(programming))

c#客户:

using (var client = new WebClient())
using (var reader = new StreamReader(client.OpenRead(uri), Encoding.UTF8, true))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

(che è quello che vi dicevo questo pomeriggio)

Http server push mechanism can do this.
See this:
http://en.wikipedia.org/wiki/Comet_(programming))

c# client:

using (var client = new WebClient())
using (var reader = new StreamReader(client.OpenRead(uri), Encoding.UTF8, true))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

(che è quello che vi dicevo questo pomeriggio)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文