title: How to escape URL-encoded data in POST with HttpWebRequest
body: <p>I am trying to send an URL-encoded post to a REST API implemented in PHP. The POST data contains two user-provided strings:</p>
  
  <pre><code>WebRequest request = HttpWebRequest.Create(new Uri(serverUri, "rest"));
  request.Method = "POST";
  request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
  request.Headers.Add("Content-Transfer-Encoding", "binary");
  
  // Form the url-encoded credentials we'll use to log in
  StringBuilder builder = new StringBuilder();
  builder.Append("user=");
  builder.Append(user);
  builder.Append("&amp;password=");
  builder.Append(password);
  byte[] credentials = Encoding.UTF8.GetBytes(builder.ToString());
  
  // Write the url-encoded post data into the request stream.
  request.ContentLength = credentials.Length;
  using (Stream requestStream = request.GetRequestStream()) {
    requestStream.Write(credentials, 0, credentials.Length);
  }
  </code></pre>
  
  <p>This sends a HTTP request to the server containing <code>user=myusername&amp;password=mypassword</code> in UTF-8 as its POST data.</p>
  
  <p>How can I escape the user-provided strings?
  For example, if I had a user named <code>big&amp;mean</code>, how should the ampersand be escaped so that it does not mess up the request line?</p>