0

I am checking a website for 302 messages, but I keep receiving 200 in my code:

private class Checker extends AsyncTask<Integer,Void,Integer>{
    protected void onPreExecute(){
        super.onPreExecute();
        //display progressdialog.
    }

    protected Integer doInBackground(Integer ...code){
        try {
            URL u = new URL ( "http://www.reddit.com/r/notarealurlinredditqwerty");
            HttpURLConnection huc =  (HttpURLConnection) u.openConnection();
            huc.setRequestMethod("POST");
            HttpURLConnection.setFollowRedirects(true);
            huc.connect();
            code[0] = huc.getResponseCode();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return code[0];
    }

    protected void onPostExecute(Integer result){
        super.onPostExecute(result);
        //dismiss progressdialog.
    }
}

That is my Async task for checking. Here is the code implementing it:

int code = -1;
Checker checker = new Checker();
try {
    code = checker.execute(code).get();
} catch (InterruptedException e) {
    e.printStackTrace();
} catch (ExecutionException e) {
    e.printStackTrace();
}
Log.d("code:", "" + code);

That log always returns 200, but I know that URL is 302 (it redirects to a search page on reddit.com). What am I doing wrong?

Eric Cochran
  • 8,414
  • 5
  • 50
  • 91
  • http://android-spirit.blogspot.in/2013/08/cosume-phppost-method-webservice-in.html watch this – Nirmal Sep 26 '13 at 05:20
  • It's not 302 it 404 - the page doesn't exist! but then reddit redirects you to a valid "404" page that's why you get 200 I guess. – Nir Alfasi Sep 26 '13 at 05:20
  • @alfasin Actually, it is 302 because it redirects to a search page automatically. Still, why is this giving me 200 (OK)? That should definitely not happen. – Eric Cochran Sep 26 '13 at 05:23
  • It shows the result HTTP code of the last page you reached which is 200 – Nir Alfasi Sep 26 '13 at 05:24
  • 1
    You're following the redirect - so your code will get a 302, follow it, then return the status code of the first page that doesn't send a redirect. – Sam Dufel Sep 26 '13 at 05:27
  • @SamDufel How do I do that? Can you post the code in an answer? Is there any way not to follow the redirect? – Eric Cochran Sep 26 '13 at 05:34
  • @NightlyNexus why `get()` in this `checker.execute(code).get()`. you should not use get() which does not make asynctask asynchronous – Raghunandan Sep 26 '13 at 05:35
  • @Raghunandan I am trying to return a value from the AysncTask. I thought that was the best way. Correct me if I am wrong, though. – Eric Cochran Sep 26 '13 at 05:37
  • @NightlyNexus you are wrong. Use a interface or make your asynctask an inner class of your activity class and then access data in `onPostExecute`. `get` will block the ui thread untill result is returned which you should not do. – Raghunandan Sep 26 '13 at 05:38
  • 1
    @Raghunandan Ah, thank you. That is much better. Now, just have to figure out why this is still getting a code of 200. – Eric Cochran Sep 26 '13 at 05:42
  • In httpclient you need to do the following: http://stackoverflow.com/a/1519487/1892046 . Don't know if it works for HttpUrlConnection though... – Bas Nov 21 '14 at 18:26

2 Answers2

0

This line just needs to be set to false:

HttpURLConnection.setFollowRedirects(false);
Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
0

You can use HttpURLConnection. I have used it for response code in a few applications. I did not encounter any problems.

URL url = new URL("http://yoururl.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
kksal55
  • 500
  • 7
  • 14