用wininet抓取asp网页有问题,高手帮忙看看,先谢谢了 (100分求)

cheerry 2009-05-22 10:34:55
用wininet抓取asp网页有问题,高手帮忙看看,先谢谢了

函数如下,调用的时候参数是:TEXT("http://search.callerlocation.com/phonenumber.asp?keyword=256&keywordname=400&keywordend=0000")

如果我直接读取新浪的网页可以读取成功,但asp网页带查询的就不可以了。

BOOL GetInternetFile (LPTSTR lpszServer)
{
BOOL bReturn = FALSE;

HINTERNET hOpen = NULL,
hConnect = NULL,
hRequest = NULL;

DWORD dwSize = 0,
dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;

LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL};

char *lpBufferA,
*lpHeadersA;

TCHAR *lpBufferW,
*lpHeadersW;

TCHAR szErrMsg[200];

//1:Call the InternetOpen function to initialize an Internet handle.


hOpen = InternetOpen(TEXT("CeHttp"), INTERNET_OPEN_TYPE_PRECONFIG,
NULL, 0, 0);

if (!hOpen)
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetOpen Error"), GetLastError());
return bReturn;
}

//2:Call InternetConnect using the HINTERNET returned by InternetOpen to create an HTTP session.
if (!(hConnect = InternetConnect (hOpen,
lpszServer,
INTERNET_INVALID_PORT_NUMBER,
NULL, NULL,
INTERNET_SERVICE_HTTP,
0, 0)))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetConnect Error"),
GetLastError());
goto exit;
}

//3: Open an HTTP request handle. Call HttpOpenRequest to open an HTTP request handle.

if (!(hRequest = HttpOpenRequest (hConnect,
TEXT("GET"),
NULL,
HTTP_VERSION,
NULL,
(LPCTSTR*)AcceptTypes,
dwFlags, 0)))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpOpenRequest Error"),
GetLastError());
goto exit;
}

//4: Send a request to the HTTP server. Call HttpSendRequest using the handle created by the HttpOpenRequest to send an HTTP request to the HTTP server.
TCHAR swRequest[] = TEXT("keyword=256&keywordname=400&keywordend=0000");

if (!HttpSendRequest (hRequest, NULL,0, NULL, 0))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpSendRequest Error"),
GetLastError());
goto exit;
}

//5:Call InternetReadFile to download data.

HttpQueryInfoA(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize,
NULL);

// Allocate a block of memory for lpHeadersA.
lpHeadersA = new CHAR [dwSize];
//if memory allocation fails, then free the block of memory.

if(!lpHeadersA)
{
delete[] lpHeadersA;
goto exit;
}
// Call HttpQueryInfoA again to get the headers.
if (!HttpQueryInfoA(hRequest,
HTTP_QUERY_RAW_HEADERS_CRLF,
(LPVOID) lpHeadersA, &dwSize, NULL))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpQueryInfo"),
GetLastError());
goto exit;
}



// Terminate headers with NULL.
lpHeadersA [dwSize] = '\0';

// Get the required size of the buffer that receives the Unicode
// string.
dwSize = MultiByteToWideChar (CP_ACP, 0, lpHeadersA, -1, NULL, 0);

// Allocate a block of memory for lpHeadersW.
lpHeadersW = new TCHAR [dwSize];
//if memory allocation fails, then free the block of memory.
if(!lpHeadersW)
{
delete[] lpHeadersW;
goto exit;
}

// Convert headers from ASCII to Unicode.
MultiByteToWideChar (CP_ACP, 0, lpHeadersA, -1, lpHeadersW, dwSize);

// Free the blocks of memory.
delete[] lpHeadersA;
delete[] lpHeadersW;

// Allocate a block of memory for lpHeadersW.
lpBufferA = new CHAR [32000];
//if memory allocation fails, then free the block of memory.
if(!lpBufferA)
{
delete[] lpBufferA;
goto exit;
}

do
{
if (!InternetReadFile(hRequest, (LPVOID)lpBufferA, 31999, &dwSize))
{
wsprintf(szErrMsg, TEXT("%s: %x"), TEXT("InternetReadFile Error"),
GetLastError());
goto exit;
}

if (dwSize != 0)
{
// Terminate headers with NULL.
lpBufferA [dwSize] = '\0';

// Get the required size of the buffer which receives the Unicode
// string.
dwSize = MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, NULL, 0);

// Allocate a block of memory for lpBufferW.
lpBufferW = new TCHAR [dwSize];
//if memory allocation fails, then free the block of memory.
if(!lpBufferW)
{
delete[] lpBufferW;
goto exit;
}

// Convert the buffer from ASCII to Unicode.
MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, lpBufferW, dwSize);

// Free the block of memory.
delete[] lpBufferW;
}
} while (dwSize);

// Free the block of memory.
delete[] lpBufferA;

bReturn = TRUE;


exit:

if(!bReturn)
{
OutputDebugString(szErrMsg);
OutputDebugString(TEXT("\r\n"));
}

// Close the Internet handles.
if (hOpen)
{
if (!InternetCloseHandle (hOpen))
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"),
GetLastError());
}

if (hConnect)
{
if (!InternetCloseHandle (hConnect))
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"),
GetLastError());
}

if (hRequest)
{
if (!InternetCloseHandle (hRequest))
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"),
GetLastError());
}
if(!bReturn)
{
OutputDebugString(szErrMsg);
}

return bReturn;
}
...全文
212 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
cheerry 2009-05-23
  • 打赏
  • 举报
回复
读取的网页不完整,这是怎么回事啊,顶顶。
red_berries 2009-05-23
  • 打赏
  • 举报
回复
我这里是可以全读出来的
red_berries 2009-05-23
  • 打赏
  • 举报
回复
InternetReadFile这个要调用多次
goodname 2009-05-22
  • 打赏
  • 举报
回复
可以用wireshark

http://www.wireshark.org/

你分别抓取一下程序的包和浏览器的包,看看有什么区别。
oyljerry 2009-05-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 cheerry 的回复:]
我没有写过网页交互的程序,第一次写这样的东西,

抓包工具一般都用哪个工具?

我的问题是:发请求过去以后,返回来的数据是 域名纠错 的内容,但在IE里面直接读取得到得就是查询的电话号码归属地。

谢谢goodname!
[/Quote]
先看你的发送的域名数据等是否正确,可能你的http包等发送过去并不一定正确。。。

sniffer,ethereal等可以抓包
cheerry 2009-05-22
  • 打赏
  • 举报
回复
我没有写过网页交互的程序,第一次写这样的东西,

抓包工具一般都用哪个工具?

我的问题是:发请求过去以后,返回来的数据是 域名纠错 的内容,但在IE里面直接读取得到得就是查询的电话号码归属地。

谢谢goodname!
goodname 2009-05-22
  • 打赏
  • 举报
回复
程序没仔细看,只是说说这种方式。

表面上看似乎表单只是这么几个数据,实际上是可以隐式给服务器传参数的,大概是。

有两个尝试的办法

一,用抓包工具看看当你在网页提交表单的时候交互的包,分析下。
二,你新开一个浏览器,手动使用你给出的url,看看是否有内容。

另外,你说的有问题,究竟指的什么问题,最好描述详细些。
cheerry 2009-05-22
  • 打赏
  • 举报
回复
在线等待。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
cheerry 2009-05-22
  • 打赏
  • 举报
回复
谢谢!~
liliangbao 2009-05-22
  • 打赏
  • 举报
回复
哦,帮顶~
cheerry 2009-05-22
  • 打赏
  • 举报
回复
自己顶顶,别沉了。
cheerry 2009-05-22
  • 打赏
  • 举报
回复
red_berries ,谢谢你,现在已经可以读取了,主要是只能读取一部分信息,需要的信息在后面呢,没有读出来。读取了12552个字节,而读取22892个字节应该是正确的,我看了一下,我的BUFFER的长度也是足够的。
red_berries 2009-05-22
  • 打赏
  • 举报
回复
if (!(hRequest = HttpOpenRequest (hConnect,
TEXT("GET"),
NULL,
HTTP_VERSION,
NULL,
(LPCTSTR*)AcceptTypes,
dwFlags, 0)))

第三个参数传/phonenumber.asp?keyword=256&keywordname=400&keywordend=0000

if (!(hConnect = InternetConnect (hOpen,
lpszServer,
INTERNET_INVALID_PORT_NUMBER,
NULL, NULL,
INTERNET_SERVICE_HTTP,
0, 0)))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetConnect Error"),
GetLastError());
goto exit;
}
lpszServer应该是search.callerlocation.com

cheerry 2009-05-22
  • 打赏
  • 举报
回复
现在能读出数据了,可是只能读出一部分,需要的地方还没有读到BUFFER里面,应该怎么办啊?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧