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.
Thanks for you time in advance.
this is working >
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<body>
<form action="http://www.pay-pos.com/test.php" method="post">
<textarea name="datafromuser">
<CC5Request><Name>Mert</Name><Password>123</Password><ClientId>1058</ClientId><IPAddress>213.456.654.456</IPAddress><Adress>asdsa</Adress><OrderId>123</OrderId><Type>Auth</Type>
<Number>4522115422659661</Number><ExpiresAy>01</ExpiresAy><ExpiresYil>13</ExpiresYil><Cvv2Val>123</Cvv2Val><Total>10</Total><Taksit></Taksit><Kdv>18</Kdv><BankaID>1</BankaID>
<TcKimlik>12345678912</TcKimlik></CC5Request>
</textarea>
<input type="submit" value="aaa" />
</form>
</body>
</html>
This isn't working >
String ali = "<CC5Request><Name>Mert</Name><Password>123</Password><ClientId>xxxx</ClientId><IPAddress>213</IPAddress><Adress>asdsa</Adress>" +
"<OrderId>123</OrderId><Type>Auth</Type><Number>1234567891234567</Number><ExpiresAy>01</ExpiresAy><ExpiresYil>13</ExpiresYil><Cvv2Val>123</Cvv2Val>" +
"<Total>10</Total><Taksit></Taksit><Kdv>xx</Kdv><BankaID>1</BankaID><TcKimlik>12345678912</TcKimlik></CC5Request>";
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();
EDIT: EXPERIMENT 1
String postData = "Name=Mert" +
"&Password=123" +
"&ClientId=1058" +
"&IPAddress=213.456.654.456" +
"&Adress=asdsa" +
"&OrderId=123" +
"&Type=Auth" +
"&Number=4522115422659661" +
"&ExpiresAy=01" +
"&ExpiresYil=13" +
"&Cvv2Val=123" +
"&Total=10" +
"&Taksit=1" +
"&Kdv=18" +
"&BankaID=1" +
"&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);
EXPERIMENT 2
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();
EXPERIMENT 3 !!!WORKING!!!
String ali = "datafromuser=<CC5Request><Name>Mert</Name><Password>123</Password><ClientId>1058</ClientId><IPAddress>213.456.654.456</IPAddress><Adress>asdsa</Adress><OrderId>123</OrderId><Type>Auth</Type>" +
"<Number>4522115422659661</Number><ExpiresAy>01</ExpiresAy><ExpiresYil>13</ExpiresYil><Cvv2Val>123</Cvv2Val><Total>10</Total><Taksit></Taksit><Kdv>18</Kdv><BankaID>1</BankaID>"+
"<TcKimlik>12345678912</TcKimlik></CC5Request>";
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();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…