简体   繁体   中英

How do I get protocol version from System.Web.HttpRequest?

I have an Nginx server reverse-proxying my IIS server running af .NET Runtime 4 Web Forms application.

I'm trying to find out what HTTP version (1.0 or 1.1) Nginx is using when making requests to the IIS.

How do I get the HTTP version from the current request?

I've tried searching and looking through the documentation. Closest I've found is the ProtocolVersion of System.Net.HttpWebRequest but HttpRequest doesn't have the same property and it seems I can't cast it.

(BTW: I realise that the HTTP version rarely matter these days but it's in relation to some debugging I'm doing)

该值位于Request.ServerVariables["SERVER_PROTOCOL"] ,例如, "HTTP/1.1"

I was trying to get and analyse extended information from various websites, the only sustainable solution I could find is to using Selenium, with following peace of could get any information, including protocol version: "H2", "HTTP/1.1" etc.

        ChromeOptions options = new ChromeOptions();

        //Following Logging preference helps in enabling the performance logs
        options.SetLoggingPreference("performance", LogLevel.All);

        //Creating Chrome driver instance
        IWebDriver driver = new ChromeDriver(options);
        driver.Navigate().GoToUrl(/*your url goes here*/);

        System.Threading.Thread.Sleep(20000);

        //Extracting the performance logs
        var logs = driver.Manage().Logs.GetLog("performance");
        string str = string.Empty;
        for (int i = 0; i < logs.Count; i++)
        {
            if (logs[i].Message.ToString().IndexOf("\"method\":\"Network.responseReceived\"") > -1 &&
                logs[i].Message.ToString().IndexOf("\"url\":") > -1)
            {
                MessageBox.Show(
                     str.Substring(str.IndexOf("\"protocol\":"), 
                     str.IndexOf(",", str.IndexOf("\"protocol\":")) - str.IndexOf("\"protocol\":"))      
                );
            }
        }

As you can see from following peace of response, you will be able to get pretty much any kind of information you could possibly need (including protocol version):

{"message":{"method":"Network.responseReceived","params":{"frameId":"755A984985C3F1263469B348C78A4AA5","loaderId":"2116D4C83A7C6EFD017CC5BC6814FCAB","requestId":"2116D4C83A7C6EFD017CC5BC6814FCAB","response":{"connectionId":41,"connectionReused":false,"encodedDataLength":9595,"fromDiskCache":false,"fromPrefetchCache":false,"fromServiceWorker":false,"headers":{"cache-control":"no-cache, must-revalidate", "content-encoding":"gzip" ,"content-language":"en","content-type":"text/html; charset=utf-8","date":"Sun, 19 Apr 2020 12:37:09 GMT","expires":"Sun, 19 Nov 1978 05:00:00 GMT","server":"nginx","status":"200","vary":"Accept-Encoding","x-content-type-options":"nosniff\\nnosniff","x-frame-options":"SAMEORIGIN","x-generator":"Drupal 7 ( http://drupal.org )","x-powered-by":"PleskLin"},"mimeType":"text/html", "protocol":"h2" ,"remoteIPAddress":"195.210.46.29","remotePort":443,"requestHeaders":{":authority":"www.WEBSITENAME.com",":method":"GET",":path":"/",":scheme":"https","accept":"text/html,app lication/xhtml+xml,application/xml;q=0.9,image/webp,image/apng, / ;q=0.8,application/signed-exchange;v=b3;q=0.9","accept-encoding":"gzip, deflate, br","accept-language":"en-US,en;q=0.9","sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"none","sec-fetch-user":"?1","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36"},"securityDetails":{"certificateId":0,"certificateTransparencyCompliance":"compliant","cipher":"AES_128_GCM","issuer":"Let's Encrypt Authority X3","keyExchange":"ECDHE_RSA","keyExchangeGroup":"P-256","protocol":"TLS 1.2","signedCertificateTimestampList":[{"hashAlgorithm":"SHA-256","logDescription":"Let's Encrypt 'Oak2020' log","logId":"E712F2B0377E1A62FB8EC90C6184F1EA7B37CB561D11265BF3E0F34BF241546E","origin":"Embedded in certificate","signatureAlgorithm":"ECDSA","signatureData":"30440220600B7A7DE2155D200AE2179CE5E297DC6AB9118E57934440C20E25E33C420ADC02201DC0B323CDCA6BF85100E4816B1405BA5BBB2F41EB225CABCBA4CB5C0513E449","status":"Verified","timestamp":1.586426567863e+12},{"hashAlgorithm":"SHA-256","logDescription":"Google 'Argon2020' log","logId":"B21E05CC8BA2CD8A204E8766F92BB98A2520676BDAFA70E7B249532DEF8B905E","origin":"Embedded in certificate","signatureAlgorithm":"ECDSA","signatureData":"3046022100AF2600 74C39A0F1294C8038BAEE0B85F984C7EC80D10203D6AAAC1BB8B5CDF1D022100ECE351015B9375A3F85CA84EC5CB606A5453AF34AFFDC25C5D32BC938A01FD67","status":"Verified","timestamp":1.586426567862e+12}],"subjectName":"WEBSITENAME.com","validFrom":1586422967,"validTo":1594198967},"securityState":"secure","status":200,"statusText":"","timing":{"connectEnd":399.532,"connectStart":163.118,"dnsEnd":163.118,"dnsStart":163.073,"proxyEnd":-1,"proxyStart":-1,"pushEnd":0,"pushStart":0,"receiveHeadersEnd":2137.345,"requestTime":51622.890236,"sendEnd":399.969,"sendStart":399.759,"sslEnd":399.526,"sslStart":281.492,"workerReady":-1,"workerStart":-1},"url":" https://www.WEBSITENAME.com/ "},"timestamp":51625.02969,"type":"Document"}},"webview":"755A984985C3F1263469B348C78A4AA5"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM