读书人

CInternetSession HTTP代理服务器,该如

发布时间: 2013-01-05 15:20:40 作者: rapoo

CInternetSession HTTP代理服务器
如题。
我写了一个通过HTTP代理下载一个小文件的DEMO,然后自己用CCProxy搭了一个简单的环境,这个DEMO可以测试通过。但有两个很奇怪的问题:
1、在开启代理服务器的时候,代理服务器的地址不填写指定的,乱填,它也可以通过代理服务下载东西。当然,如果我关掉CCProxy,它就不能下载那个小文件了。
2、在ISA环境中,且是域中的电脑上,却不能下载那个小文件。

请高手指点!!!

附:如果是从google或baidu上搜过来的代码,若允确信把握可以解决我的问题,就不要贴了,直接写“加分”就好了。因为我搜了三天了——我是新手。

再次恳请高手指点我!!

代码如下:

       UpdateData(TRUE);

CString szProxyAddress;
CString szuser;
CString szpass;
int nProxyPort;

if ( (ProAdd != "") && (ProPort != "") )
{
szProxyAddress=ProAdd;//代理服务器地址
nProxyPort=atoi(ProPort);//端口
}
else
{
MessageBox("请填写代理服务器地址和端口!","注意!");
return ;
}

szuser = sUseName;
szpass = sPassWord;

CString str_url=_T("http://download.qidian.com/pda/1187465.txt");
CString str_savepath=_T("C:\\Test.xml");

DWORD dwServiceType;
CString strServerName;
CString strFilePath;

INTERNET_PORT nPort;
CInternetSession comm;
CHttpConnection* pServer = NULL;
CHttpFile* pHttpFile = NULL;

INTERNET_PROXY_INFO Proxy;

AfxParseURL(str_url, dwServiceType, strServerName, strFilePath, nPort);

Proxy.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
CString strProxyServer;
strProxyServer.Format(_T("%s:%d"), szProxyAddress, nProxyPort);
Proxy.lpszProxy = strProxyServer;
Proxy.lpszProxyBypass = NULL;

pServer = comm.GetHttpConnection(strServerName, nPort);

comm.SetOption (INTERNET_OPTION_CONNECT_TIMEOUT, 30 * 1000) ;

comm.SetOption (INTERNET_OPTION_CONNECT_BACKOFF, 1000) ;

comm.SetOption (INTERNET_OPTION_CONNECT_RETRIES, 3) ;

CFile wrifile;
if (!wrifile.Open(str_savepath,CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary))
{
MessageBox("文件创建失败!","提示!");
comm.Close();
return;
}

try
{

pHttpFile = (CHttpFile*)comm.OpenURL(str_url,1,
INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_RELOAD);

CString str ;
pHttpFile->QueryInfo (HTTP_QUERY_STATUS_CODE, str) ;
if (str == _T("407"))
{
MessageBox("需要用户名和密码验证!!!");

//pHttpFile->SetOption(INTERNET_OPTION_PROXY, (LPVOID)&Proxy, sizeof(INTERNET_PROXY_INFO));
pServer->SetOption(INTERNET_OPEN_TYPE_PROXY, (LPVOID)&Proxy, sizeof(INTERNET_PROXY_INFO));
pHttpFile->SetOption(INTERNET_OPTION_PROXY_USERNAME, (LPVOID)(LPCTSTR)szuser, lstrlen(szuser)+1);//此处没有添加
pHttpFile->SetOption(INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)(LPCTSTR)szpass, lstrlen(szpass)+1);//用户名和密码验证
pHttpFile->SendRequest(NULL);

pHttpFile->QueryInfo (HTTP_QUERY_STATUS_CODE, str) ;
if (str != _T("200"))
{
MessageBox("用户名或密码验证失败!");
pHttpFile->Close() ;
delete pHttpFile ;
pHttpFile = NULL;
return ;
}

}


else
{
pServer->SetOption(INTERNET_OPTION_PROXY, (LPVOID)&Proxy, sizeof(INTERNET_PROXY_INFO));
//pHttpFile->SetOption(INTERNET_OPTION_PROXY, (LPVOID)&Proxy, sizeof(INTERNET_PROXY_INFO));
}

}
catch(CInternetException * m_pException)
{
pHttpFile = NULL;

m_pException->m_dwError;

m_pException->Delete();

comm.Close();

MessageBox("连接失败!","错误!");
return;
}

DWORD uRead=0;
BYTE buffer[2048];
do
{
try
{
memset((void*)buffer,0,2048);
uRead=pHttpFile->Read(buffer,2048);

}
catch (CInternetException* e)
{
MessageBox("读取数据失败!","错误!");
e->Delete();
continue;
}

if(uRead<=0)
{
break;
}
// 如果读到数据就写入文件
wrifile.Write(buffer,uRead);

}
while(true);

wrifile.Flush();
wrifile.Close();// 接收完毕关闭文件

pHttpFile->Close();// 接收完毕关闭连接

delete pHttpFile;
pHttpFile = NULL;

comm.Close();

MessageBox(" 成功! \n请查看C:\\Test.xml!","成功!");


[解决办法]
这个不行吗?我没有代理,所以无法测试,不过读了一下代码,写得是正确的:

另外,你的代理设置错了,也能联网说明你的系统会自动读取IE的设置,你将IE的清除掉再看看。

CInternetSession(
LPCTSTR pstrAgent = NULL,//名字,可以自己定义
DWORD_PTR dwContext = 1,//默认
DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS,//这里就要修改了,呵呵
LPCTSTR pstrProxyName = NULL,//根据type的不同而不同
LPCTSTR pstrProxyBypass = NULL,//代理是否需要口令
DWORD dwFlags = 0 //默认
);

这是另一种方式:也可参考
INTERNET_PROXY_INFO ipi;
ipi.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
ipi.lpszProxy = _T("hogehoge.proxy.com:8080");
ipi.lpszProxyBypass = NULL;
HRESULT hr = ::UrlMkSetSessionOption(INTERNET_OPTION_PROXY, (void *)&ipi, sizeof(ipi), 0);

下面是第一种方式的测试代码
CHttpFile* myhttp=NULL;
CString temp,total;
CInternetSession *mysession=new CInternetSession("wangke",1,INTERNET_OPEN_TYPE_PROXY,_T("61.129.102.208:8080"),NULL,0);
try{
myhttp=(CHttpFile*)mysession->OpenURL("http://www.020114.org/timeacaa.asp");
while(myhttp->ReadString(temp))
total+=temp;
myhttp->Close();
mysession->Close();
delete myhttp;
delete mysession;
GetDlgItem(IDC_EDIT1)->SetWindowText(total);
total.Empty();
}
catch(CInternetException &e)
{
char errs[MAX_PATH]={0};
e.GetErrorMessage(errs,MAX_PATH,NULL);
AfxMessageBox(errs);
}
[解决办法]
http://topic.csdn.net/t/20031025/23/2395299.html

读书人网 >VC/MFC

热点推荐