English 中文(简体)
我的GetHttp与HTTP合作,但HTTPS将数据无效。
原标题:my GetHttp works with HTTP but HTTPS returns invalid data
  • 时间:2009-11-24 11:56:19
  •  标签:
  • delphi

我的GetHttp与HTTP合作,但HTTPS将无效数据退回。

GetHTTP( https://localhost/ , ,temp,true); temp.SaveToFile( c: emp.txt );

如何确定任何想法?

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;
问题回答

我只是在Indy这样做的,它使用OpenSSL,并且需要另外几个DLs。 你们还需要明确为国家扫盲运动设立一个国际综合组织手稿。 这里是一个简化的法典实例。

MyHTTP := GetIdHTTP(True);
MyHTTP.Get(SomeSecureURL, MyStream);

......

function GetIdHTTP(aSecure: Boolean = False): TIdHTTP;
var
  lSSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  Result := TIdHTTP.Create(nil);

  if aSecure then
  begin
    lSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
    Result.IOHandler := lSSLIOHandler;
  end;
end;

除因诺迪外,你还可以使用Delphi s THTTPReqResp部分(或研究如何处理HTTPS;它使用WinInet)。 随函附上您的《GetHTTP》利用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;

http://www.un.org/Depts/DGACM/index_french.htm

check that urlinfo.port is being set to 443 for https requests.
you have to set the INTERNET_FLAG_SECURE flag for https requests.





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签