本文介绍了我的GetHttp适用于HTTP,但HTTPS返回无效数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的GetHttp与HTTP协同工作,但HTTPS返回无效数据,这是测试过程: GetHTTP('','',temp,true);
temp.SaveToFile('c:\temp.txt');
如何修复任何想法?
函数GetHTTP(AUrl,APostStr:ansistring; AStream:TStream; noncached:boolean = false):integer;
var
Retry:Boolean;
hSession,hConnect,hRequest:hInternet;
RequestMethod,Header:ansistring;
Buf:数组[0..1023]的Char;
ReadCount:Cardinal;
dwError,dwErrorCode:DWORD;
ptNil:指针;
UrlInfo:TUrlInfo;
flags:DWORD;
程序SetTimeOut;
var
TimeOut:integer;
begin
TimeOut:= 5 * 1000;
InternetSetOption(hSession,INTERNET_OPTION_RECEIVE_TIMEOUT,@TimeOut,
SizeOf(TimeOut));
结束
procedure GetHttpStatus; // StatusCode
var
Len,Reserved:DWORD;
begin
保留:= 0;
Len:= SizeOf(Result);
HttpQueryInfo(hRequest,HTTP_QUERY_STATUS_CODE或HTTP_QUERY_FLAG_NUMBER,
@Result,Len,Reserved);
结束
开始
标志:= 0;
if noncached = true then flags:= INTERNET_FLAG_RELOAD;
// flags:= INTERNET_FLAG_SECURE;这个不帮助??输出为空,
结果:= 0;
如果不是GetUrlInfo(AUrl,UrlInfo)然后退出;
hSession:= InternetOpen(nil,INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
try
SetTimeOut;
hConnect:= InternetConnect(hSession,pchar(UrlInfo.hostname),urlinfo.port,nil,nil,INTERNET_SERVICE_HTTP,INTERNET_FLAG_EXISTING_CONNECT,0);
尝试
如果APostStr =''然后
RequestMethod:='GET'
else
RequestMethod:='POST';
hRequest:= HttpOpenRequest(hConnect,PChar(RequestMethod),Pchar(urlinfo.urlpath),'HTTP / 1.0',nil,nil,flags,0);
尝试
如果APostStr =''然后
标题:=''
其他
标题:='内容类型:应用程序/ x-www形式的
重试:= True;如果HttpSendRequest(hRequest,PChar(Header),Length(Header),PChar(APostStr),Length(APostStr))然后
dwErrorCode:= ERROR_SUCCESS,则
,而Retry do
begin
else
dwErrorCode:= GetLastError;
dwError:= InternetErrorDlg(application.Handle,hRequest,dwErrorCode,
FLAGS_ERROR_UI_FILTER_FOR_ERRORS或
FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS或
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA,
ptNil);
重试:=(dwError = ERROR_INTERNET_FORCE_RETRY);
结束
// HttpSendRequest(hRequest,PChar(Header),Length(Header),PChar(APostStr),Length(APostStr));
GetHttpStatus;
如果Result<> HTTP_STATUS_OK然后退出;
而True do
begin
如果不是InternetReadFile(hRequest,@Buf,SizeOf(Buf),ReadCount)then
Break;
// res:= GetLastError();
如果ReadCount = 0然后
Break
else
AStream.Write(Buf,ReadCount);
结束
finally
InternetCloseHandle(hRequest);
结束
终于
InternetCloseHandle(hConnect);
结束
finally
InternetCloseHandle(hSession);
结束
结束
解决方案
除了Indy,您还可以使用Delphi的THTTPReqResp组件或看看它如何处理HTTPS;它使用WinInet)。以下是使用GET组件编写的GetHTTP:
函数GetHTTP(AUrl,APostStr:String; AStream:TStream; noncached:boolean = false):Boolean;
var
ReqResp:THTTPReqResp;
begin
结果:= False;
ReqResp:= THTTPReqResp.Create(nil);
try
{Get Case}
如果AUrl<> ''然后
begin
ReqResp.URL:= AUrl;
ReqResp.Get(AStream);
结果:= True;
结束
finally
ReqResp.Free;
结束
结束
my GetHttp works with HTTP but HTTPS returns invalid data, this is the test procedure:
GetHTTP('https://localhost/','',temp,true); temp.SaveToFile('c:\temp.txt');
How to fix, any ideas?
function GetHTTP(AUrl, APostStr:ansistring; AStream:TStream; noncached :boolean =false): integer;
var
Retry: Boolean;
hSession, hConnect, hRequest: hInternet;
RequestMethod, Header: ansistring;
Buf:array[0..1023] of Char;
ReadCount:Cardinal;
dwError, dwErrorCode: DWORD;
ptNil: Pointer;
UrlInfo: TUrlInfo;
flags : DWORD ;
procedure SetTimeOut;
var
TimeOut: integer;
begin
TimeOut := 5 * 1000;
InternetSetOption(hSession, INTERNET_OPTION_RECEIVE_TIMEOUT, @TimeOut,
SizeOf(TimeOut));
end;
procedure GetHttpStatus; //StatusCode
var
Len, Reserved: DWORD;
begin
Reserved := 0;
Len := SizeOf(Result);
HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE or HTTP_QUERY_FLAG_NUMBER,
@Result, Len, Reserved) ;
end;
begin
flags := 0;
if noncached = true then flags := INTERNET_FLAG_RELOAD;
// flags := INTERNET_FLAG_SECURE; THIS DID NOT HELP ??!!! output was blank with it
Result := 0;
if not GetUrlInfo(AUrl, UrlInfo) then Exit;
hSession := InternetOpen(nil, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
SetTimeOut;
hConnect := InternetConnect(hSession, pchar(UrlInfo.hostname),urlinfo.port,nil,nil,INTERNET_SERVICE_HTTP,INTERNET_FLAG_EXISTING_CONNECT ,0);
try
if APostStr = '' then
RequestMethod := 'GET'
else
RequestMethod := 'POST';
hRequest := HttpOpenRequest(hConnect, PChar(RequestMethod),Pchar(urlinfo.urlpath), 'HTTP/1.0', nil, nil,flags, 0);
try
if APostStr = '' then
Header := ''
else
Header := 'Content-type: application/x-www-form-urlencoded';
Retry:=True;
while Retry do
begin
if HttpSendRequest(hRequest, PChar(Header),Length(Header), PChar(APostStr), Length(APostStr)) then
dwErrorCode:=ERROR_SUCCESS
else
dwErrorCode:=GetLastError;
dwError:=InternetErrorDlg(application.Handle, hRequest, dwErrorCode,
FLAGS_ERROR_UI_FILTER_FOR_ERRORS or
FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS or
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA,
ptNil);
Retry:=(dwError=ERROR_INTERNET_FORCE_RETRY);
end;
//HttpSendRequest(hRequest, PChar(Header),Length(Header), PChar(APostStr), Length(APostStr));
GetHttpStatus;
if Result <> HTTP_STATUS_OK then Exit;
while True do
begin
if not InternetReadFile(hRequest, @Buf, SizeOf(Buf), ReadCount) then
Break;
//res := GetLastError();
if ReadCount = 0 then
Break
else
AStream.Write(Buf, ReadCount);
end;
finally
InternetCloseHandle(hRequest);
end;
finally
InternetCloseHandle(hConnect);
end;
finally
InternetCloseHandle(hSession);
end;
end;
解决方案
Besides Indy, you can also use Delphi's THTTPReqResp component (or look at how it handles HTTPS; it uses WinInet). Here's your GetHTTP written using the component for GET:
function GetHTTP(AUrl, APostStr: String; AStream:TStream; noncached :boolean =false): Boolean;
var
ReqResp: THTTPReqResp;
begin
Result := False;
ReqResp := THTTPReqResp.Create(nil);
try
{ Get Case }
if AUrl <> '' then
begin
ReqResp.URL := AUrl;
ReqResp.Get(AStream);
Result := True;
end;
finally
ReqResp.Free;
end;
end;
这篇关于我的GetHttp适用于HTTP,但HTTPS返回无效数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!