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","
[email protected]"));
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();
}
The above code works fine when I try it out on my blog. But when I try this on the actual blog, I get HTTP 302 Found (Redirect to temporary location) exceptions in the logs. The
comments never make it to the blog page. 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.
Questions:
1. Could this be a post-a-comment settings problem(perhaps something the original blog owner might have set)?
2. How should my 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.