I am working on a module that allows users to post comments on a blog published on WordPress. I looked at the HTML source for Post-Comment-Form displayed at the bottom of a blog entry (Leave a Reply section). Using that as a reference, I translated it to Java using DefaultHTTPClient
and BasicNameValuePairs
and my code looks like:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xycabz.wordpress.com/wp-comments-post.php");
httppost.setHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("author","abc"));
nvps.add(new BasicNameValuePair("email","abz@xyc.com"));
nvps.add(new BasicNameValuePair("url",""));
nvps.add(new BasicNameValuePair("comment","entiendamonos?"));
nvps.add(new BasicNameValuePair("comment_post_ID","123"));
//this was a hidden field and always set to 0
nvps.add(new BasicNameValuePair("comment_parent","0"));
try {
httppost.setEntity(new UrlEncodedFormEntity(nvps));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
BasicResponseHandler handler = new BasicResponseHandler();
try {
Log.e("OUTPUT",httpclient.execute(httppost,handler));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I get HTTP 302 Found
(Redirect to temporary location) exceptions in the logs with this code, which I ignore. (Note: Usually, when you post a comment(on the web page) you are taken back to the blog page that enlists all the comments. The URL I am getting in the redirects is the same) Even after ignoring this redirect, I can post comments on my personal blog using this code but not on the actual (production) blog.
Questions:
1. Could this be a post-a-comment settings problem(perhaps something the original blog owner might have set)?
2. How should HTTPClient handle 302 status code? Eventually, I just have to notify the user of success and failure and not actually take him to the comments page.
You can check for gravatar, akismet or some other comment related plugins creating issues for u.
Try checking the output using Poster and LiveHTTP headers extensions in Firefox for debugging the production website
Assuming wp-comments-post.php exist, the production site must be blocking direct access to wp-comments-post.php by either checking for HTTP_REFERER and/or HTTP_USER_AGENT. Check the .htaccess for the production site.