2017-06-01 67 views
1

我正在使用一個應用程序,它連接了url並從頁面檢索數據。不幸的是,它沒有到達我感興趣的頁面。將url重定向到java代碼中的新url

如何通過java代碼中的GET方法將其重定向到期望的頁面?

輸出 「的ReadLine」 方法顯示了我它擊中/MRcgi/MRlogin.pl,但我希望去不同的網址

O/P

<TITLE> Service Core</TITLE> 
      <script type="text/javascript"> 
       var replace_function = function() { 
        window.location.replace('/MRcgi/MRlogin.pl?USER=***p&PASSWORD=webauth&PROJECTID=-1'); 
       } 

代碼 -

URL obj = new URL(url); 
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); 
    String userPassword = "*****"; 

    String encoding = new String(
      org.apache.commons.codec.binary.Base64 
        .encodeBase64(org.apache.commons.codec.binary.StringUtils 
          .getBytesUtf8(userPassword))); 



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


    // Send post request 
    /// 
    // con.setDoOutput(false); 
     /* DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
     System.out.println("1111" +wr); 
     wr.writeBytes(urlParameters); 
     wr.flush(); 
     wr.close();*/ 


    int responseCode = ((HttpURLConnection) con).getResponseCode(); 
    System.out.println("\nSending 'POST' request to URL : " + con); 
    //System.out.println("Post parameters : " + urlParameters); 
    System.out.println("Response Code : " + responseCode); 
    //window.location.replace(""); 

    BufferedReader in = new BufferedReader(new InputStreamReader(
      con.getInputStream())); 
    //con. 

    String inputLine; 
    StringBuffer response = new StringBuffer(); 
    System.out.println("-----" + in.readLine()); 
    while ((inputLine = in.readLine()) != null) { 
     System.out.println(inputLine); 
     //response.append(inputLine); 

    } 
    in.close(); 

回答

0

Raz,URL方法不遵循重定向,並且在指定一些額外的HTTP行爲時不那麼敏捷:請求類型,標頭等。

我建議您使用Apache HTTP Client來代替您的需求。這裏是an example with authentication from official docs

CredentialsProvider credsProvider = new BasicCredentialsProvider(); 
credsProvider.setCredentials(
     new AuthScope("httpbin.org", 80), 
     new UsernamePasswordCredentials("user", "passwd")); 
CloseableHttpClient httpclient = HttpClients.custom() 
     .setDefaultCredentialsProvider(credsProvider) 
     .build(); 
try { 
    HttpGet httpget = new HttpGet("http://httpbin.org/basic-auth/user/passwd"); 

    System.out.println("Executing request " + httpget.getRequestLine()); 
    CloseableHttpResponse response = httpclient.execute(httpget); 
    try { 
     System.out.println("----------------------------------------"); 
     System.out.println(response.getStatusLine()); 
     System.out.println(EntityUtils.toString(response.getEntity())); 
    } finally { 
     response.close(); 
    } 
} finally { 
    httpclient.close(); 
} 

注意:如果使用JDK7或更高版本,您可以使用try-with-resources隱式關閉資源。

+0

沒有它不適合我。我無法運行「httpclient.execute(httpget);」 。我收到一個錯誤 - 處理請求到{s}時發生I/O異常(java.net.SocketException) - > https://help.com:443:來自SOCKS服務器的格式錯誤的回覆 Jun 21,2017 11:33 :上午07點org.apache.http.impl.execchain.RetryExec執行 – Raz