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

时间:2017-05-22 19:10:22

标签: java

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

目前我接收的in.readLine()方法如果我使用“OPTIONS”而不是GET则返回null。 如果我使用GET然后错误405。 Rest Client通过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());

}

1 个答案:

答案 0 :(得分:0)

setDoOutput(true)用于POST和PUT请求。如果它是假的那么它是用于使用GET请求。 它在我的代码中不是必需的,所以我只是评论了setDoOutput(true),并且请求变为GET

同样在url下方帮助我理解它 - What exactly does URLConnection.setDoOutput() affect?