简体   繁体   中英

How to convert restsharp code to httpclient which contains file

I'm trying to post some data with file uploaded , I have restsharp code to do that but i want to do that with htttpclient

I've tried Many time to convert the code but i can't the challange that i can't upload file with some data using httpclient this is restsharp code

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\ndvbahaa2@gmail.com\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);
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=......);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM