title: Uploading a file to a web service using POST in C#
body: <p>The problem with this code is that the file, once it is uploaded, is not the correct format. I'm trying to upload a .zip file.</p>
  
  <p>public string HttpPost(string uri, string parameter)
          {<br>
              WebRequest webRequest = WebRequest.Create(uri);</p>
  
  <pre><code>        NetworkCredential credentials = new NetworkCredential("username", "password");
          webRequest.Credentials = credentials;
  
          webRequest.ContentType = "application/x-www-form-urlencoded";
          webRequest.Method = "POST";
  
          byte[] bytes = Encoding.ASCII.GetBytes(parameter);
          Stream os = null;
          try
          { // send the Post
              webRequest.ContentLength = bytes.Length;   //Count bytes to send
              os = webRequest.GetRequestStream();
              os.Write(bytes, 0, bytes.Length);         //Send it
          }
          catch (WebException ex)
          {
              MessageBox.Show(ex.Message, "HttpPost: Request error",
                 MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
          finally
          {
              if (os != null)
              {
                  os.Close();
              }
          }
  
          try
          { // get the response
              WebResponse webResponse = webRequest.GetResponse();
              if (webResponse == null)
              { return null; }
              StreamReader sr = new StreamReader(webResponse.GetResponseStream());
              return sr.ReadToEnd().Trim();
          }
          catch (WebException ex)
          {
              MessageBox.Show(ex.Message, "HttpPost: Response error",
                 MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
          return null;
      } 
  </code></pre>