我要用winform程式呼叫一個網站api介面
給的java呼叫示例中寫:
form.add("file", fileSystemResource);
form.add("fileId", IdUtil.simpleUUID());
HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, files, Result.class);
我在網上搜索,沒看到c#中,用什么來代替這個HttpEntity ,請大家幫我
uj5u.com熱心網友回復:
HttpWebRequest和HttpWebResponseuj5u.com熱心網友回復:
和httpclient很像uj5u.com熱心網友回復:
你看下面C#的Post資料public static async Task<string> APIPost(string url, string data)
{
string result = string.Empty;
//設定HttpClientHandler的AutomaticDecompression
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
//創建HttpClient(注意傳入HttpClientHandler)
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
//傳遞單個值
{"", data}//鍵名必須為空
//傳遞物件
//{"name","hello"},
//{"age","16"}
});
//await異步等待回應
var response = await http.PostAsync(url, content);
//確保HTTP成功狀態值
response.EnsureSuccessStatusCode();
//await異步讀取最后的JSON(注意此時gzip已經被自動解壓縮了,因為上面的AutomaticDecompression = DecompressionMethods.GZip)
result = await response.Content.ReadAsStringAsync();
}
return result;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/17938.html
標籤:ASP.NET
