C# 过帐多部分表单数据问题

C# 过帐多部分表单数据问题,c#,https,base64,httpwebrequest,mime,C#,Https,Base64,Httpwebrequest,Mime,我正在使用HttpWebRequest发布到MMS API。文章正文包含有关传递和彩信的XML数据,作为需要Base64编码的MIME多部分附件 帖子成功了,但我只收到了文本,没有收到图像 查看我的代码时,它似乎构建了表单数据,但当我将其转换回字符串时,文件数据丢失了 str变量的内容: ------------f2de17263b724d5a919b14a6834c489f 内容配置:表单数据;name=“username” 三部曲 ------------f2de17263b724d5a91

我正在使用HttpWebRequest发布到MMS API。文章正文包含有关传递和彩信的XML数据,作为需要Base64编码的MIME多部分附件

帖子成功了,但我只收到了文本,没有收到图像

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

str变量的内容:

------------f2de17263b724d5a919b14a6834c489f 内容配置:表单数据;name=“username”

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

ZBo8KE6m ------------f2de17263b724d5a919b14a6834c489f 内容配置:表单数据;name=“编号”

614027898 ------------f2de17263b724d5a919b14a6834c489f 内容配置:表单数据;name=“主题”

测试消息主题

------------f2de17263b724d5a919b14a6834c489f 内容配置:表单数据;name=“消息”

测试消息体

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

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

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

凭证.png ------------f2de17263b724d5a919b14a6834c489f 内容配置:表单数据;name=“name1”

QRCode.png ------------f2de17263b724d5a919b14a6834c489f 内容配置:表单数据;name=“attachment0”;filename=“凭证.png” 内容类型:图像/jpeg

�巴布亚新几内亚

    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;
    }
private static readonly Encoding Encoding=Encoding.UTF8;
公共静态HttpWebResponse MultipartFormDataPost(字符串姿势、字符串用户代理、字典后参数)
{
string formDataBoundary=string.Format(“------------{0:N}”,Guid.NewGuid());
string contentType=“多部分/表单数据;边界=“+formDataBoundary;
byte[]formData=GetMultipartFormData(后参数,formDataBoundary);
var str=Encoding.UTF8.GetString(formData);
返回PostForm(postrl、userAgent、contentType、formData);
}
私有静态字节[]GetMultipartFormData(字典后参数,字符串边界)
{
Stream formDataStream=new System.IO.MemoryStream();
字节[]formData=新字节[0];
bool needsCLRF=假;
尝试
{
foreach(后参数中的var参数)
{
//添加一个CRLF以允许添加多个参数(跳过第一个参数)
如果(需要SCLRF)
formDataStream.Write(encoding.GetBytes(“\r\n”)、0、encoding.GetByteCount(“\r\n”);
needsCLRF=true;
if(param.Value为FileParameter)
{
FileParameter fileToUpload=(FileParameter)param.Value;
//只添加此参数的第一部分,因为我们将直接将文件数据写入流
string header=string.Format(“--{0}\r\n内容配置:表单数据;名称=\“{1}\”;文件名=\“{2}\”\r\n内容类型:{3}\r\n\r\n”,
边界,
参数键,
fileToUpload.FileName??参数键,
fileToUpload.ContentType??“应用程序/八位字节流”);
Write(encoding.GetBytes(header),0,encoding.GetByteCount(header));
//将文件写入流
string str=Convert.ToBase64String(fileToUpload.File);
byte[]myBytes=Convert.FromBase64String(str);
写入(myBytes,0,myBytes.Length);
}
其他的
{
string postData=string.Format(“--{0}\r\n内容配置:表单数据;名称=\“{1}\”\r\n\r\n{2}”,
边界,
参数键,
参数值);
Write(encoding.GetBytes(postData),0,encoding.GetByteCount(postData));
}
}
//添加请求的结尾。以换行符开头
string footer=“\r\n-->+boundary+”-->\r\n”;
Write(encoding.GetBytes(footer),0,encoding.GetByteCount(footer));
//将流转储到字节数组中
formDataStream.Position=0;
formData=新字节[formDataStream.Length];
读取(formData,0,formData.Length);
formDataStream.Close();
}
捕获(例外情况除外)
{
gFunc.ProcessError(true,例如ToString(),“Post数据”);
}
返回表单数据;
}

原来问题出在post数据上

在删除文件参数的“内容分发”中的附加字段后,它起作用了

以前: 内容配置:表单数据;name=“attachment0”;filename=“凭证.png” 内容类型:图像/jpeg

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

事实证明,问题出在post数据上

在删除文件参数的“内容分发”中的附加字段后,它起作用了

以前: 内容配置:表单数据;name=“attachment0”;filename=“凭证.png” 内容类型:图像/jpeg

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

我通过更改byte[]myBytes=Convert.FromBase64String(str)解决了部分问题;to byte[]myBytes=encoding.GetBytes(str);但是当我收到彩信时,我仍然只收到文本,没有收到图片。我通过更改byte[]myBytes=Convert.FromBase64String(str)解决了部分问题;to byte[]myBytes=encoding.GetBytes(str);但是,当我收到彩信时,我仍然只收到短信,没有收到短信