ucomm

unit uComm;

interface
  uses classes,msxml2_tlb,Windows;

type
  TPubToolKit = class(TObject)
  private
    FXmlHttp : IXMLHTTPRequest;
  public
    class function NewInstance: TObject; override;
    procedure FreeInstance; override;
    class function RefCount: Integer;
    function isHttpSerOk(sTestUrl: string): boolean;
    function doGet(sUrl: string): OleVariant;
    function doGet2(sUrl: string): string;
    function ProcPost(sUrl:string; PostStream,rtnStream: TStream):boolean;overload;
    function ProcPost(sUrl: string; PostStream: TStream): TStream;overload;
    property xmlHttpReq : IXMLHTTPRequest read FXmlHttp;
  end;

var
    Ptk : TPubToolKit;
    Ref_Count : Integer = 0;
    HTTPCS: TRTLCriticalSection ;

implementation
uses variants,axctrls,Activex,SysUtils;

procedure SaveXMLHttpResponseToStream(xmlHttp : IXMLHTTPRequest;AStream: TStream);
var
  OleStream: TOleStream;
begin
  OleStream := TOleStream.Create(IStream(IUnknown(xmlHttp.responseStream)));
  try
    try
      AStream.CopyFrom(OleStream, OleStream.Size);
    except
      on e : exception do
        showmessage(e.Message);
    end;
  finally
    OleStream.Free;
  end;
end;

function TPubToolKit.isHttpSerOk(sTestUrl:string):boolean;
begin
  EnterCriticalSection(HTTPCS);
  Result := false;
  try
    try
      //向服务器发送一个Test请求,只要状态为200,则表示跟服务器是可连通的状态
      FXmlHttp.open('GET',sTestUrl,false,EmptyParam,EmptyParam);
      FXmlHttp.send( EmptyParam );
      result := FXmlHttp.status = 200;
    except
      on e:Exception do
      begin
        e.Message := '连接失败:' + e.Message ;
        raise;
      end;
    end;
  finally
    LeaveCriticalSection(HTTPCS);
  end;
end;

function TPubToolKit.ProcPost(sUrl:string; PostStream,rtnStream: TStream):boolean;
begin
  result :=false;
  EnterCriticalSection(HTTPCS);
  try
    try
      FXmlHttp.open('POST',sURL,false,EmptyParam,EmptyParam);
      FXmlHttp.send( StreamToVariant(PostStream) );
      if FXmlHttp.status = 200 then begin
        SaveXMLHttpResponseToStream(FXmlHttp,rtnStream);
        result := true;
      end else
        StringToStream(rtnStream,FXmlHttp.statusText);
    except
      on e:Exception do
      begin
        e.Message := '连接失败:' + e.Message ;
        raise;
      end;
    end;
  finally
     LeaveCriticalSection(HTTPCS);
  end;
end;

function TPubToolKit.ProcPost(sUrl:string; PostStream: TStream):TStream;
begin
  try
    Result := TMemoryStream.Create;
    ProcPost(sUrl,PostStream,Result);
  except
    if Assigned(Result) then FreeAndNil(Result);
    raise;
  end;
end;

function TPubToolKit.doGet(sUrl:string): OleVariant;
begin
  EnterCriticalSection(HTTPCS);
  try
    try
      FXmlHttp.open('GET',sURL,false,EmptyParam,EmptyParam);
      FXmlHttp.send(EmptyParam);
      Result := FXmlHttp.responseBody;
    except
      on e:Exception do
      begin
        e.Message := '连接失败:' + e.Message ;
        raise;
      end;
    end;
  finally
    LeaveCriticalSection(HTTPCS);
  end;
end;

function TPubToolKit.doGet2(sUrl: string): string;
begin
  EnterCriticalSection(HTTPCS);
  try
    try
      FXmlHttp.open('GET',sURL,false,EmptyParam,EmptyParam);
      FXmlHttp.send(EmptyParam);
      Result := FXmlHttp.responseText;
    except
      on e:Exception do
      begin
        e.Message := '连接失败:' + e.Message ;
        raise;
      end;
    end;
  finally
    LeaveCriticalSection(HTTPCS);
  end;
end;

{ TChObj }
procedure TPubToolKit.FreeInstance;
begin
    // 减少引用计数
  Dec(Ref_Count);
    //当引用计数为0时,释放该对象实例
  if (Ref_Count = 0) then
  begin
    DeleteCriticalSection(HTTPCS);
    Ptk := nil;
    FXmlHttp := nil;
    inherited FreeInstance;
  end;
end;

class function TPubToolKit.NewInstance: TObject;
begin
  if (not Assigned(Ptk) ) then
  begin
    Ptk := inherited NewInstance as TPubToolKit;
    // 在这里可以初始化些局部变量例如:
    with Ptk do
    begin
      FXmlHttp := CoXMLHTTP.Create;
    end;
    InitializeCriticalSection(HTTPCS);
  end 
  Result := Ptk;
  // 增加引用计数
  Inc( Ref_Count );
end;

class function TPubToolKit.RefCount: Integer;
begin
  //返回引用计数
  Result := Ref_Count;
end;

end.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值