2017-11-15 4 views
1

携帯電話(Android)からリンクを開くときにリンクが別のURIにリダイレクトされるかどうかを確認します。私がテストしたそのサイトが、携帯電話から開くと、そのリンクを「www.site.com」から「www.m.site.com」に変更することがわかります。携帯電話からリンクを開くときにリンクが別のURIにリダイレクトされるかどうかを確認する方法

私はこのコードを試してみましたが、それは動作しません:

HttpGet httpGet = new HttpGet(url); 
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Linux; Android 7.0; SAMSUNG SM-G930F Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/6.2 Chrome/56.0.2924.87 Mobile Safari/537.36"); 

HttpClient httpClient = HttpClients.createDefault(); 
HttpClientContext context = HttpClientContext.create(); 
httpClient.execute(httpGet, context); 
List<URI> redirectURIs = context.getRedirectLocations(); 
if (redirectURIs != null && !redirectURIs.isEmpty()) { 
    for (URI redirectURI : redirectURIs) { 
     System.out.println("Redirect URI: " + redirectURI); 
    } 
    URI mobileURI = redirectURIs.get(redirectURIs.size() - 1); 
    return mobileURI.toString(); 
} 

私はいつもmobileURIにnullを受けます。助けていただければ幸いです。

+1

私は、Apache-のHTTPClient-4.5を使用してコードを試してみましたが、それは私が org.apache.httpcomponents HTTPClientの 4.5.3を使用しています... – abel90

+0

を働きました とwww.fishki.netを試してみてください。試したウェブサイトはどれですか? – Geha

+0

同じです。私は 'http:// lenta.ru'が' https:// m.lenta.ru'にリダイレクトしようとしました。 – abel90

答えて

1

ロード後にページがリダイレクトされるかどうかをテストするには、ターゲット(モバイルの場合)ブラウザをエミュレートする必要があります。 SeleniumHQ(org.seleniumhq.selenium:selenium-server:3.4.0)とChrome Driverでこれを行うことができます。たとえば:

@Test 
public void testSeleniumChromeDriver() throws IOException { 
    // Create a new instance of the Chrome driver 
    System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver"); 
    Map<String, Object> deviceMetrics = new HashMap<>(); 
    deviceMetrics.put("width", 360); 
    deviceMetrics.put("height", 640); 
    deviceMetrics.put("pixelRatio", 3.0); 

    Map<String, Object> mobileEmulation = new HashMap<>(); 
    mobileEmulation.put("deviceMetrics", deviceMetrics); 
    mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"); 

    ChromeOptions chromeOptions = new ChromeOptions(); 
    chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation); 
    WebDriver driver = new ChromeDriver(chromeOptions); 

    // GET the page 
    driver.get("http://www.fishki.net"); 

    try { 
     assertThat(driver.getCurrentUrl(), is("http://m.fishki.net/")); 
    } finally { 
     //Close the browser 
     driver.quit(); 
    } 
} 
関連する問題