反正也没什么人看,就用蹩脚的歪文写一写。
A http tool that uses HttpClient class.
It depend on NewtonSoft.Json.
var ut = HttpClientUtility.Create();
var resp = await ut.GetAsync("requestUrl");
// the generic type can be other class type,here is object
var ret = await resp.Content.ReadAsAsync<object>();
var data = new {name="Luoriloutai"};
var resp = await ut.PostObjectInJsonFormatAsync($"requestUrl", data, Encoding.UTF8);
var ret = await resp.Content.ReadAsAsync<object>();
var resp = await ut.PostStringAsync("requestUrl","data",Encoding.UTF8);
var ret = await resp.Content.ReadAsAsync<object>();
1.This method is used for upload local file with web input element.In this case that we can't get the path of uploading file:
// assuming there are files had been uploaded
var files = HttpContext.Current.Request.Files;
// get the first file here
var file = files[0];
var fileInfo = new MultiPartInputFile
{
FileName = file.FileName,
HttpName = "media",
InputFileStream = file.InputStream
}
var files = new List { file };
var resp = await ut.PostMultipartContentAsync("requestUrl", null, files);
var ret = await resp.Content.ReadAsAsync<object>();
2.This method is used for the case that we can get the path of uploading file,in common, this will be desktop app programing:
var files = new List{
new MultipartLocalFile{
HttpName="file",
FileLocalPath="c:\test.jpg"
};
};
var resp = await ut.PostMultipartContentAsync("requestUrl", null, files);
var ret = await resp.Content.ReadAsAsync<object>();
await ut.DownloadFile("requestSourceUrl","downloadDriectory","saveFileName");