Files
OSS.Tools/Http/OSS.Tools.Http/OssHttpRequest.cs
T
2022-06-30 21:44:58 +08:00

124 lines
3.4 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#region Copyright (C) 2016 Kevin (OSS开源系列) 公众号:osscore
/***************************************************************************
*   文件功能描述:Http请求 == 请求实体
*
*   创建人: Kevin
* 创建人Email:1985088337@qq.com
*
*****************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Net.Http;
namespace OSS.Tools.Http
{
/// <summary>
/// 请求实体
/// </summary>
public class OssHttpRequest
{
/// <summary>
/// 请求构造函数
/// </summary>
public OssHttpRequest()
{
}
/// <summary>
/// 请求构造函数
/// </summary>
/// <param name="reqUrl">请求地址</param>
public OssHttpRequest(string reqUrl)
{
address_url = reqUrl;
}
/// <summary>
/// 如果此值设置
/// </summary>
public string address_url { get; set; }
/// <summary>
/// 请求方式
/// </summary>
public HttpMethod http_method { get; set; } = HttpMethod.Get;
/// <summary>
/// 自定义内容实体
/// eg:当上传文件时,无法自定义内容
/// </summary>
public string custom_body { get; set; }
/// <summary>
/// 准备发送执行
/// </summary>
protected internal virtual void PrepareSend() //(HttpRequestMessage httpRequestMessage)
{
}
/// <summary>
/// 发送执行
/// </summary>
protected internal virtual void OnSending(HttpRequestMessage httpRequestMessage) //(HttpRequestMessage httpRequestMessage)
{
}
#region 请求的内容参数
internal List<FileParameter> FileParameters;
internal List<NameValuePair> FormParameters;
/// <summary>
/// 文件参数列表
/// </summary>
public IReadOnlyList<FileParameter> file_paras => FileParameters;
/// <summary>
/// 非文件参数列表
/// </summary>
public IReadOnlyList<NameValuePair> form_paras => FormParameters; // 兼容老版本,取值时默认赋值
#endregion
}
public static class OssHttpRequestExtension
{
/// <summary>
/// 添加文件
/// </summary>
/// <param name="req"></param>
/// <param name="file"></param>
public static OssHttpRequest AddFilePara(this OssHttpRequest req,FileParameter file)
{
if (req.FileParameters == null)
{
req.FileParameters = new List<FileParameter>();
}
req.FileParameters.Add(file);
return req;
}
/// <summary>
/// 添加表单参数
/// </summary>
/// <param name="req"></param>
/// <param name="name"></param>
/// <param name="value"></param>
public static OssHttpRequest AddFormPara(this OssHttpRequest req, string name, object value)
{
if (req.FormParameters == null)
{
req.FormParameters = new List<NameValuePair>();
}
req.FormParameters.Add(new NameValuePair(name, value));
return req;
}
}
}