偶尔看到一个用vc++编写的文件下载程序,为了对其有更深的认识,下面作一点小小分析,不适之处还望见谅。
第一步:分析AfxParseURL函数。
原型:BOOL AFXAPI AfxParseURL(LPCTSTR pstrURL,DWORD& dwServiceType,CString& strServer,CString& strObject,INTERNET_PORT& nPort);
解释:pstrURL: A pointer to a string containing the URL to be parsed.
dwServiceType:Indicates the type of Internet service. Possible values are as follows: (只列出几个值,相见MSDN)
AFX_INET_SERVICE_FTP
AFX_INET_SERVICE_HTTP
AFX_INET_SERVICE_HTTPS
AFX_INET_SERVICE_GOPHER
strServer: The first segment of the URL following the service type.
strObject: An object that the URL refers to (may be empty).
nPort: Determined from either the Server or Object portions of the URL, if either exists.
应用实例:(环境:vc 6.0)
#include "afxinet.h"
#include "iostream"
using namespace std;
int main()
{
LPCTSTR url="http://image.baidu.com/donghua.gif"; //typedef CONST CHAR *LPCSTR, *PCSTR;
//typedef LPCSTR LPCTSTR;
DWORD dwServiceType;//typedef unsigned long DWORD;
CString strServer;
CString strObject;
INTERNET_PORT nPort; //typedef unsigned short WORD;
//typedef WORD INTERNET_PORT;
AfxParseURL(url,dwServiceType,strServer,strObject,nPort);
cout<<dwServiceType<<endl;//输出3.
cout<<nPort<<endl; //输出80.
MessageBox(NULL,strServer,"待解析的URL服务器名",MB_OK); // image.baidu.com
MessageBox(NULL,strObject,"待解析的URL 涉及的对象(可能为空)",MB_OK); // /donghua.gif
return 0;
}
实例分析:url这个参数,我们可以随便赋值,只是影响AfxParseURL的解析结果,当然我们下载的时候都是从某个网址下载。
dwServiceType这个参数说明了服务的类型。从下面可以看出3代表http(系统定义的)。
#define INTERNET_SERVICE_URL 0
#define INTERNET_SERVICE_FTP 1
#define INTERNET_SERVICE_GOPHER 2
#define INTERNET_SERVICE_HTTP 3nPort:80代表http服务的端口号。
第二步:下次分析。