2012-07-09 125 views
3

我想在後面的代碼中發表帖子。我有簡單的HTML文章正在工作,但是當我嘗試使WebRequest我無法使其工作。.net post form in code behind

感謝您的時間提前。

這是工作>

<!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> 

這不是工作>

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(); 

編輯:實驗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); 

實驗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(); 

實驗3 !!!工作!

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(); 
+0

# 3看起來接近完成... add req.Method =「POST」; – 2012-07-09 12:28:58

+0

#1和#2沒有發佈正確的表單,因爲服務器期望。 – 2012-07-09 12:31:13

回答

5

我看到錯誤的第一件事就是您嘗試使用查詢字符串發佈表單數據。你的「表單數據」應該是這樣的:

String ali = "datafromuser=<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>"; 

接下來,你需要從表單數據中獲取字節[]。

byte[] byteArray = Encoding.UTF8.GetBytes(ali); 

設置一些標題:

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(); 

終於...得到您的迴應。還要注意,任何實現IDisposable的應該包含在using聲明中,即StreamWebResponse

另請注意,提交按鈕不是您的表單發佈數據的一部分。服務器可能期待它。

編輯:下面是來自Microsoft的完整示例,它將逐步引導您。

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

+0

byte [] byteArray = System.Text.Encoding.UTF8.GetBytes(ali); WebRequest req.ContentType =「application/x-www-form-urlencoded」; req.ContentLength = byteArray.Length; Stream dataStream = req.GetRequestStream(); dataStream.Write(byteArray,0,byteArray.Length); dataStream.Close(); HttpWebR esponse response =(HttpWebResponse)req.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); String responseString = reader.ReadToEnd(); response.Close(); GetrequestStream線路遊戲錯誤>無法用此動詞類型發送內容主體。 – Mert 2012-07-09 12:13:09

+0

@Mert - 請用你的新代碼更新你的答案,而不是覆蓋舊的東西......只是將它附加到底部,就像「編輯 - 嘗試以下並得到錯誤<你的錯誤在這裏> – 2012-07-09 12:18:36

+0

好的,我會感謝 – Mert 2012-07-09 12:20:23

1

這可能對你有幫助。

string URI = "http://www.myurl.com/post.php"; 
string myParamters = "param1=value1&param2=value2"; 

WebClient wc = new WebClient(); 
wc.Headers["Content-type"] = "application/x-www-form-urlencoded"; 
string HtmlResult = wc.UploadString(URI, myParameters); 
+1

我同意,WebClient對初學者來說可能會容易得多。 – 2012-07-09 11:58:57

+0

默特爾,請讓我知道它是否對你有幫助。 – 2012-07-09 12:02:13

+0

剛剛嘗試抱歉,遲到的答案,答案是「\ n」這意味着我們不能使它的工作:(也不應該我指定「datafromuser」在某些? – Mert 2012-07-09 12:19:01

0

這是simples例子。 POSTDATA取決於形式

String postData = "Name=" + Username + 
       "&Password=" + Password + 
       "&Retype=" + Password + 
       "&Email=" + HttpUtility.UrlEncode(EmailAddress) + 
       "&RealName=" + String.Format("{0}+{1}", FirstName.Replace(" ", "+"), LastName.Replace(" ", "+")); 

現在創建請求後的數據:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 
request.Method = WebRequestMethods.Http.Post; 
request.ContentLength = postData.Length; 
request.ContentType = "application/x-www-form-urlencoded"; 
request.KeepAlive = false; 

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(); 

可以爲了趕POSTDATA看起來如何使用Fiddler。

+0

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 responseString > \ n沒有工作:( – Mert 2012-07-09 12:08:47

+0

你可以給我的URL你想通過代碼做http發佈嗎? – 2012-07-09 12:12:18

+0

這是問題發佈別人的例子。 @Mert,你需要使用你的元素名稱,即「datafromuser = {文本區域中的所有內容}」 – 2012-07-09 12:15:17

0

試試這個。

using (WebClient client = new WebClient()) 
    { 

     byte[] response = client.UploadValues("http://dork.com/service", new NameValueCollection() 
     { 
      { "home", "Cosby" }, 
      { "favorite+flavor", "flies" } 
     }); 
    } 

,將需要這些包括:

using System; 
using System.Collections.Specialized; 
using System.Net; 

如果你在使用一個靜態方法/班堅持:

public static class Http 
{ 
    public static byte[] Post(string uri, NameValueCollection pairs) 
    { 
     byte[] response = null; 
     using (WebClient client = new WebClient()) 
     { 
      response = client.UploadValues(uri, pairs); 
     } 
     return response; 
    } 
} 

然後簡單:

Http.Post("http://dork.com/service", new NameValueCollection() { 
    { "home", "Cosby" }, 
    { "favorite+flavor", "flies" } 
}); 
+0

Ref Url:http://stackoverflow.com/questions/4088625/net-simplest-way-to-send-post-with-data-and-read-response for above – 2012-07-09 12:37:06