如何将restsharp代码转换为包含文件的httpclient

问题描述 投票:0回答:1

我正在尝试上传文件时上传一些数据,我有restsharp代码来做到这一点,但我想使用htttpclient来做到这一点

我已经尝试了很多次来转换代码,但是我不能我无法使用httpclient上传带有某些数据的文件的挑战这是restsharp代码

var client = new RestClient("http://hlocal.halalcircleh.com/api/user-update");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "multipart/form-data; boundary=--------------------------900467662034781824108624");
request.AddHeader("Host", "hlocal.halalcircleh.com");
request.AddHeader("Authorization", "token");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"gender\"\r\n\r\n1\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\ndv bahaa elden\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"email\"\r\n\r\[email protected]\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"phone\"\r\n\r\n01211484991\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\n123456\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password_confirmation\"\r\n\r\n123456\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"photo\"\r\n\r\ntest\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
c# httpclient restsharp
1个回答
-1
投票
var client = new HttpClient();
var json = jsonConvert.SerializeObject();
var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");

var response =  client.PostAsync("http://hlocal.halalcircleh.com/api/user-update",stringContent).Result;

client.DefaultRequestHeaders.Add("Content-Type", "multipart/form-data; boundary=-900467662034781824108624");
client.DefaultRequestHeaders.Add("Host", "hlocal.halalcircleh.com");
client.DefaultRequestHeaders.Add("Authorization", "token");
client.DefaultRequestHeaders.Add("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW..........");             
client.DefaultRequestHeaders.Add("multipart/form-data; boundary=......);
© www.soinside.com 2019 - 2024. All rights reserved.