用WinInet下载xml受挫的经历

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

这一日,我祭起WinInet类下载CSDN的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.csdn.net",port,"/expert/topic/530/530120.xml",dw);

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

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

POST /expert/topic/530/530120.xml HTTP/1.1

Referer: www.csdn.net

If-Modified-Since: Tue, 26 Feb 2002 01:12:00 GMT

If-None-Match: "46f3959762bec11:92f"

User-Agent: csdnPoster

Host: www.csdn.net

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",问题解决了。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航