2016-03-26 44 views
-3

我想通過給它的http(s)前綴使用getaddrinfo並且它不正確地查找主機。我該如何解決這個問題。我想查找https://www.google.comgai_strerror()返回「沒有這樣的主機被稱爲」,它的工作好了www.google.com上的端口80c getaddrinfo沒有這樣的主機是已知的

這是我使用的的getaddrinfo部分的代碼()部分:

// The GET request I pose to download the webpage 
char *send_buf="GET/\r\n"; 

// i tried three different urls apparently changing the value in the 
// value in the variable in my code, it only works for 'www.google.com' 
const char *URL="https://www.google.com"; 
const char *URL="http://www.google.com"; 
const char *URL="www.google.com"; 

if((status=getaddrinfo(URL, PORT, &hints, &res))!=0) 
{ 
    printf("%s\n", gai_strerror(status)); 
    exit(1); 
} 

我試圖使用Winsock2的下載網頁,當我使用www.google.com,它給了我一個302感動消息,我想下載實際的網頁,而無需使用任何外部庫。

回答

2

您必須提供所需主機的IP地址或主機名。如果你給一個主機名,那麼它將被轉換成它的IP地址(es)。主機名應僅限於此格式: "www.example.domain_name"。不包括"http://""https://"。 這是包含主機名的URL的屬性,它不是主機名本身的一部分。

下載HTML爲https://www.google.com,你要查找IP爲www.google.com,然後連接到端口443(默認的HTTPS端口)是IP,然後建立SSL/TLS加密會話,然後最後發送的HTTP GET請求/文件。

手工實施SSL/TLS加密並不明智。在現有套接字代碼之上使用庫,例如​​OpenSSL或Microsoft自己的CryptoAPI。或者,請不要直接使用套接字API,請使用HTTP/S庫(例如Microsoft自己的WinInet/WinHTTP API)或庫(如libcurl)來處理所有的細節。

+0

那我該如何下載網站的html? 因爲google.com爲我提供了一個302移動響應 – demogorgon

+0

如果您已收到302響應,那麼您可以使用此鏈接[http://www.checkupdown.com/status/E302.html](http://www.checkupdown。 com/status/E302.html)來修復你的錯誤。 – Shiv

+0

這是否有幫助.. – Shiv

相關問題