今天写了个程序,使用代理服务器下载文件,使用CCproxy设置代理,已知CCproxy设置正确,但使用如下代码就不行了,不知道哪里出错,请各位高手帮忙分析分析。
BOOL SetProxyHttp(HINTERNET hInternet, HINTERNET hHttp)
{
BOOL bFunRet = FALSE;
BOOL bRetCode = FALSE; INTERNET_PROXY_INFO proxyinfo;
CString strUserName;
CString strPassword;
CString strProxyServer;
int nProxyPort = 0;
PROXY_TYPE ProxyType;
CString strServer; nProxyPort = 808;
strProxyServer = TEXT("192.168.28.124");
strUserName = TEXT("MyName");
strPassword = TEXT("123456"); strServer.Format(TEXT("%s:%d"), strProxyServer, nProxyPort); proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxyinfo.lpszProxy = strServer;
proxyinfo.lpszProxyBypass = NULL; if (!::InternetSetOption(hInternet, INTERNET_OPTION_PROXY,  (LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO)))
goto Exit0; if (!strUserName.IsEmpty())
{
bRetCode = ::InternetSetOption(hHttp, 
INTERNET_OPTION_PROXY_USERNAME, 
(LPVOID)(LPCWSTR)strUserName, 
strUserName.GetLength());
if (!bRetCode)
goto Exit0;
} if (!strPassword.IsEmpty())
{
bRetCode = ::InternetSetOption(hHttp, 
INTERNET_OPTION_PROXY_PASSWORD, 
(LPVOID)(LPCWSTR)strPassword, 
strPassword.GetLength());
if (!bRetCode)
goto Exit0;
} bFunRet = TRUE;
Exit0:
return bFunRet;
}int SendHttpRequest(LPCWSTR lpszHostName,
LPCWSTR lpszSubAddress,
CAbstractBuffer* pBuffer,
void* pUsedData,
int nServerFileSize)
{
int nRet = -1; if (InternetAttemptConnect(NULL) == ERROR_SUCCESS)
{
HINTERNET hInternet = InternetOpenW(lpszHostName, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_MAKE_PERSISTENT);
if (hInternet)
{
HINTERNET hHttp = InternetConnectW(hInternet, lpszHostName, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, NULL);
if (hHttp)
{
SetProxyHttp(hInternet, hHttp); // 设置代理服务器,函数返回值正确,但链接不到代理服务器

HINTERNET hRequest = HttpOpenRequestW(hHttp, L"GET", lpszSubAddress, L"HTTP/1.1", NULL, NULL, INTERNET_FLAG_DONT_CACHE, NULL);
if (hRequest)
{
if (HttpSendRequestW(hRequest, NULL, 0, NULL, 0))
{
DWORD dwSize = sizeof(int);
HttpQueryInfoW(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &nRet, &dwSize, NULL); WCHAR szText[1024] = {0};
dwSize = sizeof(szText) / sizeof(WCHAR);
HttpQueryInfoW(hRequest, HTTP_QUERY_STATUS_TEXT, szText, &dwSize, NULL); if (nRet == HTTP_STATUS_OK)
{
char * lpBuffer = NULL;
DWORD nTotalLen = pBuffer->GetBuffer(&lpBuffer); DWORD nReadLen = 0;
DWORD ncurDownloaded = 0;
BOOL bResult = InternetReadFile(hRequest, lpBuffer, nTotalLen, &nReadLen);
while (bResult && nReadLen > 0)
{
pBuffer->Write(nReadLen);
nTotalLen = pBuffer->GetBuffer(&lpBuffer);
bResult = InternetReadFile(hRequest, lpBuffer, nTotalLen, &nReadLen);
} nRet = 0;
}
}
InternetCloseHandle(hRequest);
}
InternetCloseHandle(hHttp);
}
InternetCloseHandle(hInternet);
}
} if (nRet == -1)
{
nRet = GetLastError();
} return -nRet;
}

解决方案 »

  1.   

    用CInternetSession试试。CString strUrl = "http://www.abc.com/down/1224325.doc";
    CString strServer, strObject, strHeader;
    unsigned short nPort;
    DWORD dwServiceType;
    if(!AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort))
    return false;CInternetSession sess("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");//Create session
    char buf[50];
    sprintf(buf, "%s:%d\0", "192.168.6.88", 80);
    INTERNET_PROXY_INFO proxyinfo;
    proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
    proxyinfo.lpszProxy = buf;
    proxyinfo.lpszProxyBypass = NULL;
    sess.SetOption(INTERNET_OPTION_PROXY,(LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO));CHttpFile* pFile;
    //////////////////////////////////////////////
    CHttpConnection *pServer = sess.GetHttpConnection(strServer,nPort); 
    if(pServer == NULL)
    return false;
    pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, NULL, NULL, INTERNET_FLAG_EXISTING_CONNECT); 
    if(pFile == NULL)
    return false;pFile -> SendRequest(); DWORD dwStatus;
    DWORD dwBuffLen = sizeof(dwStatus);
    BOOL bSuccess = pFile->QueryInfo(HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);if( bSuccess && dwStatus>=  200 && dwStatus<300) 
    {
    char buffer[1024];
    while(pFile->Read(buffer, 1024))
    SaveData(buffer);//保存下载的数据
    }
    pFile->Close();
    sess.Close();
      

  2.   

    1. HINTERNET hInternet = InternetOpenW(lpszHostName, INTERNET_OPEN_TYPE_DIRECT, 这里指定了直接连接,而不是通过代理。要使用代理参数,用标记INTERNET_OPEN_TYPE_PROXY 
    要使用IE默认的代理设置,用标记 INTERNET_OPEN_TYPE_PRECONFIG 
    2. 如果要SetProxy,要在调用internetopen之前设置。这种方式设置的是IE的默认代理设置,所以要使用他,需要用标记 INTERNET_OPEN_TYPE_PRECONFIG 
      

  3.   

    3 把
    InternetOpenW =》InternetConnectW =》SetProxyHttp
    换成
    InternetOpenW  =》SetProxyHttp=》InternetConnectW
    或者
    SetProxyHttp =》InternetOpenW =》InternetConnectW