这篇文章主要介绍了C#中POST接口formdata传参模板的记录方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
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);
日后用到方便查看记录一下
C#模拟formdata提交参数
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# 调用WebService的方法 2023-03-09
- winform把Office转成PDF文件 2023-06-14
- C# windows语音识别与朗读实例 2023-04-27
- C#控制台实现飞行棋小游戏 2023-04-22
- ZooKeeper的安装及部署教程 2023-01-22
- unity实现动态排行榜 2023-04-27
- C#类和结构详解 2023-05-30
- C# List实现行转列的通用方案 2022-11-02
- 一个读写csv文件的C#类 2022-11-06
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26