过帐多部分表单数据问题

时间:2019-07-10 03:11:26

标签: c# https base64 httpwebrequest mime

我正在使用HttpWebRequest POST到MMS API。帖子的正文包含有关传递和XML消息的XML数据,作为MIME多部分附件,需要进行Base64编码。

发布成功,但是我只收到文字,而不是图片。

在查看我的代码时,似乎可以构建表单数据,但是当我将其转换回字符串时,文件数据丢失了。

str 变量的内容:

------------ f2de17263b724d5a919b14a6834c489f 内容处置:表单数据;名称=“用户名”

三部曲 ------------ f2de17263b724d5a919b14a6834c489f 内容处置:表单数据; name =“密码”

ZBo8KE6分钟 ------------ f2de17263b724d5a919b14a6834c489f 内容处置:表单数据; name =“ number”

61402720898 ------------ f2de17263b724d5a919b14a6834c489f 内容处置:表单数据; name =“ subject”

测试消息主题

------------ f2de17263b724d5a919b14a6834c489f 内容处置:表单数据; name =“ message”

测试邮件正文。

------------ f2de17263b724d5a919b14a6834c489f 内容处置:表单数据; name =“ type0”

图片/ jpeg ------------ f2de17263b724d5a919b14a6834c489f 内容处置:表单数据; name =“ type1”

图片/ jpeg ------------ f2de17263b724d5a919b14a6834c489f 内容处置:表单数据; name =“ name0”

Voucher.png ------------ f2de17263b724d5a919b14a6834c489f 内容处置:表单数据; name =“ name1”

QRCode.png ------------ f2de17263b724d5a919b14a6834c489f 内容处置:表单数据; name =“ attachment0”; filename =“ Voucher.png” 内容类型:图片/ jpeg

�PNG

    private static readonly Encoding encoding = Encoding.UTF8;
    public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary<string, object> postParameters)
    {
        string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
        string contentType = "multipart/form-data; boundary=" + formDataBoundary;

        byte[] formData = GetMultipartFormData(postParameters, formDataBoundary);
        var str = Encoding.UTF8.GetString(formData);

        return PostForm(postUrl, userAgent, contentType, formData);
    }

    private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
    {
        Stream formDataStream = new System.IO.MemoryStream();
        byte[] formData = new byte[0];
        bool needsCLRF = false;
        try
        {
            foreach (var param in postParameters)
            {
                // add a CRLF to allow multiple parameters to be added (skip it on the 1st parameter)
                if (needsCLRF)
                    formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));

                needsCLRF = true;

                if (param.Value is FileParameter)
                {
                    FileParameter fileToUpload = (FileParameter)param.Value;

                    // add just the first part of this param, since we will write the file data directly to the stream
                    string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
                        boundary,
                        param.Key,
                        fileToUpload.FileName ?? param.Key,
                        fileToUpload.ContentType ?? "application/octet-stream");

                    formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));

                    // write the file to the stream
                    string str = Convert.ToBase64String(fileToUpload.File);
                    byte[] myBytes = Convert.FromBase64String(str);
                    formDataStream.Write(myBytes, 0, myBytes.Length);
                                        }
                else
                {
                    string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
                        boundary,
                        param.Key,
                        param.Value);
                    formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
                }
            }

            // add the end of the request. Start with a newline
            string footer = "\r\n--" + boundary + "--\r\n";
            formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));

            // dump stream into byte array
            formDataStream.Position = 0;
            formData = new byte[formDataStream.Length];
            formDataStream.Read(formData, 0, formData.Length);
            formDataStream.Close();
        }
        catch (Exception ex)
        {
            gFunc.ProcessError(true, ex.ToString(), "Post Data");
        }
        return formData;
    }

1 个答案:

答案 0 :(得分:0)

事实证明问题出在发布数据上。

删除File参数的“ Content-Distribution”中的其他字段后,它开始工作。

以前: 内容处置:表单数据; name =“ attachment0”; filename =“ Voucher.png” 内容类型:图片/ jpeg

现在: 内容处置:表单数据; name =“ attachment0”