Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:40:20+00:00 2026-06-07T10:40:20+00:00

I’m using some of the code I found on here to POST an image

  • 0

I’m using some of the code I found on here to POST an image to a server. The problem is that I am not receiving the Json response that I should be, instead I’m getting the ol’ SERVER NOT FOUND response in my “ResponseReady” callback. (EDIT: Turns out it was just my parameters, this code works perfectly fine.)

Here is the class I am using to make the POST

    public class PostSubmitter
    {

       public string url { get; set; }
       public Dictionary<string, object> parameters { get; set; }
       string boundary = "----------" + DateTime.Now.Ticks.ToString();
       HttpWebRequest webRequest;

       public void Submit()
       {
           // Prepare web request...
           webRequest = WebRequest.CreateHttp(url);
           webRequest.Method = "POST";
           webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
           webRequest.BeginGetRequestStream(new AsyncCallback(RequestReady), webRequest);

       }

       private void RequestReady(IAsyncResult asynchronousResult)
       {
           using (Stream postStream = webRequest.EndGetRequestStream(asynchronousResult))
           {
                writeMultipartObject(postStream, parameters);
           }

           webRequest.BeginGetResponse(new AsyncCallback(ResponseReady), webRequest);

       }

       private void ResponseReady(IAsyncResult asynchronousResult)
       {
           try
           {
               using (var response =
                (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult))
               using (var streamResponse = response.GetResponseStream())
               using (var streamRead = new StreamReader(streamResponse))
               {
                   var responseString = streamRead.ReadToEnd();
                   var success = response.StatusCode == HttpStatusCode.OK;

                   if (responseString != null)
                   {
                       //JObject comes from Newtonsoft.Json ddl. This is a good one if your working with json
                       JObject jsonResponse = JObject.Parse(responseString);
                       //Do stuff with json.....
                   }
               }
           }
           catch (Exception e)
           {
               if (e.Message == "The remote server returned an error: NotFound.")
               {
                   webRequest.Abort();
                   Deployment.Current.Dispatcher.BeginInvoke(delegate() { MessageBox.Show("Unable to connect to server at this time, please try again later"); });
               }
               else
                   Deployment.Current.Dispatcher.BeginInvoke(delegate() { MessageBox.Show("Unable to upload photo at this time, please try again later"); });
               return;
           }
       }


       public void writeMultipartObject(Stream stream, object data)
       {
           using (StreamWriter writer = new StreamWriter(stream))
           {
               if (data != null)
               {
                   foreach (var entry in data as Dictionary<string, object>)
                   {
                       WriteEntry(writer, entry.Key, entry.Value);
                   }
               }
               writer.Write("--");
               writer.Write(boundary);
               writer.WriteLine("--");
               writer.Flush();
           }
       }

       private void WriteEntry(StreamWriter writer, string key, object value)
       {
           if (value != null)
           {
               writer.Write("--");
               writer.WriteLine(boundary);
               if (value is byte[])
               {
                   byte[] ba = value as byte[];

                   writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""; filename=""{1}""", key, "sentPhoto.jpg");
                   writer.WriteLine(@"Content-Type: application/octet-stream");
                   writer.WriteLine(@"Content-Type: image / jpeg");
                   writer.WriteLine(@"Content-Length: " + ba.Length);
                   writer.WriteLine();
                   writer.Flush();
                   Stream output = writer.BaseStream;

                   output.Write(ba, 0, ba.Length);
                   output.Flush();
                   writer.WriteLine();
               }
               else
               {
                   writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""", key);
                   writer.WriteLine();
                   writer.WriteLine(value.ToString());
               }
           }
       }
    }

Using this class we can then make a simple POST to the server with the following lines of code:

    Dictionary<string, object> postData = new Dictionary<string, object>()
                                                {
                                                    {"file", byteArrayOfImage}
                                                    //You can add other parameters here
                                                };
    PostSubmitter postToServer = new PostSubmitter() { url = getPicturePostUrl(), parameters = postData };
    postToServer.Submit();

There are so many questions out there on this… you’d think they’d make it easier to do complex web requests….

Thanks in advance for your helpful comments or feel free to ask a question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:40:20+00:00Added an answer on June 7, 2026 at 10:40 am

    Well this code actually works perfectly. I just didn’t have one of the required parameters so the server was rejecting the request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.