Java 服务器返回HTTP响应代码:405

Java 服务器返回HTTP响应代码:405,java,Java,我需要从第三方web应用程序用户界面获取内容,但在登录后,网站会重定向到许多页面。我不明白如何从上次打开的页面检索数据 目前,如果我使用“OPTIONS”而不是GET,则在.readLine()方法中接收到返回null的消息。 如果我使用GET,那么错误405。 Rest客户端通过GET方法显示连接成功,并将其重定向到所需页面 请建议 我通过URLConnection连接到url HttpsURLConnection con = (HttpsURLConnection) new URL("htt

我需要从第三方web应用程序用户界面获取内容,但在登录后,网站会重定向到许多页面。我不明白如何从上次打开的页面检索数据

目前,如果我使用“OPTIONS”而不是GET,则在.readLine()方法中接收到返回null的消息。 如果我使用GET,那么错误405。 Rest客户端通过GET方法显示连接成功,并将其重定向到所需页面

请建议

我通过URLConnection连接到url

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());

}
setDoOutput(true)用于POST和PUT请求。如果为false,则用于使用GET请求。 这在我的代码中不是必需的,所以我只是简单地注释了setDoOutput(true),请求作为GET执行

下面的url也帮助了我对它的理解-

您的服务器似乎不支持选项方法是的,但我希望它能用于GET方法,但不会获得任何成功。请建议如何使它工作。