HttpClient PostAsync 方法抛出聚合异常
HttpClient PostAsync method throw Aggregate Exception
我想使用 http 客户端 class 调用 api 控制器方法,但 PostAsync 方法引发了聚合异常。我尝试编写一个调用 PostAsync 的异步方法并尝试使用 ContinueWith 方法,但没有一个起作用。这是代码:
class Program
{
private const string apiPath = @"http://localhost:51140";
private const string param = "/Home/savedocumenttoPath?folderPath=string";
static void Main(string[] args)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(apiPath);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = getBack(client);
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
client.Dispose();
Console.ReadLine();
}
public static HttpResponseMessage getBack(HttpClient client)
{
return client.PostAsync(client.BaseAddress + param, null).GetAwaiter().GetResult();
}
}
这里是我想调用的控制器:(我试过 JsonResult 但还是不行)
[HttpPost]
public ActionResult saveDocumentToPath(string folderPath)
{
try
{
if (string.IsNullOrWhiteSpace(folderPath)) throw new NullReferenceException("Invalid Folder!");
var fullPath = folderPath + "\";
if (!System.IO.Directory.Exists(fullPath))
{
return new HttpStatusCodeResult(HttpStatusCode.OK, "The specified directory not exists: \n" + fullPath);
}
var fileName = "ProjectList_Excel_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day;
var filePathName = fullPath + fileName;
if (System.IO.File.Exists(filePathName))
{
return new HttpStatusCodeResult(HttpStatusCode.OK, "The specified file already exists in the folder: \n" + fileName);
}
System.IO.File.WriteAllBytes(filePathName, BL.ExcelExport.GetProjectListExcel());
return new HttpStatusCodeResult(HttpStatusCode.OK, "File Exported successfully!");
}
catch (Exception e)
{
return new HttpStatusCodeResult(HttpStatusCode.OK, "Error occured while saving the file" + e.Message);
}
}
您可以像这样修改您的 getBack
方法。由于您的端点需要简单类型的参数(例如字符串或整数),因此您需要将其包装在 FormUrlEncodedContent
中。 folderPath键在Dictionary<string, string>
对应端点的参数名称。
public static HttpResponseMessage getBack(HttpClient client)
{
var values = new Dictionary<string, string>
{
{ "folderPath", @"C:\Temp" }
};
var content = new FormUrlEncodedContent(values);
return client.PostAsync("Home/saveDocumentToPath", content).GetAwaiter().GetResult();
}
您甚至不需要在您的客户端中使用 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
,因为您没有发布 json。
我想使用 http 客户端 class 调用 api 控制器方法,但 PostAsync 方法引发了聚合异常。我尝试编写一个调用 PostAsync 的异步方法并尝试使用 ContinueWith 方法,但没有一个起作用。这是代码:
class Program
{
private const string apiPath = @"http://localhost:51140";
private const string param = "/Home/savedocumenttoPath?folderPath=string";
static void Main(string[] args)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(apiPath);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = getBack(client);
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
client.Dispose();
Console.ReadLine();
}
public static HttpResponseMessage getBack(HttpClient client)
{
return client.PostAsync(client.BaseAddress + param, null).GetAwaiter().GetResult();
}
}
这里是我想调用的控制器:(我试过 JsonResult 但还是不行)
[HttpPost]
public ActionResult saveDocumentToPath(string folderPath)
{
try
{
if (string.IsNullOrWhiteSpace(folderPath)) throw new NullReferenceException("Invalid Folder!");
var fullPath = folderPath + "\";
if (!System.IO.Directory.Exists(fullPath))
{
return new HttpStatusCodeResult(HttpStatusCode.OK, "The specified directory not exists: \n" + fullPath);
}
var fileName = "ProjectList_Excel_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day;
var filePathName = fullPath + fileName;
if (System.IO.File.Exists(filePathName))
{
return new HttpStatusCodeResult(HttpStatusCode.OK, "The specified file already exists in the folder: \n" + fileName);
}
System.IO.File.WriteAllBytes(filePathName, BL.ExcelExport.GetProjectListExcel());
return new HttpStatusCodeResult(HttpStatusCode.OK, "File Exported successfully!");
}
catch (Exception e)
{
return new HttpStatusCodeResult(HttpStatusCode.OK, "Error occured while saving the file" + e.Message);
}
}
您可以像这样修改您的 getBack
方法。由于您的端点需要简单类型的参数(例如字符串或整数),因此您需要将其包装在 FormUrlEncodedContent
中。 folderPath键在Dictionary<string, string>
对应端点的参数名称。
public static HttpResponseMessage getBack(HttpClient client)
{
var values = new Dictionary<string, string>
{
{ "folderPath", @"C:\Temp" }
};
var content = new FormUrlEncodedContent(values);
return client.PostAsync("Home/saveDocumentToPath", content).GetAwaiter().GetResult();
}
您甚至不需要在您的客户端中使用 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
,因为您没有发布 json。