Http网络请求帮助类

/// <summary>
/// Http网络请求帮助类
/// </summary>
public class HttpClientHelper
{
private static HttpClientHelper _httpClientHelper;

public static HttpClientHelper _
{
get => _httpClientHelper ?? (_httpClientHelper = new HttpClientHelper());
set => _httpClientHelper = value;
}

/// <summary>
/// 向目标地址提交图片文件参数数据
/// </summary>
/// <param name="requestUrl">请求地址</param>
/// <param name="bmpBytes">图片字节流</param>
/// <param name="imgType">上传图片类型</param>
/// <param name="fileName">图片名称</param>
/// <returns></returns>
public string HttpClientPost(string requestUrl, byte[] bmpBytes, string imgType, string fileName)
{
using (var httpClient = new HttpClient())
{
List<ByteArrayContent> byteArrayContents = new List<ByteArrayContent>();

var imgTypeContent = new ByteArrayContent(Encoding.UTF8.GetBytes(imgType));
imgTypeContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "imgType"
};
byteArrayContents.Add(imgTypeContent);

var fileContent = new ByteArrayContent(bmpBytes);//填充图片文件二进制字节
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "filename",
FileName = fileName
};
byteArrayContents.Add(fileContent);

var content = new MultipartFormDataContent();
//将ByteArrayContent集合加入到MultipartFormDataContent中
foreach (var byteArrayContent in byteArrayContents)
{
content.Add(byteArrayContent);
}

try
{
var result = httpClient.PostAsync(requestUrl, content).Result;//post请求
return result.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
return ex.Message;
}
}
}

 


/// <summary>
/// 使用multipart/form-data方式上传文件及其他数据
/// </summary>
/// <param name="headers">请求头参数</param>
/// <param name="nameValueCollection">键值对参数</param>
/// <param name="fileCollection">文件参数:参数名,文件路径</param>
/// <returns>接口返回结果</returns>
public static string PostMultipartFormData(string url, Dictionary<string, string> headers, NameValueCollection nameValueCollection, NameValueCollection fileCollection)
{
using (var client = new HttpClient())
{
foreach (var item in headers)
{
client.DefaultRequestHeaders.Add(item.Key, item.Value);
}

using (var content = new MultipartFormDataContent())
{
// 键值对参数
string[] allKeys = nameValueCollection.AllKeys;
foreach (string key in allKeys)
{
var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes(nameValueCollection[key]));
dataContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = key
};
content.Add(dataContent);
}

//处理文件内容
string[] fileKeys = fileCollection.AllKeys;
foreach (string key in fileKeys)
{
byte[] bmpBytes = File.ReadAllBytes(fileCollection[key]);
var fileContent = new ByteArrayContent(bmpBytes);//填充文件字节
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = key,
FileName = Path.GetFileName(fileCollection[key])
};
content.Add(fileContent);
}

var result = client.PostAsync(url, content).Result;//post请求
string data = result.Content.ReadAsStringAsync().Result;
return data;//返回操作结果
}
}
}


}

posted @ 2022-06-27 16:59  ₯㎕~  阅读(59)  评论(0编辑  收藏  举报