怎么在HttpClient中添加默認(rèn)請(qǐng)求報(bào)頭?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、虛擬主機(jī)、營銷軟件、網(wǎng)站建設(shè)、濉溪網(wǎng)站維護(hù)、網(wǎng)站推廣。第一種方式
直接在創(chuàng)建的HttpClient對(duì)象的DefaultRequestHeaders集合中添加報(bào)頭。
class Program { static Task Main()=> SendAsync1(); private static async Task SendAsync1() { var httpClient = new HttpClient(); AddDefaultHeaders(httpClient); await httpClient.GetStringAsync("http://localhost:5000/"); } private static void AddDefaultHeaders(HttpClient httpClient) { httpClient.DefaultRequestHeaders.Add("x-www-foo", "123"); httpClient.DefaultRequestHeaders.Add("x-www-bar", "456"); httpClient.DefaultRequestHeaders.Add("x-www-baz", "789"); } }
第二種方式
對(duì)于.NET Core應(yīng)用來說,我們更推薦的做法是采用依賴注入的方式,利用IHttpClientFactory來創(chuàng)建HttpClient對(duì)象,那么我們?cè)谶M(jìn)行相關(guān)服務(wù)注冊(cè)的時(shí)候就可以設(shè)置默認(rèn)請(qǐng)求報(bào)頭。
class Program { static Task Main()=> SendAsync2(); private static async Task SendAsync2() { var services = new ServiceCollection(); services.AddHttpClient("", AddDefaultHeaders); var httpClient = services .BuildServiceProvider() .GetRequiredService<IHttpClientFactory>() .CreateClient(); await httpClient.GetStringAsync("http://localhost:5000/"); } private static void AddDefaultHeaders(HttpClient httpClient) { httpClient.DefaultRequestHeaders.Add("x-www-foo", "123"); httpClient.DefaultRequestHeaders.Add("x-www-bar", "456"); httpClient.DefaultRequestHeaders.Add("x-www-baz", "789"); } }
第三種方式
由于HttpClient在發(fā)送請(qǐng)求的時(shí)候會(huì)利用DiagnosticSource對(duì)象發(fā)送相應(yīng)的診斷事件,并且將作為請(qǐng)求的HttpRequestMessage對(duì)象作為請(qǐng)求事件內(nèi)容負(fù)載。我們可以訂閱該事件,在請(qǐng)求被發(fā)送之前將其攔截下來,并添加相應(yīng)的請(qǐng)求頭即可。
class Program { static Task Main()=> SendAsync3(); private static async Task SendAsync3() { Func<object, HttpRequestMessage> requestAccessor = null; DiagnosticListener.AllListeners.Subscribe(listener => { if (listener.Name == "HttpHandlerDiagnosticListener") { listener.Subscribe(kv => { if (kv.Key == "System.Net.Http.HttpRequestOut.Start") { requestAccessor ??= BuildRequestAccessor(kv.Value.GetType()); var request = requestAccessor(kv.Value); AddDefaultHeaders(request); } }); } }); var httpClient = new HttpClient(); await httpClient.GetStringAsync("http://localhost:5000/"); static Func<object, HttpRequestMessage> BuildRequestAccessor(Type payloadType) { var property = payloadType.GetProperty("Request", BindingFlags.Instance | BindingFlags.Public); var payload = Expression.Parameter(typeof(object)); var convertedPayload = Expression.Convert(payload, payloadType); var getRequest = Expression.Call(convertedPayload, property.GetMethod); var convert = Expression.Convert(getRequest, typeof(HttpRequestMessage)); return Expression.Lambda<Func<object, HttpRequestMessage>>(convert, payload).Compile(); } } private static void AddDefaultHeaders(HttpRequestMessage request) { request.Headers.Add("x-www-foo", "123"); request.Headers.Add("x-www-bar", "456"); request.Headers.Add("x-www-baz", "789"); } }
第四種方式
上面這種方式可以采用強(qiáng)類型編程方式,具體的代碼如下。
class Program { static Task Main()=> SendAsync4(); private static async Task SendAsync4() { DiagnosticListener.AllListeners.Subscribe(listener => { if (listener.Name == "HttpHandlerDiagnosticListener") { listener.SubscribeWithAdapter(new HttpClientListener()); } }); var httpClient = new HttpClient(); await httpClient.GetStringAsync("http://localhost:5000/"); } private sealed class HttpClientListener { [DiagnosticName("System.Net.Http.HttpRequestOut.Start")] public void OnSend(HttpRequestMessage request) => AddDefaultHeaders(request); //Must subscribute the System.Net.Http.HttpRequestOut event. [DiagnosticName("System.Net.Http.HttpRequestOut")] public void OnSend() { } } private static void AddDefaultHeaders(HttpRequestMessage request) { request.Headers.Add("x-www-foo", "123"); request.Headers.Add("x-www-bar", "456"); request.Headers.Add("x-www-baz", "789"); } }
看完上述內(nèi)容,你們掌握怎么在HttpClient中添加默認(rèn)請(qǐng)求報(bào)頭的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章標(biāo)題:怎么在HttpClient中添加默認(rèn)請(qǐng)求報(bào)頭-創(chuàng)新互聯(lián)
分享地址:http://vcdvsql.cn/article36/csissg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、外貿(mào)建站、小程序開發(fā)、域名注冊(cè)、網(wǎng)站維護(hù)、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)