日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C#操作HttpClient工具类库

發(fā)布時間:2023/12/10 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#操作HttpClient工具类库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

using System;

using System.Collections.Generic;

using System.Net.Http;


using System.Windows.Forms;


using System.Configuration;

using System.IO;

using Newtonsoft.Json;


namespace Dcflow

{

? ? public class HttpHelper

? ? {

? ? ? ? //獲取Configuration對象

? ? ? ??

? ? ? ? public static string DCFLOW_ZUUL = ConfigurationManager.AppSettings["SERVER_URL"];


? ? ? ? //token鍵

? ? ? ? public static string ACCESS_TOKEN_KEY = "";

? ? ? ? //token值

? ? ? ? public static string ACCESS_TOKEN_VALUE = "";


? ? ? ? private static Dictionary<string, string> headers;


? ? ? ? public static Dictionary<string, string> Headers { get => headers; set => headers = value; }


? ? ? ? private static readonly HttpClient _httpClient;


? ? ? ? static HttpHelper()

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //HttpClient熱身

? ? ? ? ? ? ? ? _httpClient = new HttpClient() { BaseAddress = new Uri(DCFLOW_ZUUL) };

? ? ? ? ? ? ? ? _httpClient.DefaultRequestHeaders.Connection.Add("keep-alive");

? ? ? ? ? ? ? ? _httpClient.SendAsync(new HttpRequestMessage

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? Method = new HttpMethod("HEAD"),

? ? ? ? ? ? ? ? ? ? RequestUri = new Uri(DCFLOW_ZUUL + "/")

? ? ? ? ? ? ? ? }).Result.EnsureSuccessStatusCode();

? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception)

? ? ? ? ? ? {

? ? ? ? ? ? ? ??

? ? ? ? ? ? }

? ? ? ? }


? ? ? ? public static String httpGet(string url)

? ? ? ? {

? ? ? ? ? ??

? ? ? ? ? ? if (HttpHelper.headers != null)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? _httpClient.DefaultRequestHeaders.Clear();

? ? ? ? ? ? ? ? headers[ACCESS_TOKEN_KEY] = HttpHelper.ACCESS_TOKEN_VALUE;

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? // 設置請求頭

? ? ? ? ? ? ? ? foreach (KeyValuePair<string, string> item in headers)

? ? ? ? ? ? ? ? {


? ? ? ? ? ? ? ? ? ? _httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);


? ? ? ? ? ? ? ? }

? ? ? ? ? ? }


? ? ? ? ? ? var data = "";

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? // response

? ? ? ? ? ? ? ? var response = _httpClient.GetAsync(new Uri(DCFLOW_ZUUL + url)).Result;

? ? ? ? ? ? ? ? data = response.Content.ReadAsStringAsync().Result;


? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception e)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? MessageBox.Show("HTTP GET請求失敗,請檢查網(wǎng)絡或聯(lián)系管理員查看服務器狀態(tài),錯誤消息:" + e.Message);

? ? ? ? ? ? ? ? //throw;

? ? ? ? ? ? }

? ? ? ? ? ? return data;//接口調(diào)用成功獲取的數(shù)據(jù)

? ? ? ? }


? ? ? ? public static String httpPost(string url, Dictionary<string, string> param, string dataType)

? ? ? ? {

? ? ? ? ? ??

? ? ? ? ? ? if (HttpHelper.headers != null)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? _httpClient.DefaultRequestHeaders.Clear();

? ? ? ? ? ? ? ? headers[ACCESS_TOKEN_KEY] = HttpHelper.ACCESS_TOKEN_VALUE;


? ? ? ? ? ? ? ? //設置請求頭

? ? ? ? ? ? ? ? foreach (KeyValuePair<string, string> item in headers)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? _httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }


? ? ? ? ? ? var data = "";

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? ByteArrayContent content = null;

? ? ? ? ? ? ? ? if (dataType.ToLower().Equals("json"))

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? content = new StringContent(JsonConvert.SerializeObject(param));

? ? ? ? ? ? ? ? ? ? //設置Http的內(nèi)容標頭

? ? ? ? ? ? ? ? ? ? content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? content = new FormUrlEncodedContent(param);

? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? // response

? ? ? ? ? ? ? ? var response = _httpClient.PostAsync(DCFLOW_ZUUL + url, content).Result;

? ? ? ? ? ? ? ? data = response.Content.ReadAsStringAsync().Result;


? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception e)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? MessageBox.Show("HTTP POST請求失敗,請檢查網(wǎng)絡或聯(lián)系管理員查看服務器狀態(tài),錯誤消息:" + e.Message);

? ? ? ? ? ? ? ? //throw;

? ? ? ? ? ? }



? ? ? ? ? ? return data;//接口調(diào)用成功數(shù)據(jù)

? ? ? ? }


? ? ? ? public static String httpUploadAsync(string url, List<string> filePath)

? ? ? ? {

? ? ? ? ? ? var data = "";

? ? ? ? ? ??

??

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? using (var content = new MultipartFormDataContent())

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? for (int i = 0; i < filePath.Count; i++)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? FileStream fs = File.OpenRead(filePath[i]);

? ? ? ? ? ? ? ? ? ? ? ? var streamContent = new StreamContent(fs);


? ? ? ? ? ? ? ? ? ? ? ? var imageContent = new ByteArrayContent(streamContent.ReadAsByteArrayAsync().Result);


? ? ? ? ? ? ? ? ? ? ? ? content.Add(imageContent, "upfile", Path.GetFileName(filePath[i]));

? ? ? ? ? ? ? ? ? ? ? ? fs.Close();

? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? // response

? ? ? ? ? ? ? ? ? ? var response = _httpClient.PostAsync(DCFLOW_ZUUL + url, content).Result;

? ? ? ? ? ? ? ? ? ? data = response.Content.ReadAsStringAsync().Result;

? ? ? ? ? ? ? ? };

? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception e)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? MessageBox.Show("HTTP POST MultipartFormData 請求失敗,請檢查網(wǎng)絡或聯(lián)系管理員查看服務器狀態(tài),錯誤消息:" + e.Message);

? ? ? ? ? ? ? ? //throw;

? ? ? ? ? ? }

? ? ? ? ? ??

? ? ? ? ? ? return data;

? ? ? ? }


? ? }

}


總結(jié)

以上是生活随笔為你收集整理的C#操作HttpClient工具类库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。