Network request intercepts 302 jump and obtains Location

  When I was working on wireless WiFi recently, there was a need. When the mobile phone is connected to the wireless WiFi, you can jump
to the authentication login page by accessing a URL at will. In this process, a 302 reload of AC is actually triggered first. Orientation, and then return to the visited (location) URL web page. Now I want to get this 302 redirect URL (Location) on the app. How should I achieve it? This problem has troubled Xiaobai for a lot of time, and it is also because of my own I didn’t understand this principle, but finally found a method. I’ll record it here:

1. Obtain the 302 jump address through HttpURLConnection interception

            String url = "";  
            System.out.println("访问地址:" + url);  
            URL serverUrl = new URL(url);  
            HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection();  
            conn.setRequestMethod("GET");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            // 必须设置false,否则会自动redirect到Location的地址  
            conn.setInstanceFollowRedirects(false);  
            //获取Location地址
            String location = conn.getHeaderField("Location");

2. Prevent Url redirection method through HttpClient4.5

            RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false).build();//不允许重定向  
            CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();  
            //使用httpClient处理请求  
            HttpResponse response = httpClient.execute(new HttpGet("http://...."));​  

3. Redirection and setEnableRedirects method exception resolution in AsyncHttpClient

http://blog.csdn.net/zhouzme/article/details/20400673

4. Use OkHttp for redirection interception processing

https://www.jianshu.com/p/d04bfd6b6146
http://www.apkbus.com/blog-927424-76833.html

Reprinted from:
http://blog.csdn.net/u014727709/article/details/79382505
Welcome to start, comments and corrections welcome

Guess you like

Origin blog.csdn.net/u014727709/article/details/79382505