cURL无法解析主机,但Web浏览器可以访问它

时间:2014-03-13 03:17:14

标签: c facebook facebook-graph-api curl

我在Mac上用C语言编写我的服务器代码,我需要它来访问Facebook上的Facebook" https://graph.facebook.com/app? access_token = [在此处插入访问令牌]"。我收到了CURLE_COULDNT_RESOLVE_HOST错误,可能是因为我之前从未这样做过,而且可能做错了。我正在设置CURL *并连接如下:

curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8");
char* httpURLFormat = "https://graph.facebook.com/app?access_token=%s";
char* httpURL = emalloc(sizeof(char)*(strlen(httpURLFormat)+MAX_TOKEN_LENGTH)); //emalloc is just malloc that makes sure there is free memory
sprintf(httpURL, httpURLFormat, data->password); //data->password is the access token
curl_easy_setopt(curl, CURLOPT_URL, &httpURL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
struct url_data response; //this struct contains int size and char* data
response.size = 0;
response.data = emalloc(4096);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
CURLcode curlCode = curl_easy_perform(curl);
if (curlCode!=CURLE_OK){
    printf("curl failed!!!\n"); //this ends up printing
    free(response.data);
    free(httpURL);
    free(httpURLFormat);
    return false;
}
//Yes, I free everything left over afterwards...

我使用我的代码使用的相同用户代理测试了它在我的网络浏览器中连接的网址,并且连接没有任何问题。我尝试添加一个' \ n' URL末尾的字符,我的代码仍然无法正常工作。

1 个答案:

答案 0 :(得分:2)

我发现了问题:httpURLFormat应为“https://graph.facebook.com:443/app?access_token=%s”。您需要在URL中指定端口。 CURL显然不会使用默认的443。

相关问题