C#HttpWebRequest或cURL想要连续连接

时间:2017-06-14 08:59:18

标签: c# curl httpwebrequest webclient

我试图找出url和我的代码之间的连续连接。我每500米/秒向json发送数据到http。但是当我检查WireShark时,连接断开并再次连接。我尝试了三个(httpwebrequest,cURL,webclient),我想要的是给他们连续的连接。现在,我只想要cURL这样做。以下是我尝试过的代码以及来自wireshark的代码,让您更好地理解。

卷曲 100m / s功能

 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

正如您在Wireshark Capture上看到的那样,它们被fin / ack断开连接,然后由Syn再次连接。我只想在程序启动时需要一个SYN,然后在进度结束时需要FIN / ACK。任何帮助都会得到赞赏。

1 个答案:

答案 0 :(得分:0)

所以这就是我用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);              
        }
    }

最后来自wireshark的结果。 wireshark capture

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