2013-07-13 210 views
-1

我試圖建立一個http連接到我自己的域並返回一個字符串。我有它與www.microsoft.com工作,但當我更改域www.atomic-gaming.info(我的域名)我得到一個錯誤12029,如果我改變鏈接到www.yahoo.com我得到一個錯誤12017WinHTTP錯誤12029無法連接

代碼

DWORD dwSize = 0; 
DWORD dwDownloaded = 0; 
LPSTR pszOutBuffer; 
BOOL bResults = FALSE; 
HINTERNET hSession = NULL, 
hConnect = NULL, 
hRequest = NULL; 

// Use WinHttpOpen to obtain a session handle. 
hSession = WinHttpOpen(L"WinHTTP Example/1.0", 
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, 
WINHTTP_NO_PROXY_NAME, 
WINHTTP_NO_PROXY_BYPASS, 0); 
WinHttpSetOption(hSession, WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS,NULL,NULL); 
// Specify an HTTP server. 
if(hSession) 
hConnect = WinHttpConnect(hSession, L"www.microsoft.com", 
INTERNET_DEFAULT_HTTPS_PORT, 0); 

// Create an HTTP request handle. 
if(hConnect) 
hRequest = WinHttpOpenRequest(hConnect, L"GET", NULL, 
NULL, WINHTTP_NO_REFERER, 
WINHTTP_DEFAULT_ACCEPT_TYPES, 
WINHTTP_FLAG_SECURE); 

// Send a request. 
if(hRequest) 
bResults = WinHttpSendRequest(hRequest, 
WINHTTP_NO_ADDITIONAL_HEADERS, 0, 
WINHTTP_NO_REQUEST_DATA, 0, 
0, 0); 


// End the request. 
if(bResults) 
bResults = WinHttpReceiveResponse(hRequest, NULL); 

// Keep checking for data until there is nothing left. 
if(bResults) 
{ 
do 
{ 
// Check for available data. 
dwSize = 0; 
if(!WinHttpQueryDataAvailable(hRequest, &dwSize)) 
printf("Error %u in WinHttpQueryDataAvailable.\n", 
GetLastError()); 

// Allocate space for the buffer. 
pszOutBuffer = new char[dwSize+1]; 
if(!pszOutBuffer) 
{ 
printf("Out of memory\n"); 
dwSize=0; 
} 
else 
{ 
// Read the data. 
ZeroMemory(pszOutBuffer, dwSize+1); 

if(!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, 
dwSize, &dwDownloaded)) 
printf("Error %u in WinHttpReadData.\n", GetLastError()); 
else 
printf("%s", pszOutBuffer); 

// Free the memory allocated to the buffer. 
delete [] pszOutBuffer; 
} 
} while(dwSize > 0); 
} 


// Report any errors. 
if(!bResults) 
printf("Error %d has occurred.\n", GetLastError()); 

// Close any open handles. 
if(hRequest) WinHttpCloseHandle(hRequest); 
if(hConnect) WinHttpCloseHandle(hConnect); 
if(hSession) WinHttpCloseHandle(hSession); 

能有人給我一個想法,爲什麼這個只有microsoft.com的作品?我將如何去修復它?

+1

將那些什麼錯誤代碼的意思可能是更有幫助 – Twifty

+0

@Waldermort對於它的價值,12029是ERROR_WINHTTP_CANNOT_CONNECT和12017是ERROR_WINHTTP_OPERATION_CANCELLED –

+1

@IgorTandetnik我希望能鼓勵海報第一次使用谷歌標識錯誤,也許讓他了解他的問題可能是什麼。另一方面,我真的希望MS使用枚舉而不是錯誤代碼,使它們更易於閱讀。 – Twifty

回答

0

您正在通過HTTPS進行連接。 www.atomic-gaming.info沒有提供有效的SSL證書,所以WinHTTP由於安全原因拒絕連接(這可以被覆蓋)。

www.yahoo.com重定向到HTTP。它不適用於您,因爲您錯誤地撥打WinHttpSetOption。讓它

DWORD opt = WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS; 
WinHttpSetOption(hSession, WINHTTP_OPTION_REDIRECT_POLICY, &opt, sizeof(opt)); 
+0

我得到錯誤12029與一些網站和12175與http://atomic-gaming.info/...現在有什麼問題? –

+0

以及我將如何覆蓋ssl證書驗證。 –

+0

12175將是ERROR_WINHTTP_SECURE_FAILURE。 [查看它](http://msdn.microsoft.com/en-us/library/windows/desktop/aa383770.aspx)通過使用WinHttpSetOption將WINHTTP_OPTION_SECURITY_FLAGS選項設置爲[所需的標誌組合],可禁用證書檢查, (http://msdn.microsoft.com/en-us/library/windows/desktop/aa384066.aspx) –