title: HttpWebRequest multipart form-data
body: <p>I am trying to submit simple text data to a multipart-form CGI.</p>
  
  <p>The problem is.. I don't know how to format the form data!</p>
  
  <p>In short, my system prepares a string containing the post data, creates a streamwriter using request.GetRequestStream(), and directly writes the post string to that streamwriter. It then goes on to close the streamwriter, and send the request.</p>
  
  <p>I have tried formatting the string as so:"Param1=sometext&amp;param2=sometext", but I get returned the error:</p>
  
  <pre><code>Unhandled Exception: System.Net.WebException: The remote server returned an error: (417) Expectation Failed.
    at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x002d9] in /private/tmp/monobuild/build/BUILD/mono-2.8/mcs/class/System/System.Net/HttpWebRequest.cs:1425 
    at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00143] in /private/tmp/monobuild/build/BUILD/mono-2.8/mcs/class/System/System.Net/HttpWebRequest.cs:1250 
  </code></pre>
  
  <p>Clearly I am formatting the string wrong, but I cannot for the life of me figure out how to do so properly.</p>
  
  <p>Can someone enlighten me? :)</p>
  
  <p>EDIT, I also tried changing <code>request.ContentType = "application/x-www-form-urlencoded";</code> to <code>request.ContentType = "multipart/form-data";</code>. This had no avail, but I am going to leave it that way.</p>
  
  <p>EDIT: Full code:</p>
  
  <pre><code>public static string sendReq (string url, string pdata)//Pdata is the string containing form data
  {
      StringBuilder sb  = new StringBuilder();
      byte[] buf = new byte[8192];
      HttpWebRequest request  = (HttpWebRequest) WebRequest.Create(url);
      CookieCollection jar = new CookieCollection();
      jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
      jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
      jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
      jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
      jar.Add(new Cookie("Cookie","Data","Removed","For privacy."));
      l("Loading cookie jar onto request");
      CookieContainer cont = new CookieContainer();
      cont.Add(jar);
      request.CookieContainer = cont;
      if (pdata != "")
      {
          request.Method = "POST";
          request.ContentType = "multipart/form-data;";
          request.ContentLength = pdata.Length;
          StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
          stOut.Write(pdata);
          stOut.Close();
      }
      HttpWebResponse response = (HttpWebResponse) request.GetResponse();
      Stream resStream = response.GetResponseStream();
      string tempString = null;
      int count = 0;
      do
      {
          count = resStream.Read(buf, 0, buf.Length);
          if (count != 0)
          {
              l("Data was read. ["+count+" bytes]. Encoding to ASCII");
              tempString = Encoding.ASCII.GetString(buf, 0, count);
              l("Appending to full string");
              sb.Append(tempString);
          }
      }
      while (count &gt; 0);
      return sb.ToString();
  }
  </code></pre>
  
  <p>The part of code that handles sending the post data:</p>
  
  <pre><code>if (pdata != "")
      {
          request.Method = "POST";
          request.ContentType = "multipart/form-data;";
          request.ContentLength = pdata.Length;
          StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
          stOut.Write(pdata);
          stOut.Close();
      }
      HttpWebResponse response = (HttpWebResponse) request.GetResponse();
  </code></pre>