WP8上传图像,得到错误
问题描述:
下面的代码试图利用的multipart/form-data的上传图像到服务器:WP8上传图像,得到错误
public async void PostRequest(Stream photoStream, string lomail, string fileName)
{
try
{
using (HttpClient client = new HttpClient())
{
client.Timeout = TimeSpan.FromMinutes(10);
photoStream.Position = 0;
using (MultipartFormDataContent content = new MultipartFormDataContent())
{
content.Add(new StringContent(lomail), "lomail");
content.Add(new StreamContent(photoStream), "photo", fileName);
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("post");
});
HttpResponseMessage response = await client.PostAsync(LoUrl, content);
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(response.ToString());
});
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("finish");
});
}
}
}
catch (Exception e)
{
MessageBox.Show("post request: " + e.Message);
}
}
但有HTTP错误:(状态码404 ,Http.StramContent,Header:Content-length = 0)
这是怎么回事?
答
我找到了解决方案。
public async void PostRequest(Stream photoStream, string lomail, string fileName)
{
try
{
using (HttpClient client = new HttpClient())
{
client.Timeout = TimeSpan.FromMinutes(10);
photoStream.Position = 0;
using (MultipartFormDataContent content = new MultipartFormDataContent())
{
content.Add(new StringContent(lomail), "lomail");
content.Add(new StreamContent(photoStream), "photo", fileName);
//var imageContent = new ByteArrayContent(ImageData);
//imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
//content.Add(imageContent, "photo", "image.jpg");
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("post");
});
HttpResponseMessage response = await client.PostAsync(LoUrl, content);
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(response.ToString());
});
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("finish");
});
}
}
}
catch (Exception e)
{
MessageBox.Show("post request: " + e.Message);
}
}
请问您能解释一下a)您想做什么,b)您尝试过什么,c)您遇到什么问题?你实际上没有问过什么:) – Ben
对不起!这是我在stackowerflow上的第一个问题。我试图上传图像到服务器,但我有状态码404错误。 –
没问题,欢迎来到StackOverflow。您确信您发布的网址正确吗?服务器是否正常工作,即是否可以手动上传到相同的URL? – Ben