C# 请求 form-data格式的 接口 POSTMAN form-data

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);

posted on 2021-07-02 16:18  三千世界a  阅读(1733)  评论(0编辑  收藏  举报

导航