2023年4月3日 星期一

[研究][ASP.NET]用HttpClient取得回傳網頁Header內容

[研究][ASP.NET]用HttpClient取得回傳網頁Header內容

2023-04-03

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

********************************************************************************

Default.aspx

<%@ Page Async="true" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Net.Http;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected async void Page_Load(object sender, EventArgs e)
        {
            using (var httpClient = new HttpClient())
            {
                var request = new HttpRequestMessage(HttpMethod.Head, "https://www.example.com");
                HttpResponseMessage response = await httpClient.SendAsync(request);
                //string serverHeader = response.Headers.Server.ToString();
                foreach (var header in response.Headers)
                {
                    Label1.Text = Label1.Text + "<br />" + header.Key + ": ";
                    foreach (var headvalue in header.Value)
                    {
                        //Console.WriteLine("{0}: {1}", header.Key, string.Join(",", header.Value));
                        Label1.Text = Label1.Text + headvalue + ";";
                    }
                }
            }
        }
    }
}

結果

Age: 567867;

X-Cache: HIT;

Accept-Ranges: bytes;

Cache-Control: max-age=604800;

Date: Mon, 03 Apr 2023 07:25:55 GMT;

ETag: "3147526947";

Server: ECS;(oxr/8310);

****************************************

https://www.hinet.net/  測試結果

Connection: keep-alive;

x-request-id: 3c28f1466a8f21f7b277e6e1ada22fd5;

X-Cache: RULE;

Date: Mon, 03 Apr 2023 07:28:48 GMT;

Server: HiNetCDN/2211;

(完)

相關

沒有留言:

張貼留言