using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WCSInterfaceWindowsService
{
public class HttpClientHelper
{
public static readonly HttpClientHelper _instance = new HttpClientHelper();
public HttpClientHelper() { }
public static HttpClientHelper GetInstance()
{
return _instance;
}
/// <summary>
/// 通过Get请求数据
/// </summary>
/// <returns></returns>
public static async Task<string> HttpGetMathod()
{
var httpClient = new HttpClient();
var url = "http://localhost:9000/index.html";
var response = await httpClient.GetAsync(url);
var result = await response.Content.ReadAsStringAsync();
return result;
}
/// <summary>
/// 通过Get下载文件
/// </summary>
/// <param name="filePath"></param>
public static async void HttpGetByDownLoadFileMathod(string filePath)
{
var httpClient = new HttpClient();
var response = await httpClient.GetAsync("http://localhost:9000/middledata.mp4");//763M
var fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
await response.Content.CopyToAsync(fileStream);
fileStream.Close();
}
/// <summary>
/// 通过Post请求数据: application/x-www-form-urlencoded
/// </summary>
/// <returns></returns>
public static async Task<string> HttpPostMathod()
{
var httpClient = new HttpClient();
var url = "http://192.168.0.9:9000/Demo/PostUrlCode";
var response = await httpClient.PostAsync(url, new FormUrlEncodedContent(new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("name","小明"),
new KeyValuePair<string, string>("age","20")
}));
var result = await response.Content.ReadAsStringAsync();
return result;
}
/// <summary>
/// 使用Post请求数据:application/json
/// </summary>
/// <returns></returns>
public static async Task<string> HttpPostByJsonMathod()
{
var httpClient = new HttpClient();
var url = "http://192.168.0.9:9000/Demo/PostUrlJson";
var response = await httpClient.PostAsync(
url,
new StringContent(
Newtonsoft.Json.JsonConvert.SerializeObject(new { Name = "小明", Id = 1 }),
Encoding.UTF8,
"application/json")
);
var result = await response.Content.ReadAsStringAsync();
return result;
}
/// <summary>
/// 通过Post上传文件:multipart/form-data
/// </summary>
public static async void HttpPostByUploadMathod()
{
var httpClient = new HttpClient();
var url = "http://localhost:9000/Demo/PostMulti";
var content = new MultipartFormDataContent();
content.Add(new StringContent("小明"), "name");
content.Add(new StringContent("18"), "age");
//注意:要指定filename,即:test.txt,否则后台不认为是一个文件,而是普通的参数
content.Add(new ByteArrayContent(System.IO.File.ReadAllBytes("e:\\test.txt")), "file", "test.txt");
var response = await httpClient.PostAsync(url, content);
var str = await response.Content.ReadAsStringAsync();
}
/// <summary>
/// 上传大文件
/// </summary>
public static async void HttpPostByUploadBigMathod()
{
var httpClient = new HttpClient();
httpClient.Timeout = Timeout.InfiniteTimeSpan;
var url = "http://192.168.0.9:9000/Demo/PostMulti2";
var content = new MultipartFormDataContent();
content.Add(new StringContent("小明"), "name");
content.Add(new StringContent("18"), "age");
var filepath = @"E:\BaiduNetdiskDownload\Docker实战.pdf";//97.6MB
filepath = @"E:\BaiduNetdiskDownload\dnSpy_v6.14.zip";//151.6MB
filepath = @"E:\BaiduNetdiskDownload\dotnetfx35.exe";//231MB
filepath = @"E:\BaiduNetdiskDownload\Photoshop_13_LS3(cs6)安装包.7z";//1.12GB
filepath = @"E:\BaiduNetdiskDownload\cn_windows_10_business_editions_version_2004_updated_june_2020_x64_dvd_49d8dbba.iso";//4.83GB
var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
var streamContent = new StreamContent(fileStream, 2048);
content.Add(streamContent, "file", "bigdata.db");
var response = await httpClient.PostAsync(url, content);
var str = await response.Content.ReadAsStringAsync();
}
}
}