HttpClient Post 提交表单数据
2021-04-26 11:28
标签:new cep form remote 证书认证 write headers from sync HttpClient Post 提交表单数据 标签:new cep form remote 证书认证 write headers from sync 原文地址:https://www.cnblogs.com/elef/p/12221853.html运行环境 .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
运行环境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 提交表单数据
文章链接:http://soscw.com/index.php/essay/79784.html