windows-phone-8 - WP8 使用 multipart/form-data 在服务器上上传图片,出现错误

标签 windows-phone-8 multipartform-data dotnet-httpclient

以下代码尝试使用multipart/form-data 将图像上传到服务器:

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);
            }
        } 

关于windows-phone-8 - WP8 使用 multipart/form-data 在服务器上上传图片,出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21098555/

相关文章:

javascript - 将数据传递给 Axios 中的服务

angularjs - 使用 $http 发送带有 Angular 的多部分/表单数据文件

forms - 如何访问在 multipart/form-data POST 中上传的内容数据?

c# - 触发 HttpClient 的更简单方法(可能没有任务)?

c# - 使用 HttpClient 检查 Internet 连接

c# - Windows Phone 开发错误错误 "Csc.exe"退出,代码为 -1073741819

c# - 在 Windows Phone 中生成图像网格

windows-phone-8 - 了解 Windows Phone 8 应用程序是否正在 Windows 10 手机上运行

windows - 将包提交到 Windows Phone 商店时出现无效的包标识错误

c# - Xamarin Forms Post 请求 Http 问题