C#操作HttpClient工具類庫

640?wx_fmt=jpeg

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;

? ? ? ? }

? ? }

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/272279.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/272279.shtml
英文地址,請注明出處:http://en.pswp.cn/news/272279.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

docker從倉庫找鏡像

docker search -s 數量 要下載的 數量表示倉庫start數

bzoj 1911: [Apio2010]特別行動隊 2011-12-26

1911: [Apio2010]特別行動隊 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 892 Solved: 359[Submit][Status][Discuss] DescriptionInputOutputSample Input4 -1 10 -20 2 2 3 4 Sample Output9HINT Source _________________________________________ 很簡單的動規方程&a…

嵌入式C語言基礎鏈表

什么是鏈表&#xff1f; 鏈表其實就是一種數據結構&#xff0c;所謂的數據結構就是數據存放的思想。 數組、鏈表優缺點&#xff1a; 增加一個元素或者刪除一個元素都很難&#xff0c;因為地址是連續的&#xff0c;刪除一個元素可能會挪動多個元素&#xff0c;不靈活。但是對于鏈…

docker pull 從倉庫拉取鏡像

docker pull 要拉取的鏡像名 等價于 docker pull 要拉取的鏡像名:lastest 拉取固定的鏡像&#xff1a;docker pull 要拉取的鏡像名:版本號 省略lastest表設計就是拉取的最新的

理解js中的原型鏈,prototype與__proto__的關系

說到prototype&#xff0c;就不得不先說下new的過程。 我們先看看這樣一段代碼&#xff1a; 1<script type"text/javascript">2 var Person function () { };3 var p new Person();4</script>很簡單的一段代碼&#xff0c;我們來看看這個new究竟做了什…

C#抓取網頁HTML內容

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Net; using System.Text; using System.IO; using System.Text.RegularExpressions; namespace Web { /// <summary> /// 公共方法類 /// </summary> p…

項目一感應垃圾桶(Wemos)

硬件材料&#xff1a; Wemos D1、SG90舵機、HC-SR04、杜邦線若干、蜂鳴器3.3V&#xff08;可有可無&#xff09; 軟件材料&#xff1a; arduino IDE編譯器、USB轉串口驅動 Wemos D1&#xff1a; 特性&#xff1a; 基于ESP-8266EX及arduino做的一個融合arduino兼容&#xff0…

docker刪除本地所有鏡像

docker rmi -f ${docker images -qa}

PAT1069. The Black Hole of Numbers

//這是到水題&#xff0c;之前因為四位數的原因一直不能A&#xff0c;看了別人的程序&#xff0c;才明白&#xff0c;不夠四位的時候沒考慮到&#xff0c;坑啊。。。。。臉打腫 #include<cstdio>#include<algorithm>using namespace std;int main(){ //freopen(&qu…

WiFi避障小車

硬件清單&#xff1a; Wemos D1&#xff08;支持AP模式也就是路由模式和STA模式也就是上網設備&#xff09;、超聲波模塊、小車、L9110s步進電機控制器 軟件&#xff1a; eclipse、arduino IDE WiFi配置參考博文 ESP8266WiFi庫: 從上圖中可以看出ESP8266WiFi庫主要包含Stati…

yum常用命令整理

yum命令的形式一般如下。要說明的是以下演示中所使用到的PACKAGE、GROUP都是變量&#xff0c;需要保證運行yum命令的主機能連接外網&#xff0c;否則大部分命令將由于沒有網絡連接而不能輸出結果。yum [options] [command] [package]#以下演示中大寫的單詞是變量1.安裝操作yum …

CSS3 2D 轉換

CSS3 2D 轉換 先看兼容性 transform屬性向應用元素應用2d 或者 3d裝換&#xff1b;該屬性允許我們進行旋轉&#xff0c;縮放&#xff0c;移動或者傾斜&#xff1b; 基本語法&#xff1a; transform: none|transform-functions;transform-function&#xff1a;這東東有n的函數可…

程序猿最喜歡說的30句話

雖然代碼總會有這個那個問題&#xff0c;但程序猿卻總有謎一般的從容和自信。上圖來自&#xff1a;《當程序出問題時程序員最喜歡說的30句話》來看看程序猿經常說的話&#xff1a;1、在我的電腦上是正常的啊。。。2、不可能出現這種情況的3、快了&#xff0c;已經完成了90%。4、…

linux環境下Ncurses實現貪吃蛇游戲

游戲說明&#xff1a; linux環境下基于Ncurses圖形庫的C語言小游戲。 Ncurses介紹&#xff1a; Ncurses(new curses)是一套編程庫&#xff0c;它提供了一系列的函數以便使用者調用它們去生成基于文本的用戶界面。 Ncurses是一個能提供功能鍵定義(快捷鍵),屏幕繪制以及基于文本…

韓順平循序漸進學java 第13講 抽象類.接口

13.1抽象類 13.1.1 概念 當父類的一些方法不能確定時&#xff0c;可以用abstract關鍵字來修飾該方法&#xff0c;稱為抽象方法&#xff0c;用abstract來修飾該類&#xff0c;稱為抽象類。 13.1.2 抽象類-深入討論 抽象類是java中一個比較重要的類&#xff1a; 1、用abstract關鍵…

C#實現簡體繁體轉換代碼示例

//簡體轉繁體 public static string _ConvertChinTrad(string strInput) { EncodeRobert edControl new EncodeRobert(); string strResult ""; if (strInput null) return strResult; if (strInput.ToString().Length > 1) strResult edControl.SCTCConvert(…

java基礎JDK的安裝和環境變量的配置

JRE和JDK&#xff1a; JRE是java程序運行時環境&#xff0c;包含JVM&#xff08;相當于java在不同操作系統上運行時java和操作系統之間的翻譯&#xff0c;保證java程序的跨平臺&#xff09;和運行時所需要的核心庫。所以我們想要運行一個已有的java程序&#xff0c;那么只需要…

C#通過SMTP發送郵件代碼示例

1、新建SMTP.cs類庫文件 public class SMTP { /// <summary> /// SMTP服務器 /// </summary> public string smtp { get; set; } /// <summary> /// SMTP服務器端口 /// </summary> public int port { get; set; } /// <summary> /// 發件人 ///…