3
3h
Unregistered / Unconfirmed
GUEST, unregistred user!
想连接到某一个地址中。
这个函数是分析URL的:
function TMainForm.ExtractServer(URL: String; var Port: Integer; var ServerName,
ObjectName, UserName, Password: String): DWord;
const
DefaultEmail = '';
var
I: Integer;
U, Svr: String;
begin
If Trim(URL) = '' then
begin
Result := 0;
Exit;
end;
I := Pos('://', URL);
Svr := LowerCase(Copy(URL, 0, I - 1));
If (Svr = 'http') or (Svr = 'https') then
Result := INTERNET_SERVICE_HTTP
else If Svr = 'ftp' then
Result := INTERNET_SERVICE_FTP
else If Svr = 'gopher' then
Result := INTERNET_SERVICE_GOPHER
else
begin
Result := 0;
Exit;
end;
Inc(I, 3);
URL := Copy(URL, I, MaxInt);
I := Pos('/', URL);
If I = 0 then
begin
ServerName := URL;
ObjectName := '/';
end
Else
begin
ServerName := Copy(URL, 0, I - 1);
ObjectName := Copy(URL, I, MaxInt);
end;
I := Pos('@', ServerName);
If I = 0 then
begin
UserName := '';
Password := DefaultEmail;
end
else
begin
UserName := Copy(ServerName, 0, I - 1);
ServerName := Copy(ServerName, I + 1, MaxInt);
I := Pos(':', UserName);
If I = 0 then
Password := DefaultEmail
else
begin
Password := Copy(UserName, I + 1, MaxInt);
UserName := Copy(UserName, 0, I - 1);
end
end;
I := Pos(':', ServerName);
If I = 0 then
Case Result Of
INTERNET_SERVICE_HTTP:
Port := INTERNET_DEFAULT_HTTP_PORT;
INTERNET_SERVICE_FTP:
Port := INTERNET_DEFAULT_FTP_PORT;
INTERNET_SERVICE_GOPHER:
Port := INTERNET_DEFAULT_GOPHER_PORT;
else
Port := INTERNET_DEFAULT_HTTPS_PORT;
end
else
begin
Port := StrToIntDef(Copy(ServerName, I + 1, MaxInt), -1);
If Port = -1 then
Case Result Of
INTERNET_SERVICE_HTTP:
Port := INTERNET_DEFAULT_HTTP_PORT;
INTERNET_SERVICE_FTP:
Port := INTERNET_DEFAULT_FTP_PORT;
INTERNET_SERVICE_GOPHER:
Port := INTERNET_DEFAULT_GOPHER_PORT;
else
Port := INTERNET_DEFAULT_HTTPS_PORT;
end;
ServerName := Copy(ServerName, 0, I - 1);
end;
ServerName := ServerName + #0;
end;
经测试,分析的结果是正确的。
然后放一个按钮:
procedure TMainForm.BtnClick(Sender: TObject);
const
MaxLen = 1024;
var
URL: String;
AcceptTypes: LPSTR;
Hi, Hc, Hh: HINTERNET;
I, ServerType, Index, Len: DWORD;
Buf: String;
Port, Context: Integer;
ServerName, ObjectName, UserName, Password: String;
begin
URL := 'http://glowing.sw163.net/index.asp';
ServerType := ExtractServer(URL, Port, ServerName,
ObjectName, UserName, Password);
If ServerType <> 0 then
begin
SetLength(Buf, MaxLen);
Len := MaxLen;
AcceptTypes := 'text/*';
Hi := InternetOpen('Glowing He', INTERNET_OPEN_TYPE_DIRECT,
Nil, Nil, 0);
Hc := InternetConnect(Hi, PChar(ServerName),
Port,
PChar(UserName), PChar(Password),
ServerType, 0, 0);
I := GetLastError;
If I <> 0 then
begin
ShowMessage(IntToStr(I));
InternetGetLastResponseInfo(I, PChar(Buf), Len);
If Len > 0 then
ShowMessage(Copy(Buf, 0, Len));
Exit;
end;
Hh := HttpOpenRequest(
Hc,
Nil,
PChar(ObjectName),
Nil,
PChar(URL),
@AcceptTypes,
INTERNET_FLAG_DONT_CACHE or INTERNET_FLAG_NO_AUTO_REDIRECT,
0);
I := GetLastError;
If I <> 0 then
begin
ShowMessage(IntToStr(I));
InternetGetLastResponseInfo(I, PChar(Buf), Len);
If Len > 0 then
ShowMessage(Copy(Buf, 0, Len));
Exit;
end;
If Hh <> Nil then
begin
SetLength(Buf, MaxLen);
Len := MaxLen;
HttpQueryInfo(Hh, HTTP_QUERY_STATUS_TEXT, PChar(Buf), Len, Index);
Showmessage(inttostr(Len) + ', ' + inttostr(index));
showmessage(Copy(buf, 0, Len));
InternetCloseHandle(Hh);
end;
InternetCloseHandle(Hc);
InternetCloseHandle(Hi);
end;
end;
按理说,在执行InternetConnect的时候应该是很慢的,因为它要连接到一个网站上,
事实上执行起来特快——不可能吧,我用的是56K的MODEM:)
而且结论也是不正确的,总是显示“0,0”的窗口。
故求哪位高手哥们指点一下迷津,谢谢!
这个函数是分析URL的:
function TMainForm.ExtractServer(URL: String; var Port: Integer; var ServerName,
ObjectName, UserName, Password: String): DWord;
const
DefaultEmail = '';
var
I: Integer;
U, Svr: String;
begin
If Trim(URL) = '' then
begin
Result := 0;
Exit;
end;
I := Pos('://', URL);
Svr := LowerCase(Copy(URL, 0, I - 1));
If (Svr = 'http') or (Svr = 'https') then
Result := INTERNET_SERVICE_HTTP
else If Svr = 'ftp' then
Result := INTERNET_SERVICE_FTP
else If Svr = 'gopher' then
Result := INTERNET_SERVICE_GOPHER
else
begin
Result := 0;
Exit;
end;
Inc(I, 3);
URL := Copy(URL, I, MaxInt);
I := Pos('/', URL);
If I = 0 then
begin
ServerName := URL;
ObjectName := '/';
end
Else
begin
ServerName := Copy(URL, 0, I - 1);
ObjectName := Copy(URL, I, MaxInt);
end;
I := Pos('@', ServerName);
If I = 0 then
begin
UserName := '';
Password := DefaultEmail;
end
else
begin
UserName := Copy(ServerName, 0, I - 1);
ServerName := Copy(ServerName, I + 1, MaxInt);
I := Pos(':', UserName);
If I = 0 then
Password := DefaultEmail
else
begin
Password := Copy(UserName, I + 1, MaxInt);
UserName := Copy(UserName, 0, I - 1);
end
end;
I := Pos(':', ServerName);
If I = 0 then
Case Result Of
INTERNET_SERVICE_HTTP:
Port := INTERNET_DEFAULT_HTTP_PORT;
INTERNET_SERVICE_FTP:
Port := INTERNET_DEFAULT_FTP_PORT;
INTERNET_SERVICE_GOPHER:
Port := INTERNET_DEFAULT_GOPHER_PORT;
else
Port := INTERNET_DEFAULT_HTTPS_PORT;
end
else
begin
Port := StrToIntDef(Copy(ServerName, I + 1, MaxInt), -1);
If Port = -1 then
Case Result Of
INTERNET_SERVICE_HTTP:
Port := INTERNET_DEFAULT_HTTP_PORT;
INTERNET_SERVICE_FTP:
Port := INTERNET_DEFAULT_FTP_PORT;
INTERNET_SERVICE_GOPHER:
Port := INTERNET_DEFAULT_GOPHER_PORT;
else
Port := INTERNET_DEFAULT_HTTPS_PORT;
end;
ServerName := Copy(ServerName, 0, I - 1);
end;
ServerName := ServerName + #0;
end;
经测试,分析的结果是正确的。
然后放一个按钮:
procedure TMainForm.BtnClick(Sender: TObject);
const
MaxLen = 1024;
var
URL: String;
AcceptTypes: LPSTR;
Hi, Hc, Hh: HINTERNET;
I, ServerType, Index, Len: DWORD;
Buf: String;
Port, Context: Integer;
ServerName, ObjectName, UserName, Password: String;
begin
URL := 'http://glowing.sw163.net/index.asp';
ServerType := ExtractServer(URL, Port, ServerName,
ObjectName, UserName, Password);
If ServerType <> 0 then
begin
SetLength(Buf, MaxLen);
Len := MaxLen;
AcceptTypes := 'text/*';
Hi := InternetOpen('Glowing He', INTERNET_OPEN_TYPE_DIRECT,
Nil, Nil, 0);
Hc := InternetConnect(Hi, PChar(ServerName),
Port,
PChar(UserName), PChar(Password),
ServerType, 0, 0);
I := GetLastError;
If I <> 0 then
begin
ShowMessage(IntToStr(I));
InternetGetLastResponseInfo(I, PChar(Buf), Len);
If Len > 0 then
ShowMessage(Copy(Buf, 0, Len));
Exit;
end;
Hh := HttpOpenRequest(
Hc,
Nil,
PChar(ObjectName),
Nil,
PChar(URL),
@AcceptTypes,
INTERNET_FLAG_DONT_CACHE or INTERNET_FLAG_NO_AUTO_REDIRECT,
0);
I := GetLastError;
If I <> 0 then
begin
ShowMessage(IntToStr(I));
InternetGetLastResponseInfo(I, PChar(Buf), Len);
If Len > 0 then
ShowMessage(Copy(Buf, 0, Len));
Exit;
end;
If Hh <> Nil then
begin
SetLength(Buf, MaxLen);
Len := MaxLen;
HttpQueryInfo(Hh, HTTP_QUERY_STATUS_TEXT, PChar(Buf), Len, Index);
Showmessage(inttostr(Len) + ', ' + inttostr(index));
showmessage(Copy(buf, 0, Len));
InternetCloseHandle(Hh);
end;
InternetCloseHandle(Hc);
InternetCloseHandle(Hi);
end;
end;
按理说,在执行InternetConnect的时候应该是很慢的,因为它要连接到一个网站上,
事实上执行起来特快——不可能吧,我用的是56K的MODEM:)
而且结论也是不正确的,总是显示“0,0”的窗口。
故求哪位高手哥们指点一下迷津,谢谢!