HttpClient multipart/form-data发布图像和json同时 [英] Httpclient multipart/form-data post image and json same time

查看:734
本文介绍了HttpClient multipart/form-data发布图像和json同时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#代码在一个请求中上传图片和json,但是服务器始终返回400错误的请求.使用提琴手执行相同的请求将返回状态码200.帮助...

I'm trying to upload image and json in one request using c# code, but server always returns 400- bad request. Executing same request using fiddler returns status code 200. help...

这是我的提琴手代码:

------ WebKitFormBoundary7MA4YWxkTrZu0gW内容处置:form-data; name ="application/json"内容类型:application/json

------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="application/json" Content-Type: application/json

{"type":个人","comments":["Lorem","Ipsum"]} ------ WebKitFormBoundary7MA4YWxkTrZu0gW--内容处置:表单数据; name ="fieldNameHere"; filename ="1111.jpg"

{"type": "Personal","comments": ["Lorem", "Ipsum" ] } ------WebKitFormBoundary7MA4YWxkTrZu0gW-- Content-Disposition: form-data; name="fieldNameHere"; filename="1111.jpg"

内容类型:图片/jpeg

Content-Type: image/jpeg

< @INCLUDE C:\ Users \ user \ Desktop \ New folder \ 1111.jpg @>

<@INCLUDE C:\Users\user\Desktop\New folder\1111.jpg@>

以及在c#中的实现:

var boundary = "Upload----" + DateTime.Now.Ticks.ToString();
MultipartFormDataContent form = new MultipartFormDataContent(boundary);
StringContent content = new StringContent(bodyJson);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
form.Add(content, "application/json");

var imageContent = new ByteArrayContent(image);
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
form.Add(imageContent, "image/jpeg", "image.jpg");
var responseTask = _httpClient.PostAsync(url, form).Result;

响应始终相同:

推荐答案

您可以将参数作为字符串内容传递,请检查以下示例.

You can pass the parameter as a string content , check the below sample.

public async Task<JObject> ExecutePostAsync(Stream myStreem, string url, string token, string parameter1, string parameter2, string parameter3)
    {
        try
        {
            using (var content = new MultipartFormDataContent("----MyBoundary"))
            {

                using (var memoryStream = myStreem)
                {
                    using (var stream = new StreamContent(memoryStream))
                    {
                        content.Add(stream, "file", Guid.NewGuid().ToString() + ".jpg");
                        content.Add(new StringContent(parameter1), "parameter1");
                        content.Add(new StringContent(parameter3), "parameter2");
                        content.Add(new StringContent(parameter3), "parameter3");

                        using (HttpClient client = new HttpClient())
                        {
                            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                            var responce = await client.PostAsync(url, content);
                            string contents = await responce.Content.ReadAsStringAsync();
                            return (JObject.Parse(contents));
                        }

                    }
                }
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

在API中,从FORM请求中获取数据

In API get the data from FORM request

    public async Task<IHttpActionResult> UploadFile()
    {

        string parameter1 = HttpContext.Current.Request.Form["parameter1"];
        string parameter2 = HttpContext.Current.Request.Form["parameter2"];
        string parameter3 = HttpContext.Current.Request.Form["parameter3"];

    }

这篇关于HttpClient multipart/form-data发布图像和json同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆