并行运行Web客户端时偶尔出现超时异常

时间:2019-07-17 09:32:29

标签: c# parallel-processing tuples webclient latitude-longitude

在包含for循环的数千个并行循环之后,我获得了WebClient超时异常。我已经做过一些研究,大多数人都说要使用HttpWebRequest,但是我一直在寻找并找不到任何显示如何使用它下载字符串的地方。我的猜测是,一次有太多请求,我的互联网无法处理,但是这对我来说没有意义,因为每个字符串只有大约100个字符长,并且根据Google Developer Console,我每条字符串只命中了6个请求第二(由于我的ping高,这比我在普通的for循环中可以做的还要多)。

这是我的代码:

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在Web类中,我正在使用Newtonsoft.Json来解析从client.DownloadString()返回的Json。

任何帮助将不胜感激。

编辑:对于懒惰的人,解决方案是

System.Net.ServicePointManager.DefaultConnectionLimit = valueOfChoice;

0 个答案:

没有答案