【问题标题】:HttpWebResponse Times out on Google Places API callHttpWebResponse 在 Google Places API 调用上超时
【发布时间】:2012-07-23 22:02:14
【问题描述】:

我正在开发一个应用程序来遍历邮政编码和业务类型列表,然后通过我为这些参数构建的 URL 创建对 Google Places API(文本搜索)的调用。

网址看起来像这样

https://maps.googleapis.com/maps/api/place/textsearch/json?key=MY_API_KEY_HERE&sensor=false&query=57783+dentist

为每个请求调用以下函数:

    private static StreamReader MakeWebRequest(string sURL)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sURL);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader objReader = new StreamReader(responseStream);
        return objReader;
    }

我遇到的问题是,经过 3 到 5 次迭代后,响应对象超时(System.Net.WebException = "The operation has timed out")。我的第一个想法是请求被快速发送,所以我在循环中插入了一个 Sleep(2000) ,但这似乎没有效果。我检查了失败的网址并将它们粘贴到浏览器中,它们确实返回了正确的数据。

据我了解,此 api 调用的唯一限制是我使用未经验证的帐户每天获得 1000 次调用。我错过了什么?

编辑:这是程序块的其余部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using GooglePlacesJSONGenerator.Models;

namespace GooglePlacesJSONGenerator
{
class Program
{
    static void Main(string[] args)
    {
        //set up a list of zips
        List<string> zipcodeList = new List<string>();
        zipcodeList.Add("57754");
        zipcodeList.Add("57783");
        zipcodeList.Add("57785");

        //setup a list of business types
        List<string> businessTypeList = new List<string>();
        businessTypeList.Add("restaurants");
        businessTypeList.Add("dentist");
        businessTypeList.Add("gym");

        //main data set
        GooglePlaceDataSet places = new GooglePlaceDataSet();

        //base url
        string urlBase = "https://maps.googleapis.com/maps/api/place/textsearch/json?key=MY_API_KEY_HERE&sensor=false&query=";
        string nextUrlBase = urlBase + "&pagetoken=";
        Stream objStream;
        //loop on zip codes
        foreach (string zip in zipcodeList)
        {
            Console.WriteLine("looping on zip " + zip);

            //loop on business type
            foreach (string type in businessTypeList)
            {
                Console.WriteLine("loop on type " + type);

                string sURL;
                string query = HttpUtility.UrlEncode(zip + " " + type);
                sURL = urlBase + query;
                Console.WriteLine("Query String: " + query);
                while (sURL != "")
                {

                    Console.WriteLine("Our URL:  " + sURL);
                    Console.WriteLine("");

                    StreamReader objReader = MakeWebRequest(sURL);

                    JsonTextReader reader = new JsonTextReader(objReader);

                    JsonSerializer se = new JsonSerializer();
                    string parsedData = se.Deserialize(reader).ToString();
                    GooglePlaceDataSet gSet = JsonConvert.DeserializeObject<GooglePlaceDataSet>(parsedData);

                    foreach (GooglePlaceData place in gSet.results)
                    {
                        places.results.Add(place);
                    }

                    if (gSet.next_page_token != null)
                        sURL = nextUrlBase + gSet.next_page_token;
                    else
                    sURL = "";

                    System.Threading.Thread.Sleep(2000);
                }
            }
        }
        Console.ReadLine();
    }

【问题讨论】:

  • 你能把代码贴在你打电话给MakeWebRequest的地方吗?
  • 这将是我的荣幸。
  • 尝试取出while(sURL != "") 并替换为if(!string.IsNullOrEmpty(sURL))。为什么要使用 while 循环?
  • 它是对结果集进行分页的逻辑的一部分...... api 调用一次只返回 20 个......它包括一个 next_page_token 来指示存在可以获取的其他结果.
  • 尝试添加建议的代码无济于事。

标签: c# google-maps


【解决方案1】:

答案是“不要忘记显而易见的事情”。 StreamReader 对象需要关闭。一旦我关闭它,上面的代码就起作用了。

【讨论】:

    【解决方案2】:

    这可能是超出服务提供商允许的来自同一源地址的最大同时连接数阈值的结果。您必须通过 using 或 try/finally 正确 Dispose() 您的资源,否则连接将保持打开状态,直到垃圾收集器处理资源。相关内容见Web response in C# .NET doesn't work more than a couple of timesC# https login and download file

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2019-09-06
      相关资源
      最近更新 更多