日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#操作HttpClient工具类库

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

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請求失敗,請檢查網絡或聯系管理員查看服務器狀態,錯誤消息:" + e.Message);

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

? ? ? ? ? ? }

? ? ? ? ? ? return data;//接口調用成功獲取的數據

? ? ? ? }


? ? ? ? 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的內容標頭

? ? ? ? ? ? ? ? ? ? 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請求失敗,請檢查網絡或聯系管理員查看服務器狀態,錯誤消息:" + e.Message);

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

? ? ? ? ? ? }



? ? ? ? ? ? return data;//接口調用成功數據

? ? ? ? }


? ? ? ? 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 請求失敗,請檢查網絡或聯系管理員查看服務器狀態,錯誤消息:" + e.Message);

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

? ? ? ? ? ? }

? ? ? ? ? ??

? ? ? ? ? ? return data;

? ? ? ? }


? ? }

}


總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。