返回错误1:Id = 3129, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
返回错误2:发生了一个或多个错误
One or more errors occurred. at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task`1.get_Result()
try
{
using (HttpClient httpClient = new HttpClient())
{
var splitArray = formData.Split(new char[] { '&' });
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Add("Authorization", apiToken);
var mfdc = new MultipartFormDataContent();
mfdc.Headers.Add("ContentType", "multipart/form-data");//声明头部
foreach (var item in splitArray)
{
var inContext = item.Split(new char[] { '=' });
if (inContext.Length >= 2)
{
mfdc.Add(new StringContent(inContext[inContext.Length - 1]), inContext[0]);// 内容在前,参数名称在后
}
}
request.Content = mfdc;
var response = httpClient.SendAsync(request).Result;
if (response != null)
{
HttpResponseMessage msg = response.EnsureSuccessStatusCode();
HttpStatusCode code = response.StatusCode;
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result;
}
}
}
}
catch(Exception ex)
{
}
解决方案:因为url是https的原因:新增https协议支持
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
public static string HttpsAsyncTPFormDataPost(string url, string apiToken, string formData)
{
string result = string.Empty;
try
{
using (HttpClient httpClient = new HttpClient())
{
var splitArray = formData.Split(new char[] { '&' });
if (!string.IsNullOrEmpty(url) && url.Contains("https"))
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
}
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Add("Authorization", apiToken);
var mfdc = new MultipartFormDataContent();
mfdc.Headers.Add("ContentType", "multipart/form-data");//声明头部
foreach (var item in splitArray)
{
var inContext = item.Split(new char[] { '=' });
if (inContext.Length >= 2)
{
mfdc.Add(new StringContent(inContext[inContext.Length - 1]), inContext[0]);// 内容在前,参数名称在后
}
}
request.Content = mfdc;
var response = httpClient.SendAsync(request).Result;
if (response != null)
{
HttpResponseMessage msg = response.EnsureSuccessStatusCode();
HttpStatusCode code = response.StatusCode;
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result;
}
}
}
}
catch(Exception ex)
{
}
return result;
}
参考地址:https://www.ab62.cn/article/31018.html
https://www.itguest.com/post/bfjcbf1a2.html
https://www.jb51.net/program/2879763nb.htm
https://qa.1r1g.com/sf/ask/1817519861/
标签:C#,request,mfdc,PostAsync,Result,var,new,inContext,response From: https://blog.51cto.com/jition/8406438