在接收多部分/表单数据POST请求期间,我遇到了问题,但是我无法在我的身体内部出现问题。发送请求后,服务器会用此错误答复我
MIME多部分流的意外结尾。 MIME分段消息不是 完成。
关注请求的正文。
-------BoundaryNg6f698dj968io34abg32rfa3
Content-Disposition: form-data; name="json"; filename="json"; Content-Type:application/json
{"key":"value","key":"value","key":"value","key":"value","key":"value"}
-------BoundaryNg6f698dj968io34abg32rfa3
Content-Disposition: form-data; name="style"; filename="style.css"; Content-Type:text/css
body {
width: 100%;
margin: 0;
padding: 0;
[etc etc]}
-------BoundaryNg6f698dj968io34abg32rfa3
Content-Disposition: form-data; name="play_circle"; filename="play_circle.png"; Content-Type:image/png
[Image]
-------BoundaryNg6f698dj968io34abg32rfa3
在我看来,所有的身体看起来都是正确的,但是我很累,也许有人可以帮助我解决我的问题?
@edit
下面您将看到我如何构建multypart车身:
此部分构建Css和Json部分。
public byte[] bodyBuilder()
{
byte[] bodyAtt = new byte[1];
StringBuilder sb = new StringBuilder();
foreach (PostData p in postData)
{
sb.Append(string.Format("\r\n{0}", boundary));
sb.AppendLine("\r\n");
sb.Append(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"; Content-Type:{2}", p.Name,p.FileName, p.Type));
sb.AppendLine("\r\n");
sb.Append(string.Format("{0}\r\n", p.Value));
}
bodyAtt = encoding.GetBytes(sb.ToString());
if (content_Type.Contains("multipart/form-data"))
{
bodyAtt = Combine(bodyAtt, setAttachPostData());
}
var str = Encoding.Default.GetString(bodyAtt);
string pathFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Log.txt");
File.WriteAllText(pathFile, str);
return bodyAtt;
}
这是用于图像。
public byte[] setAttachPostData()
{
byte[] bodyAtt = new byte[1];
foreach (PostDataParam p in postDataParams)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("\r\n");
sb.Append(string.Format("{0}", boundary));
sb.AppendLine("\r\n");
sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"; Content-Type:{2}", p.Name, p.FileName, p.Type));
sb.AppendLine("\r\n");
bodyAtt = Combine(bodyAtt, encoding.GetBytes(sb.ToString())); bodyAtt = Combine(bodyAtt, p.data);
}
bodyAtt = Combine(bodyAtt, encoding.GetBytes(string.Format("\r\n\r\n{0}\r\n", boundary)));
return bodyAtt;
}
和Combine方法很简单:
public static byte[] Combine(byte[] first, byte[] second)
{
byte[] ret = new byte[first.Length + second.Length];
Buffer.BlockCopy(first, 0, ret, 0, first.Length);
Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
return ret;
}
答案 0 :(得分:0)
我已经使用这部分代码解决了这个问题:
public async Task<string> BuildMultipartAsync()
{
try
{
MultipartFormDataContent form = new MultipartFormDataContent();
foreach (PostData p in postData)
{
form.Add(new StringContent(p.Value), p.Name, p.FileName);
}
foreach (PostDataParam p in postDataParams)
{
form.Add(new ByteArrayContent(p.data, 0, p.data.Length), p.Name, p.FileName);
}
HttpResponseMessage response = await httpClient.PostAsync(URLCall, form);
response.EnsureSuccessStatusCode();
httpClient.Dispose();
string sd = response.Content.ReadAsStringAsync().Result;
return sd;
}catch (Exception e)
{
return e.Message;
}
}
引用自:
How to upload file to server with HTTP POST multipart/form-data
希望可以提供帮助。