C# HttpWebRequest 或 cURL 需要连续连接



我试图找出url和我的代码之间的连续连接。我每 500 米/秒以 json 格式将数据发送到 http。但是,当我检查WireShark时,连接正在断开连接并再次连接。我尝试了三个(httpwebrequest,cURL,webclient(,我想要的是它们提供连续的连接。现在,我只想让 cURL 做到这一点。以下是我尝试过的代码和来自wireshark的捕获,以使您更好地理解。

卷曲100米/秒功能

 static private void SystemStatusTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        string json = new JavaScriptSerializer().Serialize(new
        {
            cpuusage = CPU,
            availablememory = AVAILMEMORY,
            availableharddiskspace = AVAILABLEHD
        });
        Easy.WriteFunction wf = SystemStatusWF;    //response
        SystemStatusEasy.SetOpt(CURLoption.CURLOPT_URL, SYSTEMSTATUS_SERVER);
        SystemStatusEasy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, json);
        var resultOK = SystemStatusEasy.Perform();
        SystemStatusEasy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);  //response            
    }

主要功能

 static void Main()
    {            
        Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
        SystemStatusEasy = new Easy();
        var slistHeaders = new Slist();
        slistHeaders.Append("Content-Type: application/json");
        SystemStatusEasy.SetOpt(CURLoption.CURLOPT_VERBOSE, 1L);
        SystemStatusEasy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, slistHeaders);
        SystemStatusEasy.SetOpt(CURLoption.CURLOPT_CUSTOMREQUEST, "POST");
        SystemStatusEasy.SetOpt(CURLoption.CURLOPT_POST, true);
        SystemStatusEasy.SetOpt((CURLoption)213, 1L); //keep alive(not sure in c#)
        SystemStatusEasy.SetOpt(CURLoption.CURLOPT_TIMEOUT, false);
        SystemStatusEasy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, "cookie.txt");
        SystemStatusEasy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, "cookie.txt");
        systemStatusTimer = new Timer(500);
        systemStatusTimer.Elapsed += SystemStatusTimer_Elapsed;
        systemStatusTimer.Start();            
        Console.ReadKey();
    }

线鲨捕获

正如您在Wireshark Capture上看到的那样,它们通过fin/ack断开连接,然后通过Syn再次连接。我只想要程序启动时的一个 SYN,然后在进度结束时想要 FIN/ACK。任何帮助将不胜感激。

这就是我对HttpWebRequest所做的。

static HttpWebRequest httpWebRequest;
static void Main()
    {
        httpWebRequest = (HttpWebRequest)WebRequest.Create(SYSTEMSTATUS_SERVER); //
        systemStatusTimer = new Timer(500);
        systemStatusTimer.Elapsed += SystemStatusTimer_Elapsed;
        systemStatusTimer.Start();
        Console.ReadKey();
    }
static private void SystemStatusTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.KeepAlive = true;
        httpWebRequest.CookieContainer = cookieContainer;
        httpWebRequest.ServicePoint.SetTcpKeepAlive(true, 100000, 1000);
        Console.WriteLine(cookieContainer);
        Console.WriteLine("system Status 100m/s : {0}t{1}t{2}", CPU, AVAILABLEHD, AVAILMEMORY);
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = new JavaScriptSerializer().Serialize(new
            {
                cpuusage = CPU,                    
                availablememory = AVAILMEMORY,
                availableharddiskspace = AVAILABLEHD
            });
            streamWriter.Write(json);
        }
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            var result_header = httpResponse.Headers;
            Console.WriteLine("The response of sysStatus from server : " + result);              
        }
    }

最后是线鲨的结果。线鲨捕获

哦,好吧,它只提供一个 SYN 和一个 FIN/ACK。但是,我不断向服务器发送系统数据500m/s。为什么它给了我 FIN/ACK 并且之后没有发送数据?

相关内容

  • 没有找到相关文章

最新更新