0

我需要从 HttpResponse 获取响应 uri 并将其解析为名称-值对。我需要以下 .NET 片段的模拟:

string authorizationRequestParameters = string.Format("client_id={0}&response_type=code&scope={1}&access_type=offline", ClientID, Scope);
Uri authorizationRequestUri = new Uri(OauthHost.AbsoluteUri + "?" + authorizationRequestParameters);
HttpWebResponse authorizationResponse = DoGet(authorizationRequestUri, cookies);
NameValueCollection authorizeResponseParameters = HttpUtility.ParseQueryString(authorizationResponse.ResponseUri.Query);
string callbackCode = authorizeResponseParameters["code"];

必要条件是,Java 版本中的 DoGet 方法返回 apache HttpResponse。我尝试这样做的方式:

HttpResponse resp = hch.doGet("http://...?client_id=...&response_type=...&scope=...&access_type=...");
HttpEntity entity = resp.getEntity();
InputStream instream = entity.getContent();
System.out.print(IOUtils.toString(instream, "UTF-8"));

我可以通过这种方式接收 html 内容,但我只需要响应 Uri,它的一部分,它包含参数。我怎样才能做到这一点?

4

1 回答 1

0

似乎HttpWebResponse.ResponseUri属性来自Content-Location标头或请求URI。所以这样的事情应该有效:

final String uri = "http://...?client_id=...&response_type=...&scope=...&access_type=...";
final HttpResponse resp = hch.doGet(uri);
final String contentLocation = resp.getFirstHeader ("Content-Location");
final String responseURI = contentLocation != null ? contentLocation : uri;
于 2012-11-27T16:17:47.657 回答