用WinInet下载xml受挫的经历 (转)

本文记录了一次使用WinInet下载XML文件时遇到的问题,即GET请求被错误地转换为POST请求,通过分析及使用snifferpro抓包确认问题所在,并给出了修改后的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用WinInet下载xml受挫的经历 (转)[@more@]

这一日,我祭起Win.NET下载优快云的XML文件,遇到了个莫名其妙的问题……

现将问题和解决办法粘贴于此,如果你遇到类似问题大可用类似方法尝试解决。

其中实现GET请求的代码如下:

void CInternet::Get(CString &csResponse,//返回的内容
 const char *szServer,//服务器
  INTERNET_PORT& nPort,//端口
  const char* szobject,//URI
  Dword& dwHttpStatus)//结果
{
 CInternetSession* pSession = NULL;
 CHttpConnection* pConnection = NULL;
 CHttpFile* pHttpFile = NULL;
 try
 { 
 pSession = new CInternetSession(
 NULL,
 1,
 INTERNET_OPEN_TYPE_PRECONFIG);
 pConnection = pSession->GetHttpConnection(szServer,
 nPort,
 NULL,
 NULL);
 
 pHttpFile = pConnection->OpenRequest(
 NULL,//msdn 说 :"If NULL, ‘GET’ is used. "
 szObject,
 szServer,
 1,
 NULL,
 NULL,
 INTERNET_FLAG_EXISTING_CONNECT);
 
 pHttpFile->SendRequest();
 if (pHttpFile)
 { 
 if (pHttpFile->QueryInfoStatuSCOde(dwHttpStatus)!=0)//程序在这出现异常
 { 
 if (dwHttpStatus < 400)
 {
 int nRead = 0;
 LPSTR pBuffer = new char[1024]; 
 do
 {
 nRead = pHttpFile->Read(pBuffer, 1023);
 if (nRead != 0)
 {
 pBuffer[nRead] = 0;
 csResponse += pBuffer;
 }
 } while (nRead != 0);
 
 if(pBuffer)
 {
 delete pBuffer;
 pBuffer = NULL;
 } 
 }
 }
 }  
 }  
 catch (CInternetException* e)
 {
 e->Delete(); 
 }
 catch (...)
 { 
 }  
 if (pHttpFile != NULL)
 {
 pHttpFile->Close();
 delete pHttpFile;
 }  
 if (pConnection != NULL)
 {
 pConnection->Close();
 delete pConnection;
 }
 if (pSession != NULL)
 {
 pSession->Close();
 delete pSession;
 }
}

接下来,我对该函数进行测试的时候遇到非常非常FAINT的问题,程序在QueryInfoStatusCode处出现异常终止。

测试代码如下:

 CInternet internet;

 CString csResponse;
 DWORD dw;
 INTERNET_PORT port = 80;
 internet.Get(csResponse,"www.youkuaiyun.com",port,"/expert/topic/530/530120.xml",dw);

翻遍了MSDN也没找到原因,无奈之即想起了sniffer pro.

遂用其将HTTP包截获,惊现以下报文:

POST /expert/topic/530/530120.xml HTTP/1.1
Referer: www.youkuaiyun.com
If-Modified-Since: Tue, 26 Feb 2002 01:12:00 GMT
If-None-Match: "46f3959762bec11:92f"
User-Agent: csdnPoster
Host: www.youkuaiyun.com
Content-Length: 0
Cache-Control: no-cache
Cookie: aaa48=http%3A%2F%2Fwww%2Ecsdn%2Enet%2F

从包的第一行我们发现,WinInet私自把我们请求的方法变成了POST!!!

CHttpConnection::OpenRequest

CHttpFile* OpenRequest( LPCTSTR pstrVerb, LPCTSTR pstrObjectName, LPCTSTR pstrReferer = NULL, DWORD dwContext = 1, LPCTSTR* pstrAcceptTypes = NULL, LPCTSTR pstrVersion = NULL, DWORD dwFlags = INTERNET_FLAG_EXISTING_CONNECT );

Parameters

pstrVerb

A pointer to a string containing the verb to use in the request. If NULL, "GET" is used.

注意红色的那段解释,那么我在上文代码中使用

pHttpFile = pConnection->OpenRequest(
 NULL,//msdn 说 :"If NULL, ‘GET’ is used. "
 szObject,
 szServer,
 1,
 NULL,
 NULL,
 INTERNET_FLAG_EXISTING_CONNECT);

是完全可行的。但结果却正好相反。

最终呢,我将上面OpenRequest的第一个参数改为"GET",问题解决了。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-992178/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-992178/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值