F
fly_phoenix
Unregistered / Unconfirmed
GUEST, unregistred user!
我做了一个http代理服务器,用到了indy的IdMappedPortTCP,代码在大富翁里找的,
OnOutbondData中显示WEB服务器返的数据时出现问题,返回的html文档在memo中出现乱码
源码如下
IdMappedPortTCP1 : TIdMappedPortTCP;
idThreadMgrDefault1 : TidThreadMgrDefault1;
memo : Tmemo;
function TfmProxy.GetHost(URL : string) : string;
var
LURI : TIdURI;
begin
LURI := TIdURI.Create(URL);
result := LURI.Host;
LURI.Free;
end;
//.获取端口号
function TfmProxy.GetPort(URL : string) : integer;
var
LURI : TIdURI;
begin
LURI := TIdURI.Create(URL);
if Length(LURI.Port) <> 0 then
result := StrToInt(LURI.Port)
else
result := 80;
LURI.Free;
end; //GetPort
//.删除URL中的HOST字符串
function TfmProxy.DelHostOfURL(URL, Host : string; Port : integer) : string;
var
s : string;
begin
result := URL;
s := 'http://' + Host;
if Port <> 80 then
s := s + ':' + IntToStr(Port);
Delete(result, pos(s, result), Length(s));
end;
procedure TfmProxy.FormCreate(Sender : TObject);
begin
IdMappedPortTCP1.DefaultPort := 8080;
self.IdMappedPortTCP1.Active := True;
end;
procedure TfmProxy.IdMappedPortTCP1Execute(AThread : TIdMappedPortThread);
var
RequestHost : string;
RequestPort : integer;
begin
try
RequestHost := GetHost(AThread.NetData);
RequestPort := GetPort(AThread.NetData);
AThread.NetData := DelHostOfURL(AThread.NetData, RequestHost, RequestPort);
Memo1.Lines.Add(RequestHost);
if (RequestHost <> IdMappedPortTCP1.MappedHost) or
(RequestPort <> IdMappedPortTCP1.MappedPort) then
begin
IdMappedPortTCP1.MappedHost := RequestHost;
IdMappedPortTCP1.MappedPort := RequestPort;
TidTcpClient(AThread.OutboundClient).Host := RequestHost;
TidTcpClient(AThread.OutboundClient).Port := RequestPort;
TidTcpClient(AThread.OutboundClient).Disconnect;
TidTcpClient(AThread.OutboundClient).Connect(AThread.ConnectTimeOut);
end; except
;
end;
end;
procedure TfmProxy.IdMappedPortTCP1OutboundData(
AThread : TIdMappedPortThread);
begin
//捕捉Web服务器返回的数据
Memo1.Lines.Add(AThread.NetData);
//出现乱码,在有的计算机上所有中文都为乱码,在有的计算机上用Google搜索时出现乱码
//可能与网页使用的编码有关UTF-8,但我用了几个相关的函数都转换不过来
//不知哪位大侠能指明迷津。
end;
OnOutbondData中显示WEB服务器返的数据时出现问题,返回的html文档在memo中出现乱码
源码如下
IdMappedPortTCP1 : TIdMappedPortTCP;
idThreadMgrDefault1 : TidThreadMgrDefault1;
memo : Tmemo;
function TfmProxy.GetHost(URL : string) : string;
var
LURI : TIdURI;
begin
LURI := TIdURI.Create(URL);
result := LURI.Host;
LURI.Free;
end;
//.获取端口号
function TfmProxy.GetPort(URL : string) : integer;
var
LURI : TIdURI;
begin
LURI := TIdURI.Create(URL);
if Length(LURI.Port) <> 0 then
result := StrToInt(LURI.Port)
else
result := 80;
LURI.Free;
end; //GetPort
//.删除URL中的HOST字符串
function TfmProxy.DelHostOfURL(URL, Host : string; Port : integer) : string;
var
s : string;
begin
result := URL;
s := 'http://' + Host;
if Port <> 80 then
s := s + ':' + IntToStr(Port);
Delete(result, pos(s, result), Length(s));
end;
procedure TfmProxy.FormCreate(Sender : TObject);
begin
IdMappedPortTCP1.DefaultPort := 8080;
self.IdMappedPortTCP1.Active := True;
end;
procedure TfmProxy.IdMappedPortTCP1Execute(AThread : TIdMappedPortThread);
var
RequestHost : string;
RequestPort : integer;
begin
try
RequestHost := GetHost(AThread.NetData);
RequestPort := GetPort(AThread.NetData);
AThread.NetData := DelHostOfURL(AThread.NetData, RequestHost, RequestPort);
Memo1.Lines.Add(RequestHost);
if (RequestHost <> IdMappedPortTCP1.MappedHost) or
(RequestPort <> IdMappedPortTCP1.MappedPort) then
begin
IdMappedPortTCP1.MappedHost := RequestHost;
IdMappedPortTCP1.MappedPort := RequestPort;
TidTcpClient(AThread.OutboundClient).Host := RequestHost;
TidTcpClient(AThread.OutboundClient).Port := RequestPort;
TidTcpClient(AThread.OutboundClient).Disconnect;
TidTcpClient(AThread.OutboundClient).Connect(AThread.ConnectTimeOut);
end; except
;
end;
end;
procedure TfmProxy.IdMappedPortTCP1OutboundData(
AThread : TIdMappedPortThread);
begin
//捕捉Web服务器返回的数据
Memo1.Lines.Add(AThread.NetData);
//出现乱码,在有的计算机上所有中文都为乱码,在有的计算机上用Google搜索时出现乱码
//可能与网页使用的编码有关UTF-8,但我用了几个相关的函数都转换不过来
//不知哪位大侠能指明迷津。
end;