title: .net post form in code behind
body: <p>I wanna make a post form in code behind. I have simple html post is working but when I try make it WebRequest I can't make it work.</p>
  
  <p>Thanks for you time in advance.</p>
  
  <p>this is working > </p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
  &lt;head&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form action="http://www.pay-pos.com/test.php" method="post"&gt;
  &lt;textarea name="datafromuser"&gt;
  &lt;CC5Request&gt;&lt;Name&gt;Mert&lt;/Name&gt;&lt;Password&gt;123&lt;/Password&gt;&lt;ClientId&gt;1058&lt;/ClientId&gt;&lt;IPAddress&gt;213.456.654.456&lt;/IPAddress&gt;&lt;Adress&gt;asdsa&lt;/Adress&gt;&lt;OrderId&gt;123&lt;/OrderId&gt;&lt;Type&gt;Auth&lt;/Type&gt;
  &lt;Number&gt;4522115422659661&lt;/Number&gt;&lt;ExpiresAy&gt;01&lt;/ExpiresAy&gt;&lt;ExpiresYil&gt;13&lt;/ExpiresYil&gt;&lt;Cvv2Val&gt;123&lt;/Cvv2Val&gt;&lt;Total&gt;10&lt;/Total&gt;&lt;Taksit&gt;&lt;/Taksit&gt;&lt;Kdv&gt;18&lt;/Kdv&gt;&lt;BankaID&gt;1&lt;/BankaID&gt;
  &lt;TcKimlik&gt;12345678912&lt;/TcKimlik&gt;&lt;/CC5Request&gt;
  &lt;/textarea&gt;
  &lt;input type="submit" value="aaa" /&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This isn't working ></p>
  
  <pre><code>String ali = "&lt;CC5Request&gt;&lt;Name&gt;Mert&lt;/Name&gt;&lt;Password&gt;123&lt;/Password&gt;&lt;ClientId&gt;xxxx&lt;/ClientId&gt;&lt;IPAddress&gt;213&lt;/IPAddress&gt;&lt;Adress&gt;asdsa&lt;/Adress&gt;" +
                          "&lt;OrderId&gt;123&lt;/OrderId&gt;&lt;Type&gt;Auth&lt;/Type&gt;&lt;Number&gt;1234567891234567&lt;/Number&gt;&lt;ExpiresAy&gt;01&lt;/ExpiresAy&gt;&lt;ExpiresYil&gt;13&lt;/ExpiresYil&gt;&lt;Cvv2Val&gt;123&lt;/Cvv2Val&gt;" +
                          "&lt;Total&gt;10&lt;/Total&gt;&lt;Taksit&gt;&lt;/Taksit&gt;&lt;Kdv&gt;xx&lt;/Kdv&gt;&lt;BankaID&gt;1&lt;/BankaID&gt;&lt;TcKimlik&gt;12345678912&lt;/TcKimlik&gt;&lt;/CC5Request&gt;";
                      WebRequest req = WebRequest.Create("http://www.pay-pos.com/test.php?datafromuser=" + ali);
                      req.Credentials = CredentialCache.DefaultCredentials;
                      req.Method = "POST";
                      HttpWebResponse loWebResponse = (HttpWebResponse)req.GetResponse();
                      Encoding enc = Encoding.GetEncoding(1254); 
  
                      StreamReader loResponseStream =
  
                         new StreamReader(loWebResponse.GetResponseStream(), enc);
  
                      string lcHtml = loResponseStream.ReadToEnd();
                      loWebResponse.Close();
                      loResponseStream.Close();
  </code></pre>
  
  <p>EDIT: EXPERIMENT 1</p>
  
  <pre><code>String postData = "Name=Mert" +
                "&amp;Password=123" +
                "&amp;ClientId=1058" +
                "&amp;IPAddress=213.456.654.456" +
                "&amp;Adress=asdsa" +
                "&amp;OrderId=123" +
                "&amp;Type=Auth" +
                "&amp;Number=4522115422659661" +
                "&amp;ExpiresAy=01" +
                "&amp;ExpiresYil=13" +
                "&amp;Cvv2Val=123" +
                "&amp;Total=10" +
                "&amp;Taksit=1" +
                "&amp;Kdv=18" +
                "&amp;BankaID=1" +
                "&amp;TcKimlik=12345678912";
  
              string URI = "http://www.pay-pos.com/test.php";
  
              WebClient wc = new WebClient();
              wc.Headers["Content-type"] = "application/x-www-form-urlencoded";
              string HtmlResult = wc.UploadString(URI, postData);
  </code></pre>
  
  <p>EXPERIMENT 2 </p>
  
  <pre><code> HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.pay-pos.com/test.php");
          request.Method = WebRequestMethods.Http.Post;
          request.ContentLength = postData.Length;
          request.ContentType = "application/x-www-form-urlencoded";
          request.KeepAlive = false; //also tried true as well
  
          StreamWriter writer = new StreamWriter(request.GetRequestStream());
          writer.Write(postData);
          writer.Close();
          HttpWebResponse response = (HttpWebResponse)request.GetResponse();
          StreamReader reader = new StreamReader(response.GetResponseStream());
          String responseString = reader.ReadToEnd();
  
          response.Close();
  </code></pre>
  
  <p>EXPERIMENT 3 !!!WORKING!!!</p>
  
  <pre><code>String ali = "datafromuser=&lt;CC5Request&gt;&lt;Name&gt;Mert&lt;/Name&gt;&lt;Password&gt;123&lt;/Password&gt;&lt;ClientId&gt;1058&lt;/ClientId&gt;&lt;IPAddress&gt;213.456.654.456&lt;/IPAddress&gt;&lt;Adress&gt;asdsa&lt;/Adress&gt;&lt;OrderId&gt;123&lt;/OrderId&gt;&lt;Type&gt;Auth&lt;/Type&gt;" +
  "&lt;Number&gt;4522115422659661&lt;/Number&gt;&lt;ExpiresAy&gt;01&lt;/ExpiresAy&gt;&lt;ExpiresYil&gt;13&lt;/ExpiresYil&gt;&lt;Cvv2Val&gt;123&lt;/Cvv2Val&gt;&lt;Total&gt;10&lt;/Total&gt;&lt;Taksit&gt;&lt;/Taksit&gt;&lt;Kdv&gt;18&lt;/Kdv&gt;&lt;BankaID&gt;1&lt;/BankaID&gt;"+
  "&lt;TcKimlik&gt;12345678912&lt;/TcKimlik&gt;&lt;/CC5Request&gt;";
  
              byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(ali);
  
              WebRequest req = WebRequest.Create("http://www.pay-pos.com/test.php");
  
              req.ContentType = "application/x-www-form-urlencoded"; 
              req.ContentLength = byteArray.Length;
              req.Method = "POST"; 
  
              Stream dataStream = req.GetRequestStream(); 
              dataStream.Write(byteArray, 0, byteArray.Length); 
              dataStream.Close();
  
              HttpWebResponse response = (HttpWebResponse)req.GetResponse();
              StreamReader reader = new StreamReader(response.GetResponseStream());
              String responseString = reader.ReadToEnd();
  
              response.Close();
  </code></pre>