先InternetSetOption,然后InternetConnect,函数原型如下,在MSDN里面可以查到
HINTERNET InternetConnect(
IN HINTERNET hInternetSession,
IN LPCSTR lpszServerName,
IN INTERNET_PORT nServerPort,
IN LPCSTR lpszUsername,
IN LPCSTR lpszPassword,
IN DWORD dwService,
IN DWORD dwFlags,
IN DWORD dwContext
);
另外转高手代码如下
ttp://www.delphibbs.com/delphibbs/dispq.asp?lid=2438817
var
hSession, hConnect, hRequest: hInternet;
HostName, FileName: string;
AAgent: string;
Buf: Pointer;
dwBufLen, dwIndex: DWord;
RequestMethod: PChar;
InternetFlag: DWord;
AcceptType: LPStr;
procedure CloseHandles;
begin
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
end;
begin
try
ParseURL(FURL, HostName, FileName);
hSession := InternetOpen(nil,INTERNET_OPEN_TYPE_PRECONFIG, nil,nil,0);//没有使用代理
hConnect := InternetConnect(hSession, PChar(HostName), INTERNET_DEFAULT_HTTP_PORT,
nil,nil, INTERNET_SERVICE_HTTP, 0, 0);//这里可能需要用户名和密码
RequestMethod := 'GET'
InternetFlag := INTERNET_FLAG_RELOAD;
AcceptType := PChar('Accept: ' + '*/*');
hRequest := HttpOpenRequest(hConnect, RequestMethod, PChar(FileName), 'HTTP/1.0',
PChar(FReferer), @AcceptType, InternetFlag, 0);
HttpSendRequest(hRequest, nil, 0, nil, 0)
dwIndex := 0;
dwBuflen := 1024;
GetMem(Buf, dwBuflen);
FResult := HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH, Buf, dwBuflen, dwIndex);
if FResult then FFileSize := StrToInt(StrPas(Buf));//这里就是文件大小了
{...比较文件的大小...}
FreeMem(Buf);
CloseHandles;
except
end;
另一高手
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2079124
const
cBuffsize = 1024 * 64;
var
httpContext: Integer;
HttpServer, UserID, Passwd: string;
BufferIn: INTERNET_BUFFERS;
RemoteFile: string;
LocalFile: string;
httpFile: THandle;
readInF: DWORD;
oBytesWritten: DWORD;
rRead: BOOL;
Count: DWORD;
HttpNet, HttpConnect, HttpRequest: HINTERNET;
pBuffer: string;
begin
LocalFile := 'W:/ccc.txt';
RemoteFile := '/aaaaa.txt';
HttpServer := '127.0.0.1';
UserID := 'anonymous';
Passwd := 'on@the.net';
HttpNet := InternetOpen(PChar(HttpServer), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
httpContext := 0;
HttpConnect := InternetConnect(HttpNet, PChar(HttpServer), 0, PChar(UserID),
PChar(Passwd), INTERNET_SERVICE_HTTP, 0, httpContext);
HttpRequest := HttpOpenRequest(HttpConnect, 'PUT', PChar(RemoteFile),
nil, nil, nil, 0, 0); //'GET'
httpFile := CreateFile(PChar(LocalFile), GENERIC_READ, FILE_SHARE_READ,
nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
ReadInF := 0;
FillChar(BufferIn, SizeOf(BufferIn), 0);
BufferIn.dwStructSize := SizeOf(BufferIn);
BufferIn.dwBufferLength := cBuffsize;
BufferIn.dwBufferTotal := GetFileSize(httpFile, nil);
HttpSendRequestEx(HttpRequest, @BufferIn, nil, HSR_INITIATE, 0);
repeat
SetLength(pBuffer, cBuffsize);
ReadFile(httpFile, Pointer(pBuffer)^, cBuffsize, Count, nil);
ReadInF := ReadInF + Count;
rRead := InternetWriteFile(httpRequest, Pointer(pBuffer), Count, oBytesWritten);
until ReadInF = BufferIn.dwBufferTotal;
CloseHandle(httpFile);
HttpEndRequest(HttpRequest, nil, 0, 0);
InternetCloseHandle(HttpRequest);
InternetCloseHandle(HttpConnect);
InternetCloseHandle(HttpNet);
end;