C#'da HttpWebResponse Location Değerini nasıl aldırabilirim?

emkoroglu (327) 10 yıl önce sordu

HttpWebRequest ile yapmış olduğum site linlerinde yönlendirme var. QueryString değerlerine göre yönlendirme yapıyor.

Siteye aşağıdaki link ile bir istekde bulunduğumda,

/index.php?option=com_&task=urun&ref=a&sid=CHS87GGM38&urunid=27708

Gelen yanıt ResponseHeaders linki

Location: index.php?option=com_&task=urun&ref=a&syf=JTF207-TRW-54

Location linkini sayfa yönlendirmesi yapmadan nasıl alabilirim?

 

Toplam 1 cevap


emkoroglu (327) 10 yıl önce cevapladı

Öncelikle yönlendirme olduğu için istekde bulunmadan önce bazı ayarlamalar yapmamız gerekiyor.

HttpWebRequest Istek = (HttpWebRequest)WebRequest.Create("http://www.uzmanim.net/index.php?&task=urun&ref");

 Yönlendirme linkini Response.Headers 'den alabilmemiz için Webrequestin "AllowAutoRedirect" false yapmamız gerekiyor.

Istek.AllowAutoRedirect = false;
var response = Istek.GetResponse();
string location = response.Headers[HttpResponseHeader.Location]; // Response Header Değerlerinden Location' nun değerini aldık.