TGQHttp 封装的IDhttp

 

TGQHttp<T: TEntityBase, constructor> = class(TIdHTTP)

 

function TGQHttp<T>.GetExSingle(url: string; paramList: THttpParams)
  : THttpResult<T>;
var
  inStream, outStream: TMemoryStream;
  prm: THttpParam;
  msg: string;
begin
  try
    outStream := TMemoryStream.Create;
    inStream := TMemoryStream.Create;
    if ((paramList <> nil) and (paramList.Count > 0)) then
    begin
      if (rightstr(url, 1) <> '?') then
        url := url + '?';
      for prm in paramList do
        url := url + prm.ToString();
    end;
    if (UpperCase(LeftStr(url, 4)) <> 'HTTP') then
      url := fRootURL + url;
    OutputDebugString(PWideChar('Get :' + url));
    self.Get(url, inStream);  //获取
    try
      if self.Response.ContentEncoding = 'gzip' then
      begin
        inStream.Position := 0;
        outStream := TMemoryStream.Create;
        GZDecompressStream(inStream, outStream);
        OutputDebugString(PWideChar('压缩传输:' + inttostr(inStream.Size) + '/' +
          inttostr(outStream.Size)));
        result := THttpResult<T>.Create(true, 'OK', outStream, url);
      end
      else
      begin
        result := THttpResult<T>.Create(true, 'OK', inStream, url);
        OutputDebugString(PWideChar('未压缩'));
      end;
    except
      on ex: Exception do
      begin
        result := THttpResult<T>.Create(false,
          '网络查询失败:请检测网络连接' + ex.Message, nil, url);
      end;
    end;

  finally
    FreeAndNil(paramList);
    FreeAndNil(inStream);
    FreeAndNil(outStream);
  end;

end;
function TGQHttp<T>.PostExSingle(url: string; paramList: THttpParams;
  postJson: string): THttpResult<T>;
var
  inStream, outStream: TMemoryStream;
  prms: TStringList;
  prm: THttpParam;
  msg: string;
begin
  try
    outStream := TMemoryStream.Create;
    inStream := TMemoryStream.Create;
    prms := TStringList.Create;
    if ((paramList <> nil) and (paramList.Count > 0)) then
    begin
      if (rightstr(url, 1) <> '?') then
        url := url + '?';
      for prm in paramList do
        url := url + prm.ToString();
    end;
    if (UpperCase(LeftStr(url, 4)) <> 'HTTP') then
      url := fRootURL + url;
    OutputDebugString(PWideChar('post aaa:' + url));
    try
      if (postJson <> '') then
        prms.Add(fJsonParamName + '=' + postJson);
      self.Post(url, prms, inStream);
      if self.Response.ContentEncoding = 'gzip' then
      begin
        inStream.Position := 0;
        outStream := TMemoryStream.Create;
        GZDecompressStream(inStream, outStream);
        OutputDebugString(PWideChar('压缩传输:' + inttostr(inStream.Size) + '/' +
          inttostr(outStream.Size)));
        result := THttpResult<T>.Create(true, 'OK', outStream, url,
          iif(prms = nil, '', prms.Text));

      end
      else
      begin
        result := THttpResult<T>.Create(true, 'OK', inStream, url,
          iif(prms = nil, '', prms.Text));
        OutputDebugString(PWideChar('未压缩'));
      end;
    except
      on ex: Exception do
      begin
        result := THttpResult<T>.Create(false, '网络查询失败:请检测网络连接' + ex.Message,
          nil, url, iif(prms = nil, '', prms.Text));
      end;
    end;

  finally
    FreeAndNil(paramList);
    FreeAndNil(prms);
    FreeAndNil(inStream);
    FreeAndNil(outStream);
  end;

end;

 

调用:

r:THttpResult<TForbidToExpresswayResult>;
r := TForbidToExpresswayResult.Post(gpsParam);

{ TForbidToExpresswayResult } 
class function TForbidToExpresswayResult.Post(aParam: TForbidToExpresswayParam) 
: THttpResult<TForbidToExpresswayResult>; 
begin  
  Result := THttpHelper.HttpAdpter.
  HTTPPostSingle<TForbidToExpresswayResult>(APINumber, nil,
  aParam.ToJsonStr);
end; 
//-------------
function THttpAdpter.HTTPPostSingle<T>(httpName: string;
ParamList: THttpParams;jsonStr:string): THttpResult<T>;
var
  http: TGQHttp<T>;
begin
  http := CreateHttp<T>();
  result := http.PostExSingle(httpName,  ParamList,jsonStr);
  FreeAndNil(http);
end;
function THttpAdpter.CreateHttp<T>(): TGQHttp<T>;
begin
  result := TGQHttp<T>.Create(nil,self.fZip,fRootURL,fJsonParamName);
end;
调用

 

posted @ 2016-03-16 17:38  海蓝7  阅读(110)  评论(0)    收藏  举报