Reputation: 1406
It's strange because it was working like yesterday, but stopped working today (may be the website made some changes). I'm using this code in an Android app to post to a URL with proper parameters. Now, when it started showing 404 error I've tried various post 'headers', but still not working.
If you go to http://www.indianrail.gov.in/pnr_Enq.html and enter for example this PNR number: 2727059219, enter that plain TEXT captcha (yes, I know. Sigh.) and click 'Get Status' watching it in Firebug, you can see the parameters clearly. When I do almost exactly the same thing in my app though, it doesn't work anymore. Please note that the captcha is random 5 digits generated by a client script, and just matches if the user wrote the same. So anything, like '11111' in both (generated, and entered) works while sending post parameters.
Here's my code which was working until yesterday. The commented headers are the ones which I've tried now but still didn't work:
try
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi");
post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0");
post.setHeader("Accept","text/html,application/xhtml+xml,application/xml");
//post.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
post.setHeader("Accept-Language", "en-US,en;q=0.5");
post.setHeader("Accept-Encoding", "gzip, deflate");
//post.setHeader("Referer", "http://www.indianrail.gov.in/pnr_Enq.html");
//post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setHeader("Connection", "keep-alive");
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("lccp_cap_val", "11111"));
urlParameters.add(new BasicNameValuePair("lccp_capinp_val", "11111"));
urlParameters.add(new BasicNameValuePair("lccp_pnrno1", "2727059219")); //the PNR number taken from app, but hard coded to test
urlParameters.add(new BasicNameValuePair("submit", "Get+Status"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode() != 200)
throw new Exception("Error " + String.valueOf(response.getStatusLine().getStatusCode())); String result = EntityUtils.toString(response.getEntity());
}
catch (Exception ex)
{
mException = ex;
}
Can someone tell me where is the code wrong? And why does it work only from the browser?
Upvotes: 0
Views: 719
Reputation: 8617
You are using the wrong endpoint url, you have an extra character in there.
Change your URL:
Notice the removed 'r' inet_pn*r*stat_cgi.
I was able to replicate 404; with the changed url and it works - no longer 404.
Upvotes: 2