2009-11-24 2 views
4

내 GetHttp는하지만 HTTPS가 잘못된 데이터를 반환 HTTP와 함께 작동 잘못된 데이터를 반환,이 시험 절차 :내 GetHttp는 HTTP와 함께 작동하지만 HTTPS는

GetHTTP ('https://localhost/', '', 온도, TRUE); temp.SaveToFile ('c : \ temp.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; 
+0

은 HTTPS에 로컬 인증서가 필요하지 않습니까? – dusoft

답변

2

난 단지 OpenSSL를 사용하고 별도의 DLL을 몇 필요 인디,이 일을했다. 또한 SSL 용 IO 처리기를 명시 적으로 만들어야합니다. 다음은 간단한 코드 예제입니다.

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; 
0

HTTPS는 다른 포트에서 실행하고,이 윈속은 보안 연결을 수행 말할 필요가있다.

urlinfo.port이 https 요청의 경우 443으로 설정되어 있는지 확인하십시오.
이 있으니 https 요청에 INTERNET_FLAG_SECURE 플래그를 설정하십시오.

2

Indy 외에 Delphi의 THTTPReqResp 구성 요소 (또는 WinInet을 사용하는 HTTPS를 처리하는 방법을 살펴보십시오)를 사용할 수도 있습니다. 다음은 GET 용 구성 요소를 사용하여 작성한 GetHTTP입니다.

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; 
+0

IIRC는 운영 체제의 내장 SSL 라이브러리를 사용하기 때문에 특별한 SSL 라이브러리가 필요 없다는 이점이 있습니다. – mjn

관련 문제