0

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:

  1. https://www.google.com/
  2. https://www.google.com/
  3. https://www.google.de/
  4. https://www.google.ded/ (=> throws exception as intended)
  5. http://www.google.de/
  6. http://www.google.de/
  7. http://www.google.de/
  8. http://www.google.de/
  9. 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;
    }
Community
  • 1
  • 1
JieBaef
  • 122
  • 1
  • 11
  • Does setting `ServicePointManager.DefaultConnectionLimit` to 50 fix it? – mjwills Jul 15 '19 at 10:17
  • `ServicePointManager.DefaultConnectionLimit` worked like a charm! Thanks! It would be awesome if you could add that as an answer so i can close this question. – JieBaef Jul 15 '19 at 11:31
  • http://blogs.msdn.com/b/jpsanders/archive/2009/05/20/understanding-maxservicepointidletime-and-defaultconnectionlimit.aspx may be worth a read. In practice, the limit specifies how many concurrent requests can be made to a specific domain. – mjwills Jul 15 '19 at 12:42
  • so if the requests are limited to a domain I have to include it even if there are parameters in the link since its target will always be the same server just aiming at different files based on the parameters – JieBaef Jul 15 '19 at 12:44
  • Correct. That is why bumping the number up solved your issue. – mjwills Jul 15 '19 at 12:45

0 Answers0