獲得響應的URI參數我發送一個HTTP GET /使用HEAD請求Apache的HttpClient的4.x版我送包含「http://example.com/getAccessToken」的URL的請求。我期待的迴應是重定向URL在如「http://redirecturl.com/?code=accessTokenStuff」返回的URL參數。我希望能夠解析響應重定向url參數,即我想獲得「accessTokenStuff」。我怎樣才能做到這一點?從阿帕奇的HttpResponse
HttpClient client = new DefaultHttpClient();
HttpHead request = new HttpHead(authUrl);
HttpResponse response = client.execute(request);
System.out.println(response.getStatusLine().getStatusCode());//returns 200
request.releaseConnection();
簡而言之:我想要的是執行原始網址,然後得到結果,這是另一個具有名爲「代碼」參數的網址。然後我想獲取該參數的值。
編輯:
我也試過,但它返回相同的原始URL
DefaultHttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
HttpClientParams.setRedirecting(params, false);
HttpGet request = new HttpGet(authUrl);
HttpResponse response = client.execute(request);
String location = response.getLastHeader("Location").getValue();//returns same original url
System.out.println(location);
request.releaseConnection();
設置HttpClientParams.setRedirecting(params, true);
返回NULL
見 http://stackoverflow.com/questions/1456987/httpclient-4-how-to-capture-last-redirect-url –