0

After a few thousand parallel loops that contain for loops that contain for loops I get the WebClient timeout exception. I've done some research and most people say to use HttpWebRequest but I've looked and can't find anywhere that shows how to downloadstring using it. My guess is that too many requests are happening at once and my internet can't handle it but that doesn't make sense to me as each string is only about 100 characters long and according to the google developer console I only hit 6 requests per second (which is more than I could do in a normal for loop because of my high ping).

Here's my code:

public static (double Lat, double Lon) GetExact((double Lat, double Lon) point, double searchRadius)
    {
        dynamic data;
        using (WebClient client = new WebClient())
            data = JsonConvert.DeserializeObject(client.DownloadString(Sign("https://maps.googleapis.com/maps/api/streetview/metadata?location=" + point.Lat + "," + point.Lon + "&key=" + apiKey + "&radius=" + searchRadius, signingKey)));
        if (data.status == "OK")
            return (Convert.ToDouble(data.location.lat), Convert.ToDouble(data.location.lng));
        return (0, 0);
    }
public static (double Lat, double Lon)[] Interpolate((double Lat, double Lon)[] locData, double desiredMperPoint, double searchRadius)
    {
        locData = Get.ExactCoords(locData, searchRadius);

        List<(double Lat, double Lon)>[] pointlistarray = new List<(double Lat, double Lon)>[locData.Length];

        Parallel.For(0, pointlistarray.Length - 1, a =>
        {
            pointlistarray[a] = new List<(double Lat, double Lon)>() {locData[a], locData[a + 1]};
            for (int b = 0; b < InterpolationCalc(Calculate.Distance(locData[a], locData[a + 1]), desiredMperPoint); b++)
                pointlistarray[a] = InterpolateList(pointlistarray[a], searchRadius);
        });

        List<(double Lat, double Lon)> sortedList = new List<(double Lat, double Lon)>();
        foreach (List<(double Lat, double Lon)> list in pointlistarray)
        {
            if (list == null)
                continue;
            foreach ((double Lat, double Lon) point in list)
                sortedList.Add(point);
        }

        return Remove.Dupes(sortedList.ToArray());
    }
private static List<(double Lat, double Lon)> InterpolateList(List<(double Lat, double Lon)> list, double searchRadius)
    {
        List<(double Lat, double Lon)> interpolatedList = new List<(double Lat, double Lon)>();
        for (int i = 0; i < list.Count - 1; i++)
        {
            interpolatedList.Add(list[i]);
            interpolatedList.Add(Web.GetExact(Calculate.Midpoint(list[i], list[i + 1]), searchRadius));
        }
        interpolatedList.Add(list[list.Count - 1]);
        return interpolatedList;
    }

GetExact is within the Web class and I'm using Newtonsoft.Json to parse the Json that comes back from client.DownloadString().

Any help would be appreciated.

Edit: For the lazy the solution is

System.Net.ServicePointManager.DefaultConnectionLimit = valueOfChoice;
TrueCP5
  • 358
  • 3
  • 14
  • What is `ServicePointManager.DefaultConnectionLimit` set to? https://stackoverflow.com/questions/48785681/the-operation-has-timed-out-at-system-net-httpwebrequest-getresponse-while-sen – mjwills Jul 17 '19 at 09:35
  • @mjwills Yes, otherwise locData[a+ 1] would throw an exception. – TrueCP5 Jul 17 '19 at 09:42
  • @mjwills apparently `ServicePointManager.DefaultConnectionLimit` is set to 2 at the start of my program. – TrueCP5 Jul 17 '19 at 09:44
  • Yes it does and my requests per second has jumped to 150! – TrueCP5 Jul 17 '19 at 10:03

0 Answers0