2012-07-05 59 views
1

我想要做的代碼連接代理需要密碼和用戶名認證(ip:端口:用戶名:pw)的C++程序我有http工作,但是當我使用https我總是得到一個407錯誤。無法發送代理憑據(407)

如何以正確的方式在https上發送代理憑據? (C++)

回答

3

那麼這很好,因爲狀態407意味着代理需要認證。

所以,你可以使用這個:

case 407: 
      // The proxy requires authentication. 
      printf("The proxy requires authentication. Sending credentials...\n"); 

      // Obtain the supported and preferred schemes. 
      bResults = WinHttpQueryAuthSchemes(hRequest, 
       &dwSupportedSchemes, 
       &dwFirstScheme, 
       &dwTarget); 

      // Set the credentials before resending the request. 
      if(bResults) 
       dwProxyAuthScheme = ChooseAuthScheme(dwSupportedSchemes); 

      // If the same credentials are requested twice, abort the 
      // request. For simplicity, this sample does not check 
      // for a repeated sequence of status codes. 
      if(dwLastStatus == 407) 
       bDone = TRUE; 
      break; 

功能

DWORD ChooseAuthScheme(DWORD dwSupportedSchemes) 
    { 
// It is the server's responsibility only to accept 
// authentication schemes that provide a sufficient 
// level of security to protect the servers resources. 
// 
// The client is also obligated only to use an authentication 
// scheme that adequately protects its username and password. 
// 
// Thus, this sample code does not use Basic authentication 
// becaus Basic authentication exposes the client's username 
// and password to anyone monitoring the connection. 

if(dwSupportedSchemes & WINHTTP_AUTH_SCHEME_NEGOTIATE) 
    return WINHTTP_AUTH_SCHEME_NEGOTIATE; 
else if(dwSupportedSchemes & WINHTTP_AUTH_SCHEME_NTLM) 
    return WINHTTP_AUTH_SCHEME_NTLM; 
else if(dwSupportedSchemes & WINHTTP_AUTH_SCHEME_PASSPORT) 
    return WINHTTP_AUTH_SCHEME_PASSPORT; 
else if(dwSupportedSchemes & WINHTTP_AUTH_SCHEME_DIGEST) 
    return WINHTTP_AUTH_SCHEME_DIGEST; 
else 
    return 0; 
    } 

這就決定了認證方案.....之後,你使用

bResults = WinHttpSetCredentials(hRequest, 
     WINHTTP_AUTH_TARGET_SERVER, 
     dwProxyAuthScheme, 
     username, 
     password, 
     NULL); 

希望這有助於...我也與這些連接到Azure市場的微軟翻譯,因爲它移動到那裏和f rom八月所有的舊翻譯者都不會得到請求。對我來說,它是通過標題發送驗證密鑰。但我想你有一個用戶名和密碼。

+0

非常感謝您的幫助! –