0

WebRequest을 사용하여 HTML 사이트를 읽습니다. 서버가 내 요청을 리디렉션하고있는 것 같습니다.WebRequest - 리디렉션 방지

내 코드는 다음과 유사합니다

String URI = "http://www.foo.com/bar/index.html" 
    WebRequest req = WebRequest.Create(URI); 
    WebResponse resp = req.GetResponse(); 
    StreamReader sr = new StreamReader(resp.GetResponseStream()); 
    String returnedContent = sr.ReadToEnd(); 

나는 returnedContent의 내용을 확인하는 경우가 http://www.foo.com/FOO_BAR/index "와 같은 재에서 콘텐츠가 포함되어 있습니다. html ". 수신 된 응답 (IFrame)의 일부이므로 요청한 URL이 존재한다고 확신합니다.

WebResponse의 리디렉션을 방지하고 요청한 URL의 콘텐츠를 가져 오는 방법이 있습니까?

302 Found 상태 코드에 req.AllowAutoRedirect = false 리드를 설정 UPDATE

하지만 acutal 콘텐츠를 제공하지 않습니다.

일부 자세한 내용

:

<body> 
    <div> 
     <iframe src="/foo/index.html"></iframe> 
    </div> 
</body> 
+1

체크 아웃 http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowautoredi rect.aspx – Matthew

답변

5

당신은 HttpWebRequestAllowAutoRedirect property를 사용할 수 있습니다 :

내 요청 된 URL 내가받은 내용을 응답이 비슷합니다 http://www.foo.com/FOO_BAR/index.html

에 위치한 http://www.foo.com/bar/index.html했다

… 
var req = (HttpWebRequest)WebRequest.Create(URI); 
req.AllowAutoRedirect = false; 
… 
+0

내용 대신 "302 Found"Repsonse 코드가 표시됩니다 - 어떤 아이디어입니까? – Jaster

+0

@Jaster : 예, 리디렉션 코드입니다. 콘텐츠로 리디렉션하고 싶지 않은 경우 콘텐츠를 찾을 위치는 어디입니까? '