简体   繁体   English

如何使用HttpClient w.post发送数据和图像?

[英]How to send data and a Image using HttpClient w.post?

I need to perform a post method using HttpClient and capture it in my PHP web service. 我需要使用HttpClient执行post方法并将其捕获到我的PHP Web服务中。

At the first time, I was starting using a FormUrlEncodedContent and that's okay, my web service received these data. 第一次,我开始使用FormUrlEncodedContent,没关系,我的Web服务收到了这些数据。 But now I need that the user capture an image and get it in a byte array. 但是现在我需要用户捕获图像并将其保存为字节数组。

For this, I guess MultipartFormDataContent is the heroe here. 为此,我想MultipartFormDataContent是这里的英雄。 This is what I have 这就是我所拥有的

        Dictionary<string, string> @params = new Diction ...
        byte[] imageData = ...

        var requestContent = new MultipartFormDataContent();
        requestContent.Add(new FormUrlEncodedContent(@params));
        requestContent.Add(new ByteArrayContent(imageData));

        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.PostAsync(request, requestContent);
        response.EnsureSuccessStatusCode();
        string responseBody = await response.Content.ReadAsStringAsync();

But when I receive the response, I get the following: 但是,当我收到响应时,得到以下信息:

responseBody " responseBody“
\\n Warning : File Upload Mime headers garbled in Unknown on line 0 \\ n 警告 :文件上传Mime标头在第0行的“ 未知”中显示为乱码
\\n{\\"…" string \\ n {\\“…”字符串

I don't know if there's a problem in the php code or in my c# code. 我不知道php代码或c#代码是否存在问题。

we had similar problem - and we had to get rid of HttpClient as it's not very flexible. 我们有类似的问题-我们不得不摆脱HttpClient,因为它不是很灵活。 In our case, we had to upload multiple files to the server. 在我们的情况下,我们必须将多个文件上传到服务器。 We ended up using WebRequest. 我们最终使用了WebRequest。 Working code is below. 工作代码如下。 Look at PostMultipart function - everything else are helpers. 看一下PostMultipart函数-其他一切都是助手。

public class UploadFile
{
    public UploadFile(Stream data, string fieldName, string fileName, string contentType)
    {
        this.Data = data;
        this.FieldName = fieldName;
        this.FileName = fileName;
        this.ContentType = contentType;
    }

    public UploadFile(string fileAbsPath, string fieldName, string fileName, string contentType)
        : this(File.OpenRead(fileAbsPath), fieldName, fileName, contentType)
    {
    }

    public UploadFile(string fileAbsPath, string fieldName, string contentType)
        : this(fileAbsPath, fieldName, Path.GetFileName(fileAbsPath), contentType)
    {
    }


    public UploadFile(string fileAbsPath)
        : this(fileAbsPath, null, Path.GetFileName(fileAbsPath), "application/octet-stream")
    {
    }

    public Stream Data { get; set; }

    public string FieldName { get; set; }

    public string FileName { get; set; }

    public string ContentType { get; set; }

    public DateTimeOffset? CreationDate { get; set; }

    public DateTimeOffset? ModificationDate { get; set; }
}

class MimePart
{
    private readonly NameValueCollection headers = new NameValueCollection();
    private byte[] header;

    private Stream data;

    public override Stream Data
    {
        get
        {
            return this.data;
        }
    }

    public void SetStream(Stream stream)
    {
        this.data = stream;
    }

    public NameValueCollection Headers
    {
        get { return this.headers; }
    }

    public byte[] Header
    {
        get { return this.header; }
    }

    public long GenerateHeaderFooterData(string boundary)
    {
        var sb = new StringBuilder();
        sb.Append("--");
        sb.Append(boundary);
        sb.AppendLine();

        foreach (string key in this.headers.AllKeys)
        {
            sb.Append(key);
            sb.Append(": ");
            sb.AppendLine(this.headers[key]);
        }

        sb.AppendLine();

        this.header = Encoding.UTF8.GetBytes(sb.ToString());

        return this.header.Length + this.Data.Length + 2;
    }
}

private IList<MimePart> GenerateMimePartsList(IEnumerable<UploadFile> files)
{
    var mimeParts = new List<MimePart>();

    int nameIndex = 0;

    // Files data
    foreach (UploadFile file in files)
    {
        var streamMimePart = new StreamMimePart();

        if (string.IsNullOrEmpty(file.FieldName))
        {
            file.FieldName = "file" + nameIndex++;
        }

        var contentDispositionValues = new List<string>();
        contentDispositionValues.Add("form-data");
        contentDispositionValues.Add(string.Format("name=\"{0}\"", file.FieldName));
        contentDispositionValues.Add(string.Format("filename=\"{0}\"", file.FileName));

        if (file.CreationDate.HasValue)
        {
            contentDispositionValues.Add(string.Format("creation-date=\"{0}\"", file.CreationDate.Value.ToString("R")));
        }

        if (file.ModificationDate.HasValue)
        {
            contentDispositionValues.Add(string.Format("modification-date=\"{0}\"", file.ModificationDate.Value.ToString("R")));
        }

        // "form-data; name=\"" + file.FieldName + "\"; filename=\"" + file.FileName + "\"";
        streamMimePart.Headers["Content-Disposition"] = string.Join("; ", contentDispositionValues);
        streamMimePart.Headers["Content-Type"] = file.ContentType;
        streamMimePart.SetStream(file.Data);

        mimeParts.Add(streamMimePart);
    }

    return mimeParts;
}

public void PostMultipart(Uri uri, IEnumerable<UploadFile> files)
{
    var mimeParts = this.GenerateMimePartsList(files);

    var boundary = "----------" + DateTime.Now.Ticks.ToString("x");

    byte[] footer = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");

    var request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "POST";
    request.ContentType = "multipart/form-data; boundary=" + boundary;
    request.ContentLength = mimeParts.Sum(part => part.GenerateHeaderFooterData(boundary)) + footer.Length;
    try
    {
        // Write data
        byte[] buffer = new byte[8192];
        byte[] afterFile = Encoding.UTF8.GetBytes("\r\n");

        using (Stream requestStream = request.GetRequestStream())
        {
            foreach (MimePart mimePart in mimeParts)
            {
                requestStream.Write(mimePart.Header, 0, mimePart.Header.Length);

                int read;
                while ((read = mimePart.Data.Read(buffer, 0, buffer.Length)) > 0)
                {
                    requestStream.Write(buffer, 0, read);
                }

                mimePart.Data.Dispose();

                requestStream.Write(afterFile, 0, afterFile.Length);
            }

            requestStream.Write(footer, 0, footer.Length);
        }

        // Get response 
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            var responseText = response.ReadAsString();


            return;
        }
    }
    catch (WebException webException)
    {
        this.HandleErrorResponse(webException);
    }
    finally
    {
        foreach (var mimePart in mimeParts)
        {
            if (mimePart.Data != null)
            {
                mimePart.Data.Dispose();
            }
        }
    }

    return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM