2017-05-22 13 views
0

サードパーティのWebアプリケーションUIからコンテンツを取得する必要がありますが、ログイン後にはサイトが多くのページにリダイレクトされます。最後に開いたページからデータを取得する方法を理解していません。サーバーから返されたHTTP応答コード:405

現在、GETではなくOPTIONSを使用すると、in.readLine()メソッドはnullを返します。 GETを使用するとエラー405が発生します。 Rest Clientは、GETメソッドを使用して接続が成功したことを表示し、目的のページにリダイレクトします。

お勧めします。

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

} 
+1

HTTPS IT-理解で私をたくさん助けたGETよう要求が行ったではない://developer.mozilla。 org/ja-ja/docs/Web/HTTP/Status/405 – Tschallacka

+0

あなたのサーバーはOPTIONSメソッドをサポートしていないようです –

+0

はい、GETメソッドでは動作しますが、それを動作させる方法を提案してください。 – Raz

答えて

0

setDoOutput(真)を介してURLに接続をPOSTするために使用され、要求を入れて。 falseの場合は、GET要求を使用しています。 その私のコードに必要なので、私は単にsetDoOutput(true)をコメントし、そしてまた、以下のURL

What exactly does URLConnection.setDoOutput() affect?

関連する問題