I am trying to verify all the links in a particular site in C# using the request-response technique. I notice开发者_开发技巧d that none of the links are returning "OK". I zeroed in on the problem. The site requires authentication. Knowing this, how should I be going about it????
Here is my code sample:
WebRequest objWebRequest;
HttpWebResponse objHttpResponse;
try
{
objWebRequest = (HttpWebRequest)WebRequest.Create(strCheckingLink);
objWebRequest.Timeout = 30000;
objHttpResponse = (HttpWebResponse)objWebRequest.GetResponse();
if (objHttpResponse.StatusCode == HttpStatusCode.Redirect)
{}
if (objHttpResponse.StatusCode == HttpStatusCode.OK)
{
httpSCode = (int)objHttpResponse.StatusCode;
httpMsg = "OK";
invalidLink = false;
}
else
{
invalidLink = true;
}
}
Do you want your verification code to correctly verify your website's pages?
In this case, you need to authenticate yourself to your website programmatically. Since the authentication is usually done via cookies, you need to obtain the authentication cookie somehow, and then attach it to every your request.
1, Timeout (MSDN) : A Domain Name System (DNS) query may take up to 15 seconds to return or time out. If your request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request.
2,Redirect : If a page redirected to another page ( status 301 and 302), HttpWebResponse will automatic get the finally page, so status is 200 (OK)
3, If page not found (404) or Server error (500) or other, will throw WebException
精彩评论