使用来自中国的谷歌地理编码api

using google-geocoding-api from China

本文关键字:编码 api 谷歌 中国      更新时间:2023-10-16

我是一名来自中国的新手程序员。我在使用google-geocoding-api时遇到了一些问题,当我用以下代码调试时是正确的,但是当我使用生成的.exe文件在发布模式下编译时没有响应。因为我在中国,所以我只能用网络代理访问URL,所以我不知道是网络问题还是我的代码问题。有人能帮我试试下面的代码来检查到底是什么问题吗?

// street_name.cpp : 
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <wininet.h>
using namespace std;
#define MAXBLOCKSIZE 500
#pragma comment (lib, "wininet.lib")
void download(const char*);
int main(int argc, char* argv[]){
    const char *cUrl = NULL;
    char Url[512];
    float lat = 45.798748;
    float lng = 126.531115;
    sprintf(Url,"https://maps.googleapis.com/maps/api/geocode/xml?latlng=45.797749,126.523811&result_type=route&address_component_type=route&key=AIzaSyC6M3Pbbjdrgtl8QZjuJPK-1JdAJD5oEgA",lat,lng);
    cUrl = Url;
    download(cUrl);

    if (argc > 1)
    {
        download((const char*)argv[1]);
    }
    else
    {
        cout << "Usage: auto-Update url";
    }
    return 0;
}
/**
* 
* @param Url: The target action url
*
*/
void download(const char *Url)
{
    HINTERNET hSession = InternetOpenA("RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    string StreetName;  
    if (hSession != NULL)
    {
        HINTERNET handle2 = InternetOpenUrlA(hSession, Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
        if (handle2 != NULL)
        {
            cout << Url << endl;
            byte Temp[MAXBLOCKSIZE] = {0};
            ULONG Number = 1;
            int i = 0;
            ofstream ofs("baidu.txt");
            if (ofs)
            {
                while (Number > 0)
                {
                    InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number);
                    string a = string((const char*)Temp,Number);    
                    ofs << a.c_str();
                    StreetName += a;
                }
                ofs.close();
            }   
            InternetCloseHandle(handle2);
            handle2 = NULL;
        }
        InternetCloseHandle(hSession);
        hSession = NULL;
    }
}

如果您的请求来自中国,可能是HTTPS请求被阻止了。试着HTTP .

我想这可能与https://meta.stackexchange.com/questions/191338/https-problem-when-accessing-stack-overflow-in-china有关