2016-11-18 56 views
0

我正在使用以下Delphi代碼來下載JIRA問題的附件內容。發送請求引發拒絕訪問異常。是任何安全證書相關的問題:XMLHTTPRequest.Send中的拒絕訪問錯誤

class function TnsXmlHttpReq.GetRequestForStream(const p_Url, p_UsrName, 
    p_Password: String; out p_Status: Integer): TMemoryStream; 
var 
    LBody : OleVariant; 
    Stream: IStream; 
    OleStream: TOleStream; 
begin 
    Result:= nil; 
    p_Status:= 0; 

    if FHttpRequest = nil then 
    FHttpRequest:= CoXMLHTTPRequest.Create; 

    FHttpRequest.open('GET', p_Url, false, p_UsrName, p_Password); 
    FHttpRequest.setRequestHeader('X-Atlassian-Token', 'no-check'); 
    FHttpRequest.setRequestHeader('Authorization','Basic '+EncodeBase64(p_UsrName+':'+p_Password)); 
    FHttpRequest.setRequestHeader('Cache-Control', 'no-cache'); 


    //send request 
    FHttpRequest.send(''); 

    p_Status:= FHttpRequest.status; 

    if FHttpRequest.status <> 200 then 
    Exit; 

    Result:= TMemoryStream.Create; 
    Stream := IUnknown(FHttpRequest.ResponseStream) as IStream; 
    OleStream := TOleStream.Create(Stream); 
    try 
    OleStream.Position := 0; 
    Result.CopyFrom(OleStream, OleStream.Size); 
    finally 
    OleStream.Free; 
    end;//try..finally... 
end; 

回答

-1

您可以使用標準的Delphi的HttpClient如下

uses System.Net.HttpClient; 

function GetAttachment(const AURL: string; const AUserName: string; 
    const APassword: string): TStream; 
var 
    HttpClient: THttpClient; 
    BasicAuth: string; 
begin 
    HttpClient := THttpClient.Create; 
    try 
    Result := TMemoryStream.Create; 
    BasicAuth := TRESTHTTPProcs.CreateBasicAuthenticationString('', AUserName, APassword); 
    HttpClient.Get(AURL, Result, TNetHeaders.Create(
     TNameValuePair.Create('X-Atlassian-Token', 'nocheck'), 
     TNameValuePair.Create(HTTP_HEADERFIELD_AUTH, BasicAuth) 
    )); 
    finally 
    HttpClient.Free; 
    end; 
end;