Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我从手机上打开链接时,如何检查链接是否重定向到另一个URI_Java_Apache Httpclient 4.x - Fatal编程技术网

Java 当我从手机上打开链接时,如何检查链接是否重定向到另一个URI

Java 当我从手机上打开链接时,如何检查链接是否重定向到另一个URI,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,当我从手机(Android)打开链接时,我想检查链接是否重定向到另一个URI。我知道我测试过的那个网站,当我用手机打开它时,它的链接从“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) Ap

当我从手机(Android)打开链接时,我想检查链接是否重定向到另一个URI。我知道我测试过的那个网站,当我用手机打开它时,它的链接从“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();
}
HttpGet-HttpGet=newhttpget(url);
addHeader(“用户代理”、“Mozilla/5.0(Linux;Android 7.0;三星SM-G930F构建/NRD90M)AppleWebKit/537.36(KHTML,类似Gecko)三星浏览器/6.2 Chrome/56.0.2924.87 Mobile Safari/537.36”);
HttpClient HttpClient=HttpClients.createDefault();
HttpClientContext=HttpClientContext.create();
执行(httpGet,context);
List redirectURIs=context.getRedirectLocations();
if(redirectURIs!=null&&!redirectURIs.isEmpty()){
for(URI重定向URI:redirectURI){
System.out.println(“重定向URI:+redirectURI”);
}
URI mobileURI=redirectURIs.get(redirectURIs.size()-1);
返回mobileURI.toString();
}

我总是在mobileURI中接收空值。非常感谢您的帮助。

要测试页面加载后是否会重定向,您需要模拟目标(在您的情况下是移动)浏览器。您可以使用(org.seleniumhq.selenium:selenium服务器:3.4.0)和。例如:

@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();
    }
}
@测试
public void testSeleniumChromeDriver()引发IOException{
//创建Chrome驱动程序的新实例
System.setProperty(“webdriver.chrome.driver”,“/usr/bin/chromedriver”);
Map deviceMetrics=新HashMap();
设备测量。放置(“宽度”,360);
设备测量放置(“高度”,640);
deviceMetrics.put(“像素比率”,3.0);
Map mobileEmulation=new HashMap();
mobileEmulation.put(“deviceMetrics”,deviceMetrics);
mobileEmulation.put(“用户代理”、“Mozilla/5.0(Linux;Android 4.2.1;en-us;Nexus 5 Build/JOP40D)AppleWebKit/535.19(KHTML,类似Gecko)Chrome/18.0.1025.166 Mobile Safari/535.19”);
ChromeOptions ChromeOptions=新的ChromeOptions();
色度选项。设置实验选项(“移动模拟”,移动模拟);
WebDriver驱动程序=新的ChromeDriver(chromeOptions);
//获取页面
驱动程序。获取(“http://www.fishki.net");
试一试{
断言(driver.getCurrentUrl())是(“http://m.fishki.net/"));
}最后{
//关闭浏览器
driver.quit();
}
}

我在apache-httpclient-4.5上尝试了您的代码,它成功了……我正在使用org.apache.httpcomponents httpclient 4.5.3并尝试测试www.fishki.net。您尝试了哪个网站?相同。我试过
http://lenta.ru
重定向到
https://m.lenta.ru
。对于lenta.ru,它可以工作,但是对于fishki.net,它不能。你们知道这是怎么回事吗?似乎这个网站使用了一些客户端脚本来重定向,所以用户代理头并没有多大关系。。。