2014-03-13 78 views
0

我使用C編寫我的服務器代碼在我的Mac,我需要它的URL「HTTPS訪問Facebook的圖形API://graph.facebook。 com/app?access_token = [在此處插入訪問令牌]「。我得到一個CURLE_COULDNT_RESOLVE_HOST錯誤,也許是因爲我從來沒有這樣做過,可能做了一些完全錯誤的事情。我設立的捲曲*和連接這樣的:捲曲無法解析主機,但是Web瀏覽器可以訪問它

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... 

我測試了它的使用我的代碼使用相同的用戶代理連接到我的網頁瀏覽器的URL,並將其連接無故障。我嘗試在URL的末尾添加'\ n'字符,而我的代碼仍然無法工作。

+0

你可以嘗試從https://stackoverflow.com/questions/1341644/curl-and-https-cannot-resolve-host的答案?可能是ssl,dns或ipv6問題。另外,您是否可以嘗試其他API,例如https://gdata.youtube.com/feeds/api/users/google/uploads?v=2&alt=json –

+0

另外,你可以嘗試簡單的請求?例如http://graph.facebook.com/4 –

+0

簡單的請求沒有奏效,但我意識到了爲什麼。感謝您的鏈接,但DNS不是問題。我意識到我必須在URL中指定HTTPS端口,而不管https://開頭。 – sudo

回答

相關問題