我正在尝试使用标题找到重定向网址,所以我用Google搜索并找到了一些示例:
Header[] arr = httpResponse.getHeaders("Location");
for (Header head : arr){
String whatever = arr.getValue();
}
和
HttpPost request1 = new HttpPost("https://hrlink.healthnet.com/");
HttpResponse response1 = httpclient.execute(request1);
// expect a 302 response.
if (response1.getStatusLine().getStatusCode() == 302) {
String redirectURL = response1.getFirstHeader("Location").getValue();
// no auto-redirecting at client side, need manual send the request.
HttpGet request2 = new HttpGet(redirectURL);
HttpResponse response2 = httpclient.execute(request2);
... ...
}
他们从标题中获取“位置”,但是我无法从我的版本中拉出HttpResponseMessage中的“位置”,我尝试在这里和那里移动东西,但它不包含接受参数的方法,我如何使用httpClient获取重定向网址?
var client = new HttpClient();
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("username", "---"),
new KeyValuePair<string, string>("password", "---")
};
var content = new FormUrlEncodedContent(pairs);
var response = client.PostAsync(uri, content).Result;
HttpHeaders headerlist = response.Headers;
foreach (var header in headerlist)
{
//Red line on header("Location")
label1.Text += header("Location") + "\n";
}
答案 0 :(得分:0)
只是为了测试,您可以使用www.google.com测试重定向:
var request = (HttpWebRequest) WebRequest.Create("http://www.google.com");
request.AllowAutoRedirect = false;
using (var response = (HttpWebResponse) request.GetResponse())
{
string location = response.Headers["Location"];
Console.WriteLine(location);
}