HTTP实现文件下栽的API函数有哪些,谢谢?

jing 2000-02-24 07:06:00
加精
...全文
204 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
glb 2001-02-24
  • 打赏
  • 举报
回复
老弟,我的E文不太好呀,看的不太明白,不过程序还是可以的!:)
DOU 2000-02-24
  • 打赏
  • 举报
回复
Accessing the HTTP Protocol

Use the HTTP functions provided by WinInet to use the HTTP protocol to access resources on the Internet. The following illustration shows the relationships of the WinInet functions used to access the HTTP protocol. Shaded boxes represent functions that return HINTERNET handles, while the plain boxes represent functions that use the HINTERNET handle created by the function on which they depend.



HttpAddRequestHeaders, HttpQueryInfo, and HttpSendRequest, are dependent on the HINTERNET handle created by HttpOpenRequest.

The following illustration shows the WinInet functions that use the HINTERNET handle created by HttpOpenRequest after it is sent by HttpSendRequest. The shaded boxes represent functions that return HINTERNET handles, while the plain boxes represent functions that use the HINTERNET handle created by the function on which they depend.



After HttpSendRequest has been used on the handle returned by HttpOpenRequest, InternetQueryDataAvailable, and InternetReadFile, can be used on that handle.

To use the HTTP WinInet functions
Call the InternetOpen function to initialize an Internet handle.
InternetOpen creates the root HINTERNET handle used to establish the HTTP session. The HINTERNET Internet handle is used by all subsequent functions.

Call InternetConnect using the HINTERNET returned by InternetOpen to create an HTTP session.
When calling InternetConnect specify INTERNET_DEFAULT_HTTP for the nServerPort parameter and INTERNET_SERVICE_HTTP for the dwService parameter.

InternetConnect uses the handle returned by InternetOpen to create a specific HTTP session. InternetConnect initializes a HTTP session for the specified site using the arguments passed to it and creates an HINTERNET handle that is a branch off the root handle. InternetConnect does not attempt to access or establish a connection to the specified site.

Call HttpOpenRequest to open a HTTP request handle.
The HttpOpenRequest function uses the handle created by InternetConnect to establish a connection to the specified site.

Call HttpSendRequest using the handle created by the HttpOpenRequest to send a HTTP request to the HTTP server.
Call InternetReadFile to download data.
–Or–

Call InternetQueryDataAvailable to query how much data is available to be read by a subsequent call to InternetReadFile.

Call InternetCloseHandle to close the handle created by HttpOpenRequest.
Call InternetCloseHandle to close the HTTP session created by InternetConnect.
Call InternetCloseHandle to close the handle created by InternetOpen.
The following example GetInternetFile function shows how to use the HTTP functions to establish a HTTP session and retrieve a file. Two global Boolean variables g_bproxy, use proxy server and g_bOpenURL, use URL, are options set in the CeHttp application dialog.

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

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

DWORD dwSize = 0,
dwFlags = INTERNET_FLAG_RELOAD and INTERNET_FLAG_NO_CACHE_WRITE;

TCHAR szErrMsg[200];

char *lpBufferA,
*lpHeadersA;

TCHAR *lpBufferW,
*lpHeadersW;

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


// Initializes the use of the Windows CE Internet functions.
if (g_bProxy)
{
hOpen = InternetOpen (TEXT("CeHttp"), INTERNET_OPEN_TYPE_PROXY,
lpszProxyServer, 0, 0);
}
else
{
hOpen = InternetOpen (TEXT("CeHttp"), INTERNET_OPEN_TYPE_PRECONFIG,
NULL, 0, 0);
}

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

if (g_bOpenURL)
{
if (!(hRequest = InternetOpenUrl (hOpen, lpszServer, NULL, 0,
INTERNET_FLAG_RELOAD, 0)))
{
wsprintf (szErrMsg, TEXT("%s: %x"), EXT("InternetOpenUrl Error"),
GetLastError());
goto exit;
}
}
else
{
// Opens an HTTP session for a given site by lpszServer.
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;
}

// Opens 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;
}

// Sends 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 HttpQueryInfo to find out the size of the headers.
HttpQueryInfo (hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize,
NULL);

// Allocates a block of memory for lpHeadersA.
lpHeadersA = new CHAR [dwSize];

// Call HttpQueryInfo again to get the headers.
if (!HttpQueryInfo (
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 get ready
// for putting the new data in it.
SendMessage (g_hwndEdit, EM_SETSEL, 0, -1);
SendMessage (g_hwndEdit, WM_CLEAR, 0, 0);
SendMessage (g_hwndEdit, WM_PAINT, TRUE, 0);
}

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

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

// Allocates a block of memory for lpHeadersW.
lpHeadersW = new TCHAR [dwSize];

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

// Put the headers in the edit control.
SendMessage (g_hwndMain, WM_PUTTEXT, NULL, (LPARAM) lpHeadersW);

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

// Allocates a block of memory for lpHeadersW.
lpBufferA = new CHAR [32000];

do
{
if (!InternetReadFile (hRequest, (LPVOID)lpBufferA, 32000, &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 receives the UNICODE
// string.
dwSize = MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, NULL, 0);

// Allocates a block of memory for lpBufferW.
lpBufferW = new TCHAR [dwSize];

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

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

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

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

bReturn = TRUE;

exit:

// Closes 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;
}


16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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