2011-05-24 111 views
0

我正在一個項目,我必須在wordpress博客發表評論,應包含在文本框中輸入的文本用戶。我一直在嘗試用戶HttpWebRequest,但它失敗,並返回404沒有找到。即使該鏈接不會broken.here是我的代碼發表評論在WordPress的博客

前測試的目的我已經硬編碼的條目string post

string post = "author=" + HttpUtility.UrlEncode("afnan") + "&email=" + HttpUtility.UrlEncode("[email protected]") + "&url=" + HttpUtility.UrlEncode("abcd.com") + 
        "&comment=" + HttpUtility.UrlEncode("no comments"); 
       HttpWebRequest wrWebRequest = WebRequest.Create("http://testing.autoprofitbot.com/blogtest/2011/05/13/call-3-computer-repair-services-put-to-test-4/wp-comments-post.php?") as HttpWebRequest; 

       wrWebRequest.Method = "POST"; 
       wrWebRequest.ContentLength = post.Length; 
       wrWebRequest.ContentType = "application/x-www-form-urlencoded"; 
       wrWebRequest.CookieContainer = new CookieContainer(); 

       //// Post to the login form. 
       StreamWriter swRequestWriter = new 
       StreamWriter(wrWebRequest.GetRequestStream()); 
       swRequestWriter.Write(post); 
       swRequestWriter.Close(); 

       // Get the response. 
       HttpWebResponse hwrWebResponse = 
       (HttpWebResponse)wrWebRequest.GetResponse(); 

       // Have some cookies. 
       CookieCollection ccCookies = hwrWebResponse.Cookies; 

       // Read the response 
       StreamReader srResponseReader = new 
       StreamReader(hwrWebResponse.GetResponseStream()); 
       string strResponseData = srResponseReader.ReadToEnd(); 
       srResponseReader.Close(); 
       webBrowser1.DocumentText = strResponseData; 

回答

1

有3個問題,你的代碼:

  1. 後數據缺少2個參數
  2. WebRequest.Create不正確
  3. 引薦網址丟失;使用livehttpheadershttpfox以獲得正確的標題格式!
+0

你可以做一個示例,演示簡單的表單發佈感謝 – 2011-05-25 23:37:51