代码改变世界

WinINet function(2)Request

2011-11-27 22:06  Clingingboy  阅读(543)  评论(0编辑  收藏  举报

 

参考此文档:http://wenku.baidu.com/view/4f3558edf8c75fbfc77db229.html

参考:http://wenku.baidu.com/view/b5364b87ec3a87c24028c438.html

InternetOpenUrl的内部

1.InternetConnect

Opens an File Transfer Protocol (FTP), Gopher, or HTTP session for a given site.

2.HttpOpenRequest

Creates an HTTP request handle.

3.HttpSendRequest

Sends the specified request to the HTTP server.

4.HttpQueryInfo

Retrieves header information associated with an HTTP request.

Demo

HINTERNET hInternet = InternetOpen("HttpAuth Sample",             // app name
        INTERNET_OPEN_TYPE_PRECONFIG,  // access type
        NULL,                          // proxy server
        0,                             // proxy port
        0);                            // flags

HINTERNET hConnect = ::InternetConnect(hInternet,
                                    "www.microsoft.com",
                                    INTERNET_INVALID_PORT_NUMBER,
                                    "",
                                    "",
                                    INTERNET_SERVICE_HTTP,
                                    0,
                                    0) ;

HINTERNET hHttpFile = ::HttpOpenRequest(hConnect,
                                        "GET",
                                        "/MSDN/MSDNINFO/",
                                        HTTP_VERSION,
                                        NULL,
                                        0,
                                        INTERNET_FLAG_DONT_CACHE,
                                        0) ;
BOOL bSendRequest = ::HttpSendRequest(hHttpFile, NULL, 0, 0, 0);

// Get the length of the file.            
char bufQuery[32] ;
DWORD dwLengthBufQuery = sizeof(bufQuery);
BOOL bQuery = ::HttpQueryInfo(hHttpFile,
                              HTTP_QUERY_CONTENT_LENGTH, 
                              bufQuery, 
                              &dwLengthBufQuery,NULL) ;

// Convert length from ASCII string to a DWORD.
DWORD dwFileSize = (DWORD)atol(bufQuery) ;
// Allocate a buffer for the file.   

char* buffer = new char[dwFileSize+1] ;

// Read the file into the buffer. 
DWORD dwBytesRead ;
BOOL bRead = ::InternetReadFile(hHttpFile,
                                buffer,
                                dwFileSize+1, 
                                &dwBytesRead);
buffer[dwBytesRead] = 0 ;

Other functions

HttpAddRequestHeaders

Adds one or more HTTP request headers to the HTTP request handle.

参考:http://www.cnblogs.com/songsu/articles/1346782.html

InternetSetCookie

Creates a cookie associated with the specified URL.

InternetGetCookie

Retrieves the cookie for the specified URL.

BOOL res=InternetSetCookie("http://www.microsoft.com","a","test");
TCHAR cookie[1024];
DWORD leh;
res=InternetGetCookie("http://www.microsoft.com",NULL,cookie,&leh);