[fix]Config创建默认配置文件的开关Runtime.CreateConfigOnMissing,仅需对自动创建生效,而不应该阻止用户主动Save
智能大石头 编写于 2024-08-09 00:30:41 石头 提交于 2024-08-10 14:22:24
X
using System.Text;

namespace NewLife.Http;

public class FormUrlEncodedContent : ByteArrayContent
{
    public FormUrlEncodedContent(IEnumerable<KeyValuePair<String, String>> nameValueCollection)
        : base(GetContentByteArray(nameValueCollection)) => Headers.ContentType = "application/x-www-form-urlencoded";

    private static Byte[] GetContentByteArray(IEnumerable<KeyValuePair<String, String>> nameValueCollection)
    {
        //ArgumentNullException.ThrowIfNull(nameValueCollection, "nameValueCollection");
        var stringBuilder = new StringBuilder();
        foreach (var item in nameValueCollection)
        {
            if (stringBuilder.Length > 0)
            {
                stringBuilder.Append('&');
            }
            stringBuilder.Append(Encode(item.Key));
            stringBuilder.Append('=');
            stringBuilder.Append(Encode(item.Value));
        }
        return Encoding.Default.GetBytes(stringBuilder.ToString());
    }

    private static String Encode(String data) => String.IsNullOrEmpty(data) ? String.Empty : Uri.EscapeDataString(data).Replace("%20", "+");
}