I try to get response codes from links which I extract from an email. I use the System.Net
lib to accomplish the response code extraction. While debugging and testing my code I ran into the System.Net.WebException "The operation has timed out"
exception. According to this question I have to set the timeout to a minimum of about 15 seconds because the DNS query can take up to 15 or more seconds to return the requested value.
In my code I set the timeout's value to 20 seconds (I tested with 60, then 30 seconds in before) and got the same timeout exception every try. I have to mention, that I request response codes from "similar" websites before I run into the timeout exceptions. This is the order of websites which I request information from:
- https://www.google.com/
- https://www.google.com/
- https://www.google.de/
- https://www.google.ded/ (=> throws exception as intended)
- http://www.google.de/
- http://www.google.de/
- http://www.google.de/
- http://www.google.de/
- http://www.google.de/
7th request and following return: WebException "The operation has timed out"
.
I assume that frequently requesting status codes from the same or similar link causes me to time out. Which leads me to my final questions:
Do i have to set my timeout to a specific amount of time, are there limited amounts of request per client in a set time span, should it work if I am using similar links referring to different sites on a website (examples underneath) or is there a simple work-around to avoid these problems?
Examples:
"foo.com/a" | "foo.com/b" | "foo.com/c" | "foo.com/a/d"
EDIT:
So I just tried to execute my code by using a new email which has several different links in it and I didn't run into any WebException "The operation has timed out"
problems.
Therefore I'd say that i get timeouts based on the amount of requests to a website in a short amount of time but I still have no exact explanation for this.
Feel free to share your thoughts on this occurrence (and maybe explanations for what is happening exactly)
This is the code I use to get the response code:
public static List<LinkRespCode> GetStatusCodes(string mail)
{
List<LinkRespCode> li = new List<LinkRespCode>();
try
{
Link[] obtainedLinks = Link_Obtainer.GetLinksTextHtml(mail);
string[] links = new string[obtainedLinks.Length];
var c = 0;
foreach (Link link in obtainedLinks)
links[c++] = link.HrefLink;
string startUrl = "https://pastebin.com/", newLink = string.Empty;
Uri uri = new Uri(startUrl);
for(int i = 0; i < links.Length; i++)
{
try
{
if (!links[i].StartsWith("http://") && !links[i].StartsWith("https://"))
newLink = "http://" + links[i];
else
newLink = links[i];
uri = new Uri(newLink);
if (!uri.AbsoluteUri.Equals(startUrl) && !string.IsNullOrWhiteSpace(newLink))
{
Uri uriResult;
if (Uri.TryCreate(newLink, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))
{
obtainedLinks[i].HrefLink = uriResult.AbsoluteUri;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uriResult);
webRequest.AllowAutoRedirect = false;
webRequest.Timeout = 20000;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
li.Add(new LinkRespCode(obtainedLinks[i], new StatusCode((int)response.StatusCode)));
}
}
}
catch (UriFormatException e)
{
Console.WriteLine("!!!ERROR: " + links[i] + " " + e.Message);
}
catch (WebException e)
{
Console.WriteLine("!!!ERROR: " + links[i] + " " + e.GetType() + " " + e.Message);
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return li;
}