XMLHTTPRequest.Send中的Access Denied错误

时间:2016-11-18 06:04:30

标签: delphi

我正在使用以下Delphi代码下载JIRA问题的附件内容。发送请求引发拒绝访问的异常。是否与安全证书有关:

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 个答案:

答案 0 :(得分:-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;