2011-05-20 68 views
2

我測試從http://msdn.microsoft.com/en-us/library/aa384270%28v=vs.85%29.aspx當主機名不包含www時,WinHttp不起作用。 (錯誤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); 

    // 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); 

這工作正常此WinHTTP的例子,但如果我拿出了www。從www.microsoft.com我收到12029 ERROR_WINHTTP_CANNOT_CONNECT錯誤,這是什麼原因?

如果使用www,所有的網站都可以使用winhttp嗎?因爲有些網站不使用www,我現在找不到任何權利,但是當訪問www時,我看到一些在瀏覽器上顯示「Server not found」的網站。但他們沒有工作。

回答

2

microsoft.com返回301 Moved Permanently頭,讓WinHTTP的榮譽是和重定向到Location:頭中的URL(萬維網),你需要使用WinHttpSetOption設置相應的WINHTTP_OPTION_REDIRECT_*選項(S)。

相關問題