{"id":113,"date":"2013-02-06T04:35:49","date_gmt":"2013-02-05T20:35:49","guid":{"rendered":"http:\/\/bohu.net\/blog\/?p=113"},"modified":"2023-02-11T00:59:22","modified_gmt":"2023-02-10T16:59:22","slug":"c-%e8%8e%b7%e5%8f%96http-headers-%e4%bb%a5%e5%8f%8a%e8%8e%b7%e5%8f%96%e9%a1%b5%e9%9d%a2title","status":"publish","type":"post","link":"https:\/\/as32.net\/blog\/113\/","title":{"rendered":"c# \u83b7\u53d6HTTP headers \u4ee5\u53ca\u83b7\u53d6\u9875\u9762title"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9546\" src=\"https:\/\/bohu.net\/blog\/wp-content\/uploads\/2013\/02\/csharplogo-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/as32.net\/blog\/wp-content\/uploads\/2013\/02\/csharplogo-300x300.png 300w, https:\/\/as32.net\/blog\/wp-content\/uploads\/2013\/02\/csharplogo-300x300-150x150.png 150w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/p>\n<p>\u5728\u5199\u4e00\u4e2a\u5de5\u5177, \u5176\u4e2d\u4e00\u4e2a\u529f\u80fd\u662f\u68c0\u6d4b\u6536\u96c6\u670d\u52a1\u5668\u7684\u4e00\u4e9b\u4fe1\u606f,\u00a0\u9700\u8981\u5728\u4e00\u4e2a\u64cd\u4f5c\u4e2d\u53d6\u5230HTTP headers \u4fe1\u606f\u548c \u9875\u9762\u7684\u6807\u9898(title).<\/p>\n<p>\u5c1d\u8bd5\u4e86WebRequest,WebResponse\u548c HttpWebRequest,HttpWebResponse. \u7814\u7a76\u4ed6\u4eec\u7684\u4e0d\u540c, \u6709Http\u7684\u63d0\u4f9b \u4e86\u5bf9\u65e0Http\u7c7b\u7684 HTTP \u7279\u5b9a\u65b9\u6cd5\u7684\u5b9e\u73b0. \u5173\u952e\u4f3c\u4e4e\u627e\u4e0d\u5230\u6211\u7684\u8981\u6c42&#8221;\u5728\u4e00\u6b21\u64cd\u4f5c\u4e2d&#8221;\u5b8c\u6210\u83b7\u53d6\u5230HTTP headers \u548c title.<\/p>\n<p>\u6700\u540e\u7a81\u7136\u770b\u5230\u4e86\u4e00\u6bb5\u56fd\u5916\u4ee3\u7801\u4e2d\u7528\u5230WebClient, \u867d\u7136\u529f\u80fd\u4e0d\u5c3d\u76f8\u540c, \u4f46\u53c8\u591a\u4e86\u4e00\u70b9\u601d\u8def.\u00a0 \u8fdb\u884c\u4e00\u7ffb\u6d4b\u8bd5, \u53d1\u73b0WebClient\u5341\u5206\u7b80\u4fbf\u597d\u7528, \u4ee3\u7801\u91cf\u4e5f\u9aa4\u51cf. \u867d\u7136\u6548\u7387\u53ef\u80fd\u4f1a\u7565\u6162\u4e00\u4e9b,\u4f46\u662f\u505a\u5230\u4e86\u4e00\u6b21\u8c03\u7528, \u53d6\u5230headers \u548c title.<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre class=\"brush:csharp\">\/\/\u65b9\u6cd5\u51fd\u6570\t\t\n\t\t\/\/\/ &lt;summary&gt;\n\t\t\/\/\/ \u8fd4\u56de HTTP headers.\n\t\t\/\/\/ &lt;\/summary&gt;\n\t\t\/\/\/ &lt;param name=\"Url\"&gt;\u5730\u5740&lt;\/param&gt;\n\t\t\/\/\/ &lt;returns&gt;headers\u7684\u5217\u8868&lt;\/returns&gt;\n\t\tpublic Dictionary&lt;string, string&gt; GetHTTPResponseHeaders(string url)\n\t\t{\n\t\t    Dictionary&lt;string, string&gt; HeaderList = new Dictionary&lt;string, string&gt;();\n\n            WebClient x = new WebClient();\n            x.Headers.Set(\"Timeout\", \"6000\"); \/\/\u8d85\u65f6\u8bbe\u7f6e6\u79d2\n            string source = x.DownloadString(url);\n\n            \/\/\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53d6\u9875\u9762\u6807\u9898\n            string title = Regex.Match(source, @\"&lt;titleb[^&gt;]*&gt;s*(?&lt;Title&gt;[sS]*?)&lt;\/title&gt;\", RegexOptions.IgnoreCase).Groups[\"Title\"].Value;\n            HeaderList.Add(\"Address\", url); \/\/\u52a0\u5165\u5730\u5740\n            HeaderList.Add(\"Title\", title); \/\/\u52a0\u5165\u9875\u9762\u6807\u9898\n\n            foreach (string HeaderKey in x.ResponseHeaders)\n                HeaderList.Add(HeaderKey, x.ResponseHeaders[HeaderKey]);\n\n\t\t    return HeaderList;\n\t\t}\n\n\/\/\u8c03\u7528\n\t\tvoid Button2Click(object sender, EventArgs e)\n\t\t{\n\t\t\tDictionary&lt;string, string&gt; Headers = GetHTTPResponseHeaders(\"http:\/\/www.bohu.cn\/\");\n\n\t\t\tforeach (string HeaderKey in Headers.Keys) \n\t\t\t    textBox5.Text += HeaderKey+\" : \"+Headers[HeaderKey]+\"rn\";\n\t\t}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5199\u4e00\u4e2a\u5de5\u5177, \u5176\u4e2d\u4e00\u4e2a\u529f\u80fd\u662f\u68c0\u6d4b\u6536\u96c6\u670d\u52a1\u5668\u7684\u4e00\u4e9b\u4fe1\u606f,\u00a0\u9700\u8981\u5728\u4e00\u4e2a\u64cd\u4f5c\u4e2d\u53d6\u5230HTTP headers \u4fe1\u606f\u548c &hellip; <a href=\"https:\/\/as32.net\/blog\/113\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cc# \u83b7\u53d6HTTP headers \u4ee5\u53ca\u83b7\u53d6\u9875\u9762title\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":136,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[65,66,98,178,188,189,341,459,587,596,599,616],"class_list":["post-113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-add-address","tag-add-title","tag-c","tag-groups-title","tag-http","tag-http-http","tag-set-timeout","tag-459","tag-587","tag-http-headers","tag-title","tag-616"],"_links":{"self":[{"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":1,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":9872,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions\/9872"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/as32.net\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}