http下载数据的例子 请高手指点啊

romant2005 2008-08-28 11:17:28
这是一个修改微软SDK的例子 在HttpSendRequest函数处老是出错 高手看看吧 谢谢了

int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{

BOOL bReturn = FALSE;
CHAR lpszServer[64] = "http://www.baidu.com/";

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

DWORD dwSize = 0,
dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;

TCHAR szErrMsg[200];

char *lpBufferA,
*lpHeadersA;

TCHAR *lpBufferW,
*lpHeadersW;

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

MessageBox(NULL,temp,NULL,NULL);
// Initialize the use of the Windows CE Internet functions.
if (0)
{
hOpen = InternetOpen(TEXT("CeHttp"), INTERNET_OPEN_TYPE_PROXY,
NULL, 0, 0);
}
else
{
hOpen = InternetOpen(TEXT("CeHttp"), INTERNET_OPEN_TYPE_DIRECT,
NULL, 0, 0);
}

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

if (0)
{
if (!(hRequest = InternetOpenUrl (hOpen, TEXT(" http://www.baidu.com/"), NULL, 0,
INTERNET_FLAG_RELOAD, 0)))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetOpenUrl Error"),
GetLastError());
goto exit;
}
}
else
{
// Open an HTTP session for a specified site by using lpszServer.
if (!(hConnect = InternetConnect (hOpen,
TEXT("www.baidu.com"),
INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL,
INTERNET_SERVICE_HTTP,
0, 0)))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetConnect Error"),
GetLastError());
goto exit;
}

// 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;
}
// Send a request to the HTTP server.
if (!HttpSendRequest(hRequest, NULL, 0, NULL, 0))///////////////////////////////出错处
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpSendRequest Error"),
GetLastError());
goto exit;
}
}

// Call HttpQueryInfoAto find out the size of the headers.
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;
}
else
{
// Clear all of the existing text in the edit control and prepare
// to put the new information in it.
// SendMessage (HWND_BROADCAST, EM_SETSEL, 0, -1);
// SendMessage (HWND_BROADCAST, WM_CLEAR, 0, 0);
// SendMessage (HWND_BROADCAST, WM_PAINT, TRUE, 0);
}

// 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);

// Put the headers in the edit control.
// SendMessage (HWND_BROADCAST, WM_PUTTEXT, NULL, (LPARAM) lpHeadersW);
// SendMessage (HWND_BROADCAST, NULL, NULL, (LPARAM) lpHeadersW);
// 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';
LPCWSTR temp = TEXT("78910");

// 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];
MessageBox(NULL,(LPCWSTR)lpBufferW,NULL,NULL);

//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);
MessageBox(NULL,(LPCWSTR)*lpBufferW,NULL,NULL);///////////////////////////////


// Put the buffer in the edit control.
//SendMessage (HWND_BROADCAST, WM_PUTTEXT, NULL, (LPARAM) lpBufferW);
//SendMessage (HWND_BROADCAST, NULL, NULL, (LPARAM) lpHeadersW);



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

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

bReturn = TRUE;

exit:

// 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());
}

return bReturn;

}
...全文
467 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
mszengg 2012-05-04
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/en-us/library/aa383770.aspx
12007 即这个宏:ERROR_WINHTTP_NAME_NOT_RESOLVED

中文意思是:域名无法解析,如果不是一时可访问,一时不可访问的话,我遇过类似的情形,请考虑购置国内的DNS服务。
小李匪盗 2008-09-03
  • 打赏
  • 举报
回复
你连接上网络了吗?
alonepb 2008-09-02
  • 打赏
  • 举报
回复
顶下
romant2005 2008-09-01
  • 打赏
  • 举报
回复
还是没有解决 郁闷
comiunknown 2008-08-30
  • 打赏
  • 举报
回复
Error 12007: The server name could not be resolved

感觉是要访问的URL写的有问题。
CHAR lpszServer[64] = "http://www.baidu.com/";
试验改成
CHAR lpszServer[64] = "http://www.baidu.com/index.htm";
小李匪盗 2008-08-29
  • 打赏
  • 举报
回复
hRequest = HttpOpenRequest (hConnect,
TEXT("GET"),
NULL,
HTTP_VERSION,
NULL,
(LPCTSTR*)AcceptTypes,
dwFlags, 0)

这个不对吧,第三个参数怎么为NULL,如果是访问www.baidu.com,则此参数应为L"/"。
文斌 2008-08-29
  • 打赏
  • 举报
回复
AfxParseURL(m_myURL,servicetype,server,object,port);
pHttpRequest->m_hConnection=InternetConnect(m_hRoot,server,port,
NULL,NULL,servicetype,0,0);
m_hOpen=HttpOpenRequest(m_hConnection,L"POST",object,L"HTTP/1.1",NULL,(LPCTSTR*)AcceptTypes,INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_DONT_CACHE,0);

关键是要把你的URL经过一次PARSE,将分解的结果写入这两个函数中
romant2005 2008-08-29
  • 打赏
  • 举报
回复
搞错了 错误码是“HttpSendRequest Error failed with error 12007: ”
dhm200806 2008-08-29
  • 打赏
  • 举报
回复
romant2005 2008-08-29
  • 打赏
  • 举报
回复
请高手指点一下吧
yyunffu 2008-08-29
  • 打赏
  • 举报
回复
错了,改成:CHAR lpszServer[64] = "www.baidu.com";
yyunffu 2008-08-29
  • 打赏
  • 举报
回复
CHAR lpszServer[64] = "http://www.baidu.com/";
这一句改为:
CHAR lpszServer[64] = "www.baidu.com/";
romant2005 2008-08-29
  • 打赏
  • 举报
回复
哪位有完整的代码,给发一份吧。必有重谢
romant2005 2008-08-28
  • 打赏
  • 举报
回复
错误码是:"HttpSendRequest Error: 2ee7",谢谢
romant2005 2008-08-28
  • 打赏
  • 举报
回复
编译可以通过,只是得不到结果。每次在调试时到if (!HttpSendRequest(hRequest, NULL, 0, NULL, 0))处就退出了(“线程 0xd6b0f126 已退出,返回值为 0 (0x0)”),还望高手指点啊。
guguqiaqia 2008-08-28
  • 打赏
  • 举报
回复
报告什么错误?贴出来。

7,655

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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