如何将WebClient转换为HttpClient C# WinForms

huangapple go评论63阅读模式
英文:

How to convert WebClient to HttpClient C# WinForms

问题

我需要将已经过时的 webclient 代码转换为 httpclient。

using HttpClient Client = new();
Client.DefaultRequestHeaders.UserAgent.ParseAdd(CompanyName + "/" + ProductName);
HttpContent content = new StreamContent(new MemoryStream(fileimage));
HttpResponseMessage responseMessage = await Client.PostAsync("http://blahblah.com/upload.php", content);
string response = await responseMessage.Content.ReadAsStringAsync();
_ = MessageBox.Show(response, Text);
英文:

I need to convert the webclient code which is obsolte to httpclient.

using WebClient Client = new();
Client.Headers["User-Agent"] = CompanyName + "/" + ProductName;
byte[] responseBytes = Client.UploadFile("http://blahblah.com/upload.php", fileimage);
string response = Encoding.Default.GetString(responseBytes);
_ = MessageBox.Show(response, Text);

I need the exact same way to do this. I have seen other answers but none meet my needs.

答案1

得分: 1

1- 将文件的所有字节读取到upfilebytes数组中。

2- 创建新的HttpClient和MultipartFormDataContent。

3- 将字节数组添加到内容中。

4- 异步上传MultipartFormDataContent内容并将响应存储在response中。

var upfilebytes = File.ReadAllBytes("fileimage");

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", CompanyName + "/" + ProductName);
MultipartFormDataContent content = new MultipartFormDataContent();
ByteArrayContent baContent = new ByteArrayContent(upfilebytes);

content.Add(baContent, "File", "filename.png");

var response = await client.PostAsync("http://blahblah.com/upload.php", content);

var responsestr = await response.Content.ReadAsStringAsync();

MessageBox.Show(response, responsestr);
英文:

1- read AllByte of file into upfilebytes array

2-create new HttpClient and MultipartFormDataContent

3- add byte array to content

4-upload MultipartFormDataContent content async and store response in response.

var upfilebytes = File.ReadAllBytes("fileimage");

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", CompanyName + "/" + ProductName);
MultipartFormDataContent content = new MultipartFormDataContent();
ByteArrayContent baContent = new ByteArrayContent(upfilebytes);

content.Add(baContent, "File", "filename.png");

 var response = await client.PostAsync("http://blahblah.com/upload.php", content);

 var responsestr = await response.Content.ReadAsStringAsync();

MessageBox.Show(response, responsestr);

答案2

得分: 0

最简单的上传文件方法类似于这样。然而,最好使用HttpClientFactory来生成HttpClient实例。

var client = new HttpClient
{
    BaseAddress = new Uri("http://blahblah.com/upload.php")
};
     
await using var stream = System.IO.File.OpenRead("filePathHere");
using var request = new HttpRequestMessage(HttpMethod.Post, "file");
request.Headers.Add("User-Agent", CompanyName + "/" + ProductName);
using var content = new MultipartFormDataContent
{
    { new StreamContent(stream), "file", "fileNameHere" }
};

request.Content = content;

var response = await client.SendAsync(request);
MessageBox.Show(response, Text);
英文:

The simplest way you can upload a file is something like here.However,

it's better to use HttpClientFactory to generate httpclient instance.

var client = new HttpClient
{
    BaseAddress = new("http://blahblah.com/upload.php")
};
 
await using var stream = System.IO.File.OpenRead("filePathHere");
using var request = new HttpRequestMessage(HttpMethod.Post, "file");
request.Headers.Add("User-Agent",CompanyName + "/" + ProductName);
using var content = new MultipartFormDataContent
{
    { new StreamContent(stream), "file", "fileNameHere" }
};

request.Content = content;

var response = await client.SendAsync(request);
MessageBox.Show(response, Text);

huangapple
  • 本文由 发表于 2023年2月14日 01:13:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定