2015-03-13 78 views
0

我試圖通過代理使用WinINet連接到HTTPS服務。我的代碼如下所示:如何使用WinINet通過代理連接到HTTPS

hInternet = InternetOpenW(L"Test", INTERNET_OPEN_TYPE_PROXY,L"PROXY_IP:PROXY_PORT", L"<local>", 0); 

if (hInternet == NULL) 
{ 
    printf("InternetOpenW failed! Error code: %d\n", GetLastError()); 
} 
else 
{ 
    HINTERNET hConnect = InternetConnectW(hInternet, L"HTTPS_SERVICE_IP", 443, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); 
    if (hConnect == NULL) 
    { 
     printf("InternetConnectW failed! Error code: %d\n", GetLastError()); 
    } 
    else 
    { 
     HINTERNET hRequest = HttpOpenRequestW(hConnect, NULL, L"", NULL, NULL, NULL, 0, 0); 
     if (hRequest == NULL) 
     { 
      printf("HttpOpenRequestW failed! Error code: %d\n", GetLastError()); 
     } 
     else 
     { 
      BOOL bRequestSent = HttpSendRequestW(hRequest, NULL, 0, NULL, 0); 
      if (!bRequestSent) 
      { 
       printf("HttpSendRequestW failed! Error code: %d\n", GetLastError()); 
      } 
      else 
      { 
       printf("Connection ok!\n"); 
      } 

      InternetCloseHandle(hRequest); 
     } 

     InternetCloseHandle(hConnect); 
    } 

    InternetCloseHandle(hInternet); 
} 

它將GET請求發送到代理,執行ntml身份驗證,但連接失敗。代理期望客戶端發送CONNECT請求。如何發送CONNECT方法與該代碼?

回答

0

您需要添加在您的通話HttpOpenRequestWINTERNET_FLAG_SECURE標誌,比如這裏:

HINTERNET hRequest = HttpOpenRequestW(hConnect, NULL, L"", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0); 

顯然,在你身邊的應用程序代碼需要識別是否爲你處理HTTP或HTTPS並添加此只在需要時才標誌。