用长连接处理HttpResponseMessage C#
Handling HttpResponseMessage with persistent connection c #
我用的是Httpclient C# .net
连接到 url.
我需要发出 GET 请求。这种联系是持久的。
问题是我需要根据服务器发送给我的通知来执行操作。
当执行 HttpResponseMessage
时,它挂起(持续工作并不断接收通知,但它永远不会让我能够根据服务器的通知工作)
取消执行时,我可以通过控制台看到所有通知。
有什么方法可以控制 HttpResponseMessage
能够处理服务器发送给我的每个响应吗?
我应该为此使用其他类型的技术吗?
这是我在取消操作时通过 console de "await response.Content.ReadAsStringAsync ();"
返回的内容。
<? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<message xmlns = "http://asdasd.com/asd/08/DS/Sync" xmlns: ns2 = "http: // asdadasdasdasd">
<event> KEEPALIVE </ event>
</ message>
<? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<message xmlns = "http://asdasd.com/asd/08/DS/Sync" xmlns: ns2 = "http: // asdadasdasdasd">
<event> KEEPALIVE </ event>
</ message>
<? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<message xmlns = "http://asdasd.com/asd/08/DS/Sync" xmlns: ns2 = "http: // asdadasdasdasd">
<event> KEEPALIVE </ event>
</ message>
每 ('message') 大约每 2 分钟到达一次 我希望每次得到一个我都可以执行一个操作
我留下 ultilizo 的代码。谢谢
public async static Task<int> GetRequest(string url)
{
HttpClient client = new HttpClient();
HttpResponseMessage response = new HttpResponseMessage();
response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
Console.WriteLine("STATUS OF CONNECTION");
Console.WriteLine(response.StatusCode);
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false); ;
Console.WriteLine("ANSWER");
Console.WriteLine(body);
Console.WriteLine(" \n");
Console.WriteLine("\n ATTENTION!! Disconnected from the Persistence line \n");
Console.WriteLine("Connections done");
int result1 = 1;
return result1;
}
根据 MSDN,您不必为每个请求创建一个新的 HttpClient
HttpClient is intended to be instantiated once and re-used throughout
the life of an application. Instantiating an HttpClient
class for
every request will exhaust the number of sockets available under heavy
loads.
接下来,您可以为 httpClient 指定默认超时时间或在每个请求中使用它。
那么,你使用的是ResponseHeadersRead
选项,意思是
The operation should complete as soon as a response is available and
headers are read. The content is not read yet.
尝试改用 ResponseContentRead
您希望解析部分 HTTP 响应,在收到每个部分时。 HTTP 协议不会 know/understand 您是 sending/receiving 单个 HTTP 响应中由换行符分隔的多个离散消息。
据我所知,HttpClient
对您帮助不大,因为它旨在接收一个完整的 HTTP 响应。 Rick Strahl 有一篇关于这个主题的博客 Using .NET HttpClient to capture partial Responses。
您应该能够使用 HttpWebRequest
手动将字节从网络流读取到缓冲区中。附加到缓冲区后,您需要检查它是否包含完整的消息 (<message> ... </ message>
)。如果是这样,将消息转换为字符串并根据需要宣布它:例如引发事件、启动任务、调用方法、添加到队列。然后清除缓冲区并重复。
TcpClient
可能不是这种情况下的最佳方法,因为您可能还需要实施 TLS(如果您的端点是 HTTPS)。
我用的是Httpclient C# .net 连接到 url.
我需要发出 GET 请求。这种联系是持久的。 问题是我需要根据服务器发送给我的通知来执行操作。
当执行 HttpResponseMessage
时,它挂起(持续工作并不断接收通知,但它永远不会让我能够根据服务器的通知工作)
取消执行时,我可以通过控制台看到所有通知。
有什么方法可以控制 HttpResponseMessage
能够处理服务器发送给我的每个响应吗?
我应该为此使用其他类型的技术吗?
这是我在取消操作时通过 console de "await response.Content.ReadAsStringAsync ();"
返回的内容。
<? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<message xmlns = "http://asdasd.com/asd/08/DS/Sync" xmlns: ns2 = "http: // asdadasdasdasd">
<event> KEEPALIVE </ event>
</ message>
<? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<message xmlns = "http://asdasd.com/asd/08/DS/Sync" xmlns: ns2 = "http: // asdadasdasdasd">
<event> KEEPALIVE </ event>
</ message>
<? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<message xmlns = "http://asdasd.com/asd/08/DS/Sync" xmlns: ns2 = "http: // asdadasdasdasd">
<event> KEEPALIVE </ event>
</ message>
每 ('message') 大约每 2 分钟到达一次 我希望每次得到一个我都可以执行一个操作 我留下 ultilizo 的代码。谢谢
public async static Task<int> GetRequest(string url)
{
HttpClient client = new HttpClient();
HttpResponseMessage response = new HttpResponseMessage();
response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
Console.WriteLine("STATUS OF CONNECTION");
Console.WriteLine(response.StatusCode);
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false); ;
Console.WriteLine("ANSWER");
Console.WriteLine(body);
Console.WriteLine(" \n");
Console.WriteLine("\n ATTENTION!! Disconnected from the Persistence line \n");
Console.WriteLine("Connections done");
int result1 = 1;
return result1;
}
根据 MSDN,您不必为每个请求创建一个新的 HttpClient
HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an
HttpClient
class for every request will exhaust the number of sockets available under heavy loads.
接下来,您可以为 httpClient 指定默认超时时间或在每个请求中使用它。
那么,你使用的是ResponseHeadersRead
选项,意思是
The operation should complete as soon as a response is available and headers are read. The content is not read yet.
尝试改用 ResponseContentRead
您希望解析部分 HTTP 响应,在收到每个部分时。 HTTP 协议不会 know/understand 您是 sending/receiving 单个 HTTP 响应中由换行符分隔的多个离散消息。
据我所知,HttpClient
对您帮助不大,因为它旨在接收一个完整的 HTTP 响应。 Rick Strahl 有一篇关于这个主题的博客 Using .NET HttpClient to capture partial Responses。
您应该能够使用 HttpWebRequest
手动将字节从网络流读取到缓冲区中。附加到缓冲区后,您需要检查它是否包含完整的消息 (<message> ... </ message>
)。如果是这样,将消息转换为字符串并根据需要宣布它:例如引发事件、启动任务、调用方法、添加到队列。然后清除缓冲区并重复。
TcpClient
可能不是这种情况下的最佳方法,因为您可能还需要实施 TLS(如果您的端点是 HTTPS)。