Reputation: 1836
I am trying add a feature to add comments in my app for my blog. I was trying to achieve this through httpclient, but i am continuously failing to achieve this. Here is my code:
HttpClient client = new DefaultHttpClient();
HttpPost http = new HttpPost("http://universityoftrollogy.wordpress.com/wp-comments-post.php");
http.setHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("_wp_http_referer", referer));
nameValuePairs.add(new BasicNameValuePair("hc_post_as", "guest"));
nameValuePairs.add(new BasicNameValuePair("comment_post_ID", postId));
nameValuePairs.add(new BasicNameValuePair("comment_parent","0"));
nameValuePairs.add(new BasicNameValuePair("comment", cData));
nameValuePairs.add(new BasicNameValuePair("email", cEmail));
nameValuePairs.add(new BasicNameValuePair("author", cName));
http.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(http);
My client executes properly but I cannot see any comments on my post! I am not sure why isn't it working but a possible reason may be that it is not handling redirects.
Can anyone help me by guiding me to the right way to achieve it? Any help would be greatly appreciated.
Edit : After checking my response, I found that the status line for response is 500.
Upvotes: 0
Views: 221
Reputation: 3890
When you work against a wordpress.com blog, you will move from one bug to the next, without being able to diagnose the errors. So my advise: Set up your own webserver and wordpress-blog on your development machine. It is not difficult. Then you can check the error-log of your webserver what wordpress did not like.
And yes, you probably need a "nonce". You can read about in in the codex-page. But remember that the codex-page is written from the perspective of a wordpress-plugin-developer.
Upvotes: 1
Reputation: 1746
there is a parameter called xxx_none to validate user in wordpress. It is generated randomly, I think, when a client open a session.
you may need to try to open the page once, then parse that parameter (I think in your page, it is "highlander_comment_nonce"), then put it into the http request too?
Tip: if you are using chrome, there is tool in the developer tool which help you to copy the request in the cUrl format, so you can see all the parameters sent to server.
Upvotes: 0