2016-07-22 6 views
0

私は、ヘッダーを使用してリダイレクトURLを見つけようとしているので、私はいくつかの例の周りGoogleで検索し、見つかった:彼らが得る使用してリンクをリダイレクト取得HTTPClientのポスト

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の "Location"を私のバージョンから引き出すことはできません。私はここでそこを動かしてみましたが、パラメータを受け入れるメソッドは含まれていません。どのように取得できますか? httpClientを使用しているリダイレクトURL?あなたがテストすることができwww.google.comと単なるテスト用

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

設定AllowAutoRedirect:https://msdn.microsoft.com/en-us/library/system .net.httpwebrequest.allowautoredirect(v = vs.110).aspx "Location" –

+0

@x ... HttpWebResponseから "Location"を取得するにはどうすればよいですか? –

+1

HttpWebResponse.Headers:https://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.headers(v=vs.110).aspx –

答えて

0

、リダイレクト:falseに

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); 
    } 
関連する問題