HttpClient Post 提交表单数据

2021-04-26 11:28

阅读:654

标签:new   cep   form   remote   证书认证   write   headers   from   sync   

运行环境 .net 4.6.1

//为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
var sslHandler = new HttpClientHandler()
{
};
sslHandler.ClientCertificateOptions = ClientCertificateOption.Manual;

ServicePointManager.ServerCertificateValidationCallback = new
RemoteCertificateValidationCallback(delegate
{
    return true;
});
HttpClient client = new HttpClient(sslHandler, true);
//请求Headers
client.DefaultRequestHeaders.Add("Authorization", "WSSE realm=\"SDP\",profile=\"UsernameToken\",type=\"Appkey\"");

//请求Body
var body = new Dictionary  ()
    {
        {
            "from", "sender"
        },
        {
            "to", "receiver"
        }
    };
HttpContent content = new FormUrlEncodedContent(body);
var response = client.PostAsync(apiAddress, content).Result;
Console.WriteLine(response.StatusCode); //打印响应结果码
var res = response.Content.ReadAsStringAsync();
Console.WriteLine(res.Result); //打印响应信息

运行环境dotnet core 2.2

//为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
HttpClient client = new HttpClient();
ServicePointManager.ServerCertificateValidationCallback = delegate
{
    return true;
};
//请求Headers
client.DefaultRequestHeaders.Add("Authorization", "WSSE realm=\"SDP\",profile=\"UsernameToken\",type=\"Appkey\"");
//请求Body
var body = new Dictionary  ()
    {
        {
            "from", sender
        },
        {
            "to", receiver
        }
    };
HttpContent content = new FormUrlEncodedContent(body);
var response = client.PostAsync(apiAddress, content).Result;
Console.WriteLine(response.StatusCode); //打印响应结果码
var res = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(res); //打印响应信息

HttpClient Post 提交表单数据

标签:new   cep   form   remote   证书认证   write   headers   from   sync   

原文地址:https://www.cnblogs.com/elef/p/12221853.html


评论


亲,登录后才可以留言!