C# HttpClient未按预期操作
我的C#应用程序将文件上传到某个API,我使用了多部分请求,即我上传了一个json字符串和文件的二进制内容,它对大多数文件都很好,但对极少数文件它什么也不做,我的意思是让我们尝试一下名为C# HttpClient未按预期操作,c#,task,console-application,C#,Task,Console Application,我的C#应用程序将文件上传到某个API,我使用了多部分请求,即我上传了一个json字符串和文件的二进制内容,它对大多数文件都很好,但对极少数文件它什么也不做,我的意思是让我们尝试一下名为file.pdf: 我的代码大致如下所示: public async Task<Dictionary<string , string>> Upload(string filePath) { FileInfo fi = new FileInfo(FilePath);
file.pdf
:
我的代码大致如下所示:
public async Task<Dictionary<string , string>> Upload(string filePath)
{
FileInfo fi = new FileInfo(FilePath);
string jsonString="some json string";
byte[] fileContents=File.ReadAllBytes(fi.FullName);
Uri webService = new Uri(url);
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post , webService);
requestMessage.Method = HttpMethod.Post;
requestMessage.Headers.Add("Authorization" , "MyKey1234");
const string boundry = "------------------My-Boundary";
MultipartFormDataContent multiPartContent = new MultipartFormDataContent(boundry);
ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents);
multiPartContent.Add(byteArrayContent);
requestMessage.Content = multiPartContent;
HttpClient httpClient = new HttpClient();
Console.WriteLine("before");
HttpResponseMessage httpResponse = await httpClient.SendAsync(requestMessage , HttpCompletionOption.ResponseContentRead , CancellationToken.None);
Console.WriteLine("after");
}
class Program
{
static void Main(string[] args)
{
new MyClass().Start();
}
}
输出:
before
Press any key to continue . . .
我的意思是没有例外,什么都没有,这是什么?虫子
编辑
控制台应用程序的结构如下所示:
public async Task<Dictionary<string , string>> Upload(string filePath)
{
FileInfo fi = new FileInfo(FilePath);
string jsonString="some json string";
byte[] fileContents=File.ReadAllBytes(fi.FullName);
Uri webService = new Uri(url);
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post , webService);
requestMessage.Method = HttpMethod.Post;
requestMessage.Headers.Add("Authorization" , "MyKey1234");
const string boundry = "------------------My-Boundary";
MultipartFormDataContent multiPartContent = new MultipartFormDataContent(boundry);
ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents);
multiPartContent.Add(byteArrayContent);
requestMessage.Content = multiPartContent;
HttpClient httpClient = new HttpClient();
Console.WriteLine("before");
HttpResponseMessage httpResponse = await httpClient.SendAsync(requestMessage , HttpCompletionOption.ResponseContentRead , CancellationToken.None);
Console.WriteLine("after");
}
class Program
{
static void Main(string[] args)
{
new MyClass().Start();
}
}
在MyClass:
public async void Start()
{
myDictionary = await Upload(filePath);
}
如注释部分所述,您的
main
方法不会等待等待调用,也不会等待HttpClient
实例处理响应
如果console应用程序用于测试目的,则可以对该方法返回的任务实例调用.Result
属性,如下所示:
new MyClass().Start().Result;
class Program
{
static async Task Main(string[] args)
{
await new MyClass().Start();
}
}
但是,最好使用async
关键字,如下所示:
new MyClass().Start().Result;
class Program
{
static async Task Main(string[] args)
{
await new MyClass().Start();
}
}
最后,作为示例,您应该在异步方法名称中添加后缀“Async”。例如,
Start
将被命名为StartAsync
,如注释部分所述,您的main
方法不会等待可等待的调用,也不会等待HttpClient
实例处理响应
如果console应用程序用于测试目的,则可以对该方法返回的任务实例调用.Result
属性,如下所示:
new MyClass().Start().Result;
class Program
{
static async Task Main(string[] args)
{
await new MyClass().Start();
}
}
但是,最好使用async
关键字,如下所示:
new MyClass().Start().Result;
class Program
{
static async Task Main(string[] args)
{
await new MyClass().Start();
}
}
最后,作为示例,您应该在异步方法名称中添加后缀“Async”。例如,
Start
将被命名为StartAsync
调用者在哪里?我们需要完整的类才能为您提供解释。您可以添加httpResponse.EnsureSuccessStatusCode()
行,然后中断或添加watch以查看API的响应吗?@Boxed我现在尝试了,但它以前仍然只打印”
string@exe请编辑您的问题以包含完整的。控制台应用程序的主方法很可能在其声明中没有async关键字,因此不会等待响应。你能发布主要方法代码吗?调用者在哪里?我们需要完整的类才能为您提供解释。您可以添加httpResponse.EnsureSuccessStatusCode()
行,然后中断或添加watch以查看API的响应吗?@Boxed我现在尝试了,但它以前仍然只打印”
string@exe请编辑您的问题以包含完整的。控制台应用程序的主方法很可能在其声明中没有async关键字,因此不会等待响应。你能发布主要的方法代码吗?