仅在调试时工作,但不在Run中工作

时间:2016-07-26 10:04:53

标签: c# google-places-api

我正在尝试运行此代码以从google api获取地点但是当我尝试使用pagetoken它没有工作并且给我无效请求时,它工作正常一次。但是,如果我在调试中运行它,那么有时它可以正常工作而有些则不行。你能帮忙吗?

private string request_google(string link)
    {
        try
        {
           // link = HttpUtility.ParseQueryString(link).ToString();
            HttpWebRequest webRequest = WebRequest.Create(link) as HttpWebRequest;
            webRequest.Timeout = 20000;
            webRequest.Method = "GET";

            var response = (HttpWebResponse)webRequest.GetResponse();
            using (var stream = response.GetResponseStream())
            {
                var r = new StreamReader(stream);
                string responce = r.ReadToEnd();
                file_number = file_number + 1;
                File.WriteAllText("E:\\google maps\\pharmcy\\places_" + string.Format("text-{0:yyyy-MM-dd_hh-mm-ss-tt-fff}",
                  DateTime.Now) + "_file" + file_number + " _area_" + area_number + ".txt", responce);


                dynamic places_reponce = JsonConvert.DeserializeObject(responce);
                if (places_reponce.status == "INVALID_REQUEST")
                {
                    File.WriteAllText("E:\\google maps\\pharmcy\\places_" + string.Format("text-{0:yyyy-MM-dd_hh-mm-ss-tt-fff}",
             DateTime.Now) + "_file" + file_number + " _area_" + area_number + "invalid_reques" + ".txt", "The link :" + link);
                }



                return places_reponce.next_page_token;


            }
        }
        catch (Exception e)
        {

            File.WriteAllText("E:\\google maps\\pharmcy\\places_" + string.Format("text-{0:yyyy-MM-dd_hh-mm-ss-tt-fff}",
                 DateTime.Now) + "_file" + file_number + " _area_" + area_number +"error"+ ".txt", "The link :"+link+"\n The Error"+e.ToString());
            return null;

        }


    }

1 个答案:

答案 0 :(得分:-1)

请求之间休息2秒将解决问题