Post Comment on wordpress Blogs
I was working on a project in which I have to post comment on by wordpress 开发者_JAVA百科blog that should contain the text user entered in text box.I have been trying to user HttpWebRequest
but it fails and returns 404 not found. Even the link is not broken.here is my code
fore test purpose i have hard coded the entries in string post
string post = "author=" + HttpUtility.UrlEncode("afnan") + "&email=" + HttpUtility.UrlEncode("ifi@ifi.com") + "&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;
There are 3 problems with your code:
- the post data lacks 2 parameters
- the
WebRequest.Create
is not correct - the referrer url is missing; use
livehttpheaders
orhttpfox
to get the correct headers format!
精彩评论