怎么使用InternetSetOption代码通过需要密码验证的代理服务器 ( 积分: 300 )

  • 主题发起人 主题发起人 xxdino
  • 开始时间 开始时间
X

xxdino

Unregistered / Unconfirmed
GUEST, unregistred user!
ipi.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
ipi.lpszProxy := PChar(ServerHost + ':' + ServerPort);
InternetSetOption( nil, INTERNET_OPTION_PROXY, @ipi, SizeOf(ipi) );

这是使用代理服务器的程序,但是怎么样才能通过有密码的代理服务器呢?
我看到有两个参数:INTERNET_OPTION_PROXY_USERNAME 和 INTERNET_OPTION_PROXY_PASSWORD,但是不知道怎么使用阿?
请各位一定要帮忙啊
 
ipi.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
ipi.lpszProxy := PChar(ServerHost + ':' + ServerPort);
InternetSetOption( nil, INTERNET_OPTION_PROXY, @ipi, SizeOf(ipi) );

这是使用代理服务器的程序,但是怎么样才能通过有密码的代理服务器呢?
我看到有两个参数:INTERNET_OPTION_PROXY_USERNAME 和 INTERNET_OPTION_PROXY_PASSWORD,但是不知道怎么使用阿?
请各位一定要帮忙啊
 
ipi.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
ipi.lpszProxy := PChar(ServerHost + ':' + ServerPort);
InternetSetOption( nil, INTERNET_OPTION_PROXY, @ipi, SizeOf(ipi) );

这是使用代理服务器的程序,但是怎么样才能通过有密码的代理服务器呢?
我看到有两个参数:INTERNET_OPTION_PROXY_USERNAME 和 INTERNET_OPTION_PROXY_PASSWORD,但是不知道怎么使用阿?
请各位一定要帮忙啊
 
先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;
 
好象有点对不上啊?我是想问“怎么使用InternetSetOption代码通过需要密码验证的代理服务器 ”,不然浏览器每次都弹出输入密码确认的问题框
 
兄弟,MSND 里面 InternetSetOption 的介绍就是没有你说的东西的,密码是另外的函数,我给你的链接里面有说明整个过程的,我辛苦找给你,你都没去看?真伤心

InternetSetOption
BOOL InternetSetOption(
IN HINTERNET hInternet,
IN DWORD dwOption,
IN LPVOID lpBuffer,
IN DWORD dwBufferLength
);

Sets an Internet option.

Returns TRUE if successful, or FALSE otherwise. To get a specific error message, callGetLastError.
hInternet
Internet handle on which to set information.
dwOption
Internet option to set. Can be one of the Option Flags values.
lpBuffer
Address of a buffer that contains the option setting.
dwBufferLength
Length of the lpBuffer buffer.
 
chenybin, 这位兄弟已经把大概的原理都讲出来的!!
楼主还是仔细看看吧!!

只是需要转化一下就能达到你的要求了
 
后退
顶部