3

Following code tries to upload image to server using 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);
    }
}

But there's HTTP error: (status code 404, Http.StramContent, Header: Content-length=0)

How do this correctly?

4
  • 3
    Can you please explain a) what you are trying to do, b) what have you tried, and c) what problems you are facing? You haven't actually asked anything :) Commented Jan 13, 2014 at 18:25
  • 1
    Sorry! It's my first question on stackowerflow. I'm trying to upload image to server, but i have error with status code 404. Commented Jan 13, 2014 at 18:30
  • 1
    No problem, welcome to StackOverflow. Are you confident that you're posting to the correct URL? Is the server working, i.e. can you upload manually to the same URL? Commented Jan 13, 2014 at 19:01
  • 1
    I think URL is correct. "lomobil.com/handler/upload" Commented Jan 13, 2014 at 19:05

1 Answer 1

4

I found the solution.

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);
            }
        } 
Sign up to request clarification or add additional context in comments.

3 Comments

my api return json,how to get json response from it.thanks in advance
This is not the right place to ask this. First of all, you should consider creating a new question, but it seems like the answer will already be given somewhere, so take a look at bing (yes!) or google.
@koch_kir You could probably accept this answer (that checkmark at the upper left corner of the answer), that's the Stack Overflow way to mark the problem "solved". And it's fine and good to accept your own answer if you know it works and when there's no better answer from somebody else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.