您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。