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 6008135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:46:55+00:00 2026-05-23T01:46:55+00:00

I want to make a POST to a PHP site from a C# WinForm

  • 0

I want to make a POST to a PHP site from a C# WinForm Application in a Browser window (or a WebBrowser-instance) with .net 2.0 and I need to get the data and the headers-argments right. Like:

webBrowser1.Navigate("http://mypublishservice.com/publish_picture.php","_SELF",X,Y); 

The question is: What should be X and Y? I know a have to fill all headers and file data in a byte[] array and add some additional headers as strings. I made examples of the webform below and examined them with firebug. So i know how the POST data should look like. I even created a HttpWebRequest which was ok, but I need a WebBrowser (reasons below) to start the Post-Request. So i am lost. I have tried many options, e.g. Upload files with HTTPWebrequest (multipart/form-data). Maybe there is a better way, by creating a HttpWebrequest and than passing this to a WebBrowser-instance or something like that?

Here is a web form to call the publish_picture.php page that works fine:

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <div>
      <form enctype="multipart/form-data" action="http://mypublishservice.com/publish_picture.php" method="POST">

      Please choose a photo:
      <input name="source" type="file"><br/><br/>
      Say something about this photo:
      <input name="message" type="text" value=""><br/><br/>
      <input type="submit" value="Upload"/><br/>
      </form>
    </div>
  </body>
</html>

In case you ask why I want to do it THIS way, here some thoughts to defend my silly decision 😉

Why a WebBrowser-instance and not a simple HttpWebrequest? Because the target service (e.g. Facebook) needs (or seems to need) a proper Browser!
Why not the target service APIs (e.g. Facebook API)? Found out, that Desktop-Web-communication is not good (too many 400-Errors).

UPDATE 2:

Looks better. Still I get an error but it might be the PHP page itself. Is this what you have in mind?

   public static byte[] PrepareUploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values, out string header)
   {

       using (var requestStream = new MemoryStream())
       {
           var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); //, NumberFormatInfo.InvariantInfo);
           header = "multipart/form-data; boundary=" + boundary;
           var boundaryBuffer2 = Encoding.ASCII.GetBytes(header);
               requestStream.Write(boundaryBuffer2, 0, boundaryBuffer2.Length);
               boundary = "--" + boundary;
               // Write the values
               foreach (string name in values.Keys)
           {
               var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
               requestStream.Write(buffer, 0, buffer.Length);
               buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
               requestStream.Write(buffer, 0, buffer.Length);
               buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
               requestStream.Write(buffer, 0, buffer.Length);
           }

           // Write the files
           foreach (var file in files)
           {
               var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
               requestStream.Write(buffer, 0, buffer.Length);
               buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
               requestStream.Write(buffer, 0, buffer.Length);
               buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
               requestStream.Write(buffer, 0, buffer.Length);

               CopyStream(file.Stream, requestStream); // file.Stream.CopyTo(requestStream);

               buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
               requestStream.Write(buffer, 0, buffer.Length);
           }

           var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
           requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);

           return requestStream.ToArray(); 

       }

   }

   public static void CopyStream(Stream input, Stream output)
   {
       byte[] buffer = new byte[32768];
       while (true)
       {
           int read = input.Read(buffer, 0, buffer.Length);
           if (read <= 0)
               return;
           output.Write(buffer, 0, read);
       }
 }



public void Upload()
{
  using (var stream1 = File.Open(Support.EXAMPLEIMAGE, FileMode.Open))
        {
            var files = new[] 
                 {
                new UploadFile
               {
            Name = "source",  // 1
            Filename = Support.EXAMPLEIMAGE,
            ContentType = "image/jpeg",   // 2
            Stream = stream1
              }

          };

          var values = new NameValueCollection
        {
         { "message", "a text" }   // 3

         };


             string contentType;  // 4. do I need it  
            byte[] dataToPost = Support.PrepareUploadFiles(Support.URL, files, values, out contentType); // 5. out contentType = what should be the result vaule?
            //PrepareUploadFiles(url, files, values, out contentType);
            webBrowser1.Navigate(Support.URL, null, dataToPost, "Content-Type: " + contentType + Environment.NewLine); 
        }


    }
  • 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-05-23T01:46:55+00:00Added an answer on May 23, 2026 at 1:46 am

    Personally I like using the ParseQueryString method as it takes care of properly encoding parameters:

    var values = HttpUtility.ParseQueryString(string.Empty);
    values["param1"] = "param1 value";
    values["param2"] = "param2 value";
    values["param3"] = "param3 value";
    var dataToPost = Encoding.UTF8.GetBytes(values.ToString());
    var url = "http://mypublishservice.com/publish_picture.php";
    var contentType = "Content-Type: application/x-www-form-urlencoded" + Environment.NewLine;
    webBrowser1.Navigate(url, null, dataToPost, contentType); 
    

    Now, because you are trying to upload files this is going to be a little more difficult. I wrote a blog post which illustrates how to generate a multipart/form-data request allowing to upload multiple files. So you could tweak the UploadFiles method shown there to only return the POST body and not do the actual upload and then:

    string contentType;
    byte[] dataToPost = PrepareUploadFiles(url, files, values, out contentType);
    webBrowser1.Navigate(url, null, dataToPost, "Content-Type: " + contentType + Environment.NewLine); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a php ajax post.(post value without refresh the page) here
I want to make a php ajax post.(post value without refresh the page) here
I want to make a http post to an outside url using php. By
I have an action handling a form post, but I want to make sure
I want to make a .NET Form as a TopMost Form for another external
I want to make sure people can't type the name of a PHP script
I want to make a JavaScript application that's not open source, and thus I
I want to make a login system using ASP.NET (MVC). On the internet, I
After setting up a mysqli object in php, i want to make to insert
I got this script from here $(document).ready(function() { $('#choose').change(function(event) { $.post('select-ajax.php', { selected: $('#choose').val()

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.