HttpClient should be redirected

I am currently working on a small project. The goal of the project is to enter the site and receive / process information on the site. Moreover, I would like to open some links and search through them.

The server side is as follows:

You need to go to php website. Upon successful login, you get a session and redirect to foo.username.bar.php (changed).

With this code:

BufferedReader in = null; String data = null; try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("username", user)); nameValuePairs.add(new BasicNameValuePair("passwort", pass)); HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(website); request.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = client.execute(request); in = new BufferedReader(new InputStreamReader(response.getEntity() .getContent())); StringBuffer sb = new StringBuffer(""); String l = ""; String nl = System.getProperty("line.separator"); while ((l = in.readLine()) != null) { sb.append(l + nl); } in.close(); data = sb.toString(); return data; } finally { if (in == null) { try { in.close(); return "ERROR"; } catch (Exception e) { e.printStackTrace(); } } } } 

I successfully logged in to the site. However, I can only see the source page of the php user confirmation site, which tells me that I have been successfully registered and will be redirected.

Now I really want to see the source of the page to which I was redirected, and save the connection to open a few more links on this page.

Do you have an idea how I can solve this?

+4
source share
1 answer

You need to redirect (state 302) returned by the response. Check out this answer .

+2
source

Source: https://habr.com/ru/post/1497859/


All Articles