0

I need to fetch content from third party web application UI but after login, site redirects to many pages. I am not understanding how to retrieve data from last opened page.

Presently I am receiving in.readLine() method returns null if I use "OPTIONS" instead of GET. If I use GET then error 405. Rest Client shows the connection success through GET method and also redirecting it to desired page.

Please suggest.

I connected to url through URLConnection

HttpsURLConnection con = (HttpsURLConnection) new URL("https://***.com/MRcgi/MRhomepage.pl" + "?" + query).openConnection();

Complete code is as follows-

            String charset = "UTF-8"; // Or in Java 7 and later, use the constant:

    String USER = "*****";
    String PROJECTID = "1";
    String MRP = "1eba539717f66151f557b49fd7e8a8d28";//dynamically changes
    String OPTION = "none";
    String WRITECACHE = "1";
    String FIRST_TIME_IN_FP = "1";
    String FIRST_TIME_IN_PROJ = "1";
    String dispatch_script = "MRlogin.pl";


    String query = String
            .format("USER=%s&PROJECTID=%s&MRP=%s&OPTION=%s&WRITECACHE=%s&FIRST_TIME_IN_FP=%s&FIRST_TIME_IN_PROJ=%s&dispatch_script=%s&",
                    URLEncoder.encode(USER, charset),
                    URLEncoder.encode(PROJECTID, charset),
                    URLEncoder.encode(MRP, charset),
                    URLEncoder.encode(OPTION, charset),
                    URLEncoder.encode(WRITECACHE, charset),
                    URLEncoder.encode(FIRST_TIME_IN_FP, charset),
                    URLEncoder.encode(FIRST_TIME_IN_PROJ, charset),
                    URLEncoder.encode(dispatch_script, charset));

    HttpsURLConnection con = (HttpsURLConnection) new URL(
            "https://***com/MRcgi/MRhomepage.pl" + "?" + query)
            .openConnection();


    String userPassword = "domain\\user:password";

    String encoding = new String(
            org.apache.commons.codec.binary.Base64
                    .encodeBase64(org.apache.commons.codec.binary.StringUtils
                            .getBytesUtf8(userPassword)));
    System.out.println("----" + encoding);


    con.setRequestMethod("GET");
    con.setRequestProperty("Content-Type", "text/plain");
    con.setRequestProperty("charset", "UTF-8");
            con.setRequestProperty("Authorization", "Basic " + encoding);

    USER=user&MRP=15c6ca083c2f75a73e0fbbd2832290f29&PROJECTID=1&USECACHEURL=1&IGNORE_REAL_ACTIVE_TIME=1";

    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    //wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(
            con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    System.out.println("-----" + in.readLine());
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    System.out.println(response.toString());

}
Raz
  • 33
  • 9

1 Answers1

0

setDoOutput(true) is used for POST and PUT requests. If it is false then it is for using GET requests. Its not required in my code, so I simply commented setDoOutput(true), and request went as GET

Also below url helped me a lot in understanding it- What exactly does URLConnection.setDoOutput() affect?

Raz
  • 33
  • 9