0

I'm trying to connect to a website and to send many different post requests through different pages all over the website. I've reached the first step:connect the website. But now, I need to change the request url and keeping the same httpURLConnection.

For now my code is:

//openning the connection to the website
URL url = new URL("http://path/to/the/website");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        //preparing the post arguments
        Map<String, Object> params = new HashMap<>();
        params.put("Action", "connect_user");
        params.put("login", "login");
        params.put("password", "password");

        StringBuilder sb = new StringBuilder();
        for (Entry<String, Object> e : params.entrySet()) {
            if (sb.length() != 0)
                sb.append('&');
            sb.append(URLEncoder.encode(e.getKey(), "UTF-8"));
            sb.append('=');
            sb.append(URLEncoder.encode(String.valueOf(e.getValue()), "UTF-8"));
        }
        //writing the request and sending
        BufferedOutputStream writer = new BufferedOutputStream(conn.getOutputStream());
        writer.write(sb.toString().getBytes(Charset.forName("UTF-8")));
        writer.flush();

At this step i'm connected and sucessfully logged in. What I need to do now is to redirect the same connection to a second url. Thanks.

  • Take a look to [Alex Blex answer](https://stackoverflow.com/questions/33561221/maintain-session-between-httpurlconnection-calls-native-webview). – samabcde Oct 16 '18 at 14:40
  • well in fact it does not help me so much because the path is still forbidden. But it helped me a bit thanks. My real problem is about the forbidden error. –  Oct 16 '18 at 17:22
  • Try capture the request header in browser and compare with your program to see the different. – samabcde Oct 17 '18 at 04:14
  • I ´ve tried but i think the problem is that the website is treating the data on the first page on submit and then redirecting but it doesn’t redirect me with the program. –  Oct 17 '18 at 05:19

0 Answers0