Google地理编码API - 403单元测试中的禁止错误,但不在浏览器

时间:2015-07-07 19:10:46

标签: c# google-maps

以下C#代码在DownloadString期间保持返回403:Forbidden。我正在笔记本电脑上进行单元测试中运行代码。当我将网址粘贴到浏览器(Chrome和IE)时,它可以正常运行。此外,当我删除clientID,传感器和签名时,它工作正常。

我尝试过添加标题,SecurityProtocol等许多变种无济于事。

网址如下:

https://maps.googleapis.com/maps/api/geocode/xml?address=440+Sauve+Cres&components=locality:Waterloo|administrative_area:ON&client=##clientID##&sensor=false&signature=##signature##

其中## clientID ##和## signature ##是公司内部价值。

    protected virtual string SendRequest(string url)
    {
        Console.WriteLine(url);
        string response = null;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        using (WebClient client = new WebClient())
        {
            //client.Headers.Add("User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
            client.Headers.Add("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
            client.Headers.Add("Host", "maps.googleapis.com");
            client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0");
            client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8");
            client.Headers.Add("Accept-Language", "en-US,en;q=0.5");
            client.Headers.Add("Accept-Encoding", "gzip, deflate");
            response = client.DownloadString(url);
            Console.WriteLine(">>>" + response);
        }

        return response;
    }

使用有效的httpfox,我看到以下内容:

请求标头

  • (请求行)GET / maps / api / geocode / xml?address = 440 + Sauve + Cres& components = locality:Waterloo | administrative_area:ON& client = ## clientID ##& sensor = false& signature = ## signature ## = HTTP / 1.1
  • 主持人maps.googleapis.com
  • User-Agent Mozilla / 5.0(Windows NT 6.3; WOW64; rv:38.0)Gecko / 20100101 Firefox / 38.0
  • 接受text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8
  • Accept-Language en-US,en; q = 0.5
  • 接受编码gzip,deflate
  • 连接保持活着

响应标头

  • (状态 - 行)HTTP / 2.0 200确定
  • access-control-allow-origin *
  • Alternate-Protocol 443:quic,p = 1
  • Cache-Control public,max-age = 86400
  • 内容编码gzip
  • Content-Length 510
  • Content-Type application / xml;字符集= UTF-8
  • Date Tue,07 Jul 2015 19:55:41 GMT
  • 截止日期:2015年7月8日星期三19:55:41 GMT
  • Server mafe
  • Vary Accept-Language
  • X-Frame-Options SAMEORIGIN
  • X-XSS-Protection 1;模式=块
  • X-Firefox-Spdy h2

没有饼干。

在httpfox的内容标签中: 加载内容时出错(NS_ERROR_DOCUMENT_NOT_CACHED)

0 个答案:

没有答案