C#中POST接口formdata傳參模板的記錄
更新時(shí)間:2022年06月09日 17:01:19 作者:滄?!?
這篇文章主要介紹了C#中POST接口formdata傳參模板的記錄方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
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數(shù)據(jù)
var q = JsonConvert.DeserializeObject<MODEL>(res);日后用到方便查看記錄一下
C#模擬formdata提交參數(shù)
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;
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
c#使用linq技術(shù)創(chuàng)建xml文件的小例子
c#使用linq技術(shù)創(chuàng)建xml文件的小例子,需要的朋友可以參考一下2013-03-03
C#棧和隊(duì)列的簡(jiǎn)介,算法與應(yīng)用簡(jiǎn)單實(shí)例
今天小編就為大家分享一篇關(guān)于C#棧和隊(duì)列的簡(jiǎn)介,算法與應(yīng)用簡(jiǎn)單實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
c#中單例類與靜態(tài)類的區(qū)別以及使用場(chǎng)景
這篇文章主要給大家介紹了關(guān)于c#中單例類與靜態(tài)類的區(qū)別以及使用場(chǎng)景的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源
本文主要介紹了C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
C# FileSystemWatcher 在監(jiān)控文件夾和文件時(shí)的使用方法
這篇文章主要介紹了C# FileSystemWatcher 在監(jiān)控文件夾和文件時(shí)的使用方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以參考下2020-06-06

