2016-08-06 20 views
1

嗨,每一个它已经两天了,现在我正在尝试解决我的问题。 我有url链接,我试图用AsyncHttpClient类调用,我需要等待链接所做的重定向。问题在于http get请求返回一个http响应200和http内容而不等待重定向。请问你能帮帮我吗?
下面是代码:
Java非常等待http重定向

private static String getAsyncRedirectedUrlFromUrl(String url) { 
    AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient(); 
    Future<Response> response = asyncHttpClient.prepareGet(url).execute(new AsyncCompletionHandler<Response>() { 

     @Override 
     public Response onCompleted(Response response) throws Exception { 
      // Do something with the Response 
      // ... 
      response.getLocalAddress(); 
      response.getStatusCode(); 
      return response; 
     } 

     @Override 
     public void onThrowable(Throwable t) { 
      // Something wrong happened. 
     } 
    }); 
    try { 
     System.out.println("Redirection:" + response.get().getHeader("location")); 
     return response.get().getHeader("location"); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return ""; 
    } 

这里是请求链接:Request LINK
这里是预期结果重定向链接:Expect LINK

感谢你的帮助,它会非常有帮助。

回答

0

您提供的请求链接确实返回一个普通的200 OK,而不是重定向。 您使用浏览器遇到的重定向是由javascript触发的,它不是HTTP重定向(301等)。

AsyncHttpClient是一个HTTP客户端,而不是浏览器,所以它不会执行JS。

如果你想遵循这样的重定向,你必须解析页面,抓取目标url,并手动发送第二个请求。

+0

感谢您的帮助:-),我已经能够解决与Selenium的问题。 – Mehdi