WinINet function(1)
2011-11-27 10:48 Clingingboy 阅读(939) 评论(0) 编辑 收藏 举报
一.Url相关函数
1.InternetCreateUrl
Cracks a URL into its component parts.
#define URL_STRING_TEST "http://www.cnblogs.com/Clingingboy/archive/2011/11/20/2256200.html"
void main()
{
TCHAR szHostName[128];
TCHAR szUrlPath[256];
URL_COMPONENTS crackedURL;
ZeroMemory(&crackedURL, sizeof (URL_COMPONENTS));
crackedURL.dwStructSize = sizeof (URL_COMPONENTS);
crackedURL.lpszHostName = szHostName;
crackedURL.dwHostNameLength = sizeof(szHostName);
crackedURL.lpszUrlPath = szUrlPath;
crackedURL.dwUrlPathLength = sizeof(szUrlPath);
InternetCrackUrl(URL_STRING_TEST,(DWORD)strlen(URL_STRING_TEST),0,&crackedURL);
}
2.InternetCreateUrl
Creates a URL from its component parts.
DWORD urlLength;
InternetCreateUrl(&crackedURL,ICU_ESCAPE,szUrlPath,&urlLength);
3.InternetCanonicalizeUrl
Canonicalizes a URL, which includes converting unsafe characters and spaces into escape sequences.
InternetCanonicalizeUrl("http://www.xxx.com/viewthread.php?action=printable&tid=99 ",szUrlPath,&urlLength,ICU_ENCODE_SPACES_ONLY);
4.InternetCombineUrl
Combines a base and relative URL into a single URL.
InternetCombineUrl("http://www.xxx.com/","2256200.html",szUrlPath,&urlLength,ICU_BROWSER_MODE);
二.Internet基本应用相关函数
1.InternetOpen
Initializes an application's use of the WinINet functions.
2.InternetOpenUrl
Opens a resource specified by a complete FTP, Gopher, or HTTP URL.
3.InternetReadFile
Reads data from a handle opened by the InternetOpenUrl, FtpOpenFile, GopherOpenFile, or HttpOpenRequest function
4.InternetCloseHandle
Closes a single Internet handle.
应用1:打开因特网上指定的文件
void Download()
{
DWORD byteread=0;
char buffer[100];
memset(buffer,0,100);
HINTERNET internetopen;
internetopen=InternetOpen("Testing",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
if (internetopen==NULL)
{
cout<<"Internet open failed!"<<endl;
return;
}
HINTERNET internetopenurl;
internetopenurl=InternetOpenUrl(internetopen,"http://www.cnblogs.com/Clingingboy/archive/2011/11/20/2256200.html",NULL,0,INTERNET_FLAG_RELOAD,0);
if (internetopenurl==NULL)
{
cout<<"Internet open url failed!"<<endl;
goto there;
}
BOOL hwrite;
DWORD written;
HANDLE createfile;
createfile=CreateFile("C://a.html",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if (createfile==INVALID_HANDLE_VALUE)
{
cout<<"Create File failed!"<<endl;
goto next;
}
BOOL internetreadfile;
while(1)
{
internetreadfile=InternetReadFile(internetopenurl,buffer,sizeof(buffer),&byteread);
if(byteread==0)
break;
hwrite=WriteFile(createfile,buffer,sizeof(buffer),&written,NULL);
if (hwrite==0)
{
cout<<"Write to file failed!"<<endl;
goto here;
}
}
cout<<"Finished downloading!"<<endl;
here:
CloseHandle(createfile);
next:
InternetCloseHandle(internetopenurl);
there:
InternetCloseHandle(internetopen);
}
三.连接检查函数
1.InternetCheckConnection
Allows an application to check if a connection to the Internet can be established.
BOOL result=InternetCheckConnection(URL_STRING_TEST,FLAG_ICC_FORCE_CONNECTION,0);
2.InternetAttemptConnect
Attempts to make a connection to the Internet.
BOOL result=InternetAttemptConnect(0);