您好,登錄后才能下訂單哦!
這篇文章主要介紹了C#中POST接口formdata傳參模板實例分析的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇C#中POST接口formdata傳參模板實例分析文章都會有所收獲,下面我們一起來看看吧。
var res = ""; HttpClient _httpClient = new HttpClient(); var postContent = new MultipartFormDataContent(); string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x")); postContent.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}"); var requestUri = "url"; var values = new[] { new KeyValuePair<string, string>("id","1") }; foreach (var keyValuePair in values) { postContent.Add(new StringContent(keyValuePair.Value), String.Format("\"{0}\"", keyValuePair.Key)); } var response = await _httpClient.PostAsync(requestUri, postContent); //瀏覽器出參返回入res if (response.IsSuccessStatusCode) { res = response.Content.ReadAsStringAsync().Result; } //處理返回JSON數據 var q = JsonConvert.DeserializeObject<MODEL>(res);
日后用到方便查看記錄一下
public string GetFormdata(Dictionary<string, string> dic,string ticks) { string Info = ""; string Head = string.Format("----------------------------{0}", ticks); string Foot = string.Format("----------------------------{0}--", ticks); foreach (var item in dic){ Info += string.Format("{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", Head, item.Key, item.Value); } Info += Foot; return Info; }
string Ticks = DateTime.Now.Ticks.ToString(); string Paramter = GetFormdata(dic, Ticks); string Html = ""; using (HttpHelper http = new HttpHelper()){ HttpItem item = new HttpItem(); item.URL = "http://app.farseasty.com/api/v100.smart/createSaleOrder"; item.Method = "post"; item.Header.Add("token", Token); item.ContentType = string.Format("multipart/form-data; boundary=--------------------------{0}", Ticks); item.PostEncoding = System.Text.Encoding.UTF8; item.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E; Tablet PC 2.0; TCO_20150304085044)"; item.PostDataType = PostDataType.String; item.Postdata = Paramter; HttpResult result = http.GetHtml(item); Html = result.Html; }
關于“C#中POST接口formdata傳參模板實例分析”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“C#中POST接口formdata傳參模板實例分析”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。