当我从移动 phone 打开它时,如何检查 link 是否重定向到另一个 URI
How to check if link redirects to another URI when I open it from mobile phone
我想检查当我从移动 phone (Android) 打开它时 link 是否重定向到另一个 URI。我知道我测试过的那个网站,当我从手机 phone.
打开它时,它的 link 从 "www.site.com" 更改为 "www.m.site.com"
我试过这段代码,但它不起作用:
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。如果有任何帮助,我将不胜感激。
要测试页面加载后是否会重定向,您需要模拟目标(在您的情况下为移动)浏览器。您可以使用 SeleniumHQ (org.seleniumhq.selenium:selenium-server:3.4.0) and 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();
}
}
我想检查当我从移动 phone (Android) 打开它时 link 是否重定向到另一个 URI。我知道我测试过的那个网站,当我从手机 phone.
打开它时,它的 link 从 "www.site.com" 更改为 "www.m.site.com"我试过这段代码,但它不起作用:
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。如果有任何帮助,我将不胜感激。
要测试页面加载后是否会重定向,您需要模拟目标(在您的情况下为移动)浏览器。您可以使用 SeleniumHQ (org.seleniumhq.selenium:selenium-server:3.4.0) and 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();
}
}