我正在尝试使用HttpClient在Java中发出HTTP请求。每当网址重定向我时,我想知道原始请求的状态代码和最终网址。如果我不禁用重定向,则状态代码只是上次成功请求的状态代码:
var p1 = new System.Windows.Media.MediaPlayer();
p1.Open(new System.Uri(sound1.wav));
p1.Play();
Thread.Sleep(100);
var p2 = new System.Windows.Media.MediaPlayer();
p2.Open(new System.Uri(sound2.wav));
p2.Play();
但是,如果我禁用重定向处理,那么我将获得重定向状态:
// google.com = tinyurl.com
HttpUriRequest httpUriRequest = new HttpGet("https://google.com/cqvga");
HttpClient httpClient =
HttpClientBuilder.create()
.build();
HttpResponse response = httpClient.execute(httpUriRequest);
System.out.println(response.getStatusLine().getStatusCode()); // 200
System.out.println(response.getHeaders(HttpHeaders.LOCATION).length); // 0
无论如何,如果我没有编写自己的RedirectHandler来了解我的第一个请求的原始状态代码,和我登陆的最终网址?