简体   繁体   English

C#HttpClient.PostAsync只工作一次

[英]c# HttpClient.PostAsync only works once

I'm working on a windows client for uploading a lot of small files over an http post request. 我正在Windows客户端上通过http发布请求上传许多小文件。 I'm using .NET 4.5.2 我正在使用.NET 4.5.2

public async void Upload3(HttpClient client, string url, string[] files)
{
    foreach (var file in files)
    {
        using (var stream = new FileStream(file, FileMode.Open))
        {
            FileInfo info = new FileInfo(file);
            HttpContent fileStreamContent = new StreamContent(stream);
            using (var content = new MultipartFormDataContent())
            {
                content.Add(fileStreamContent);
                var response = await client.PostAsync(url, content);
                response.EnsureSuccessStatusCode();
                //code is stopping at the following line:
                string finalresults = await response.Content.ReadAsStringAsync();
                Console.WriteLine(finalresults);
                Console.WriteLine(" > Uploaded file " + info.Name);
            }
            stream.Close();
        }
    }
    Console.WriteLine("> Uploaded all files");
}

The Code is working fine for the very first file. 该代码对于第一个文件运行良好。 But every other file is not uploaded. 但是其他所有文件均未上传。 When I try to debug the code step by step, the code execution stops (in the second iteration of the loop) on this line: 当我尝试逐步调试代码时,此行的代码执行停止(在循环的第二次迭代中):

string finalresults = await response.Content.ReadAsStringAsync();

Since the server log only shows on single request, I think that the error already occurs in this line: 由于服务器日志仅在单个请求上显示,因此我认为此行中已经发生了错误:

var response = await client.PostAsync(url, content);

Even if I use different HttpClient objects and different FileStream objects, the upload is only working for the first file. 即使我使用不同的HttpClient对象和不同的FileStream对象,上载仅适用于第一个文件。 What is wrong with this code? 此代码有什么问题?

For your requiment, you can user third party libraries like RESTSharp. 根据您的要求,您可以使用第三方库,例如RESTSharp。 There are lots of examples and good documentation. 有很多示例和良好的文档。 Also it is easy to use. 而且它易于使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 httpClient.PostAsync(URL,content)在C#Metro应用中不起作用 - httpClient.PostAsync(url, content) not working in C# Metro apps HttpClient.PostAsync() 方法在 C# xUnt 测试中永远挂起 - HttpClient.PostAsync() method is hanging forever in C# xUnt test C# HttpClient.PostAsync 不返回或抛出错误 - C# HttpClient.PostAsync doesn't return or throw an error HttpClient.PostAsync Xamarin.Android C#错误响应 - HttpClient.PostAsync Xamarin.Android C# wrong response 我在 HttpClient.PostAsync (C#) 中收到 StatusCode: 401 “Unauthorized” - I am getting StatusCode: 401 “Unauthorized” in a HttpClient.PostAsync (C#) 为什么我的 C# winforms 应用程序中的第一个 HttpClient.PostAsync 调用非常慢? - Why is first HttpClient.PostAsync call extremely slow in my C# winforms app? HTTPClient.PostAsync 仅在发布较大文件时抛出异常 - HTTPClient.PostAsync throws exception only when posting larger file HttpClient.PostAsync无法识别的响应 - HttpClient.PostAsync Unrecognized Response 没有得到HttpClient.PostAsync()的结果 - Not getting result of HttpClient.PostAsync() HttpClient.PostAsync 无响应且无异常 - HttpClient.PostAsync no response & no exceptions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM