unit WorldLingo;
interface
Uses windows,winsock,Constus,PublicUnit, MessageUnit;
type
TWLInfo = Record
Text: WideString; //翻译文本 ,处理韩文使用widestring ,DialogboxW
SrcLang: String; //源语言
TrgLang: String; //译语言
Glossary: String; //翻译主题
end;
var
WInfo: TWLInfo;
procedure WLTranslate(hWnd: HWND);
implementation
function GetSession(Source: string):string;
Const
RecData='http://www.worldlingo.com/';
var
S:string;
PosInt:integer;
begin
S:='';
PosInt:= pos(RecData,Source);
if PosInt<>0 then
begin
S:=Copy(Source,PosInt+Length(RecData),33);
end;
Result:= S;
end;
function GetCookie(Source: string):string;
Const
RecData='Set-Cookie: JSESSIONID=';
var
S:string;
PosInt:integer;
begin
S:='';
PosInt:= pos(RecData,Source);
if PosInt<>0 then
begin
S:=Copy(Source,PosInt+12,Pos(';',Source)-PosInt-11);
end;
Result:= S;
end;
procedure SocketSend_1(S: TSocket; WSA: WSADATA; Phe
HostEnt; var Session,Cookie: String);
var
SendData, RecvData: String;
RecvBuf: array[0..1023] of Char;
RecvLen: Integer;
Terminated: Boolean;
begin
SendData:='GET /S591.3/loader HTTP/1.1'+#13#10+
'Referer: http://www.worldlingo.com/en/products_services/worldlingo_translator.html'+#13#10+
'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'+#13#10+
'Host: www.worldlingo.com'+#13#10+
'Connection: Keep-Alive'+#13#10#13#10;
Send(S,PChar(SendData)^,Length(SendData),0);
RecvLen:=1;
Terminated:= False;
Session:='';
Cookie:='';
While (RecvLen>0) and not (Terminated) do
begin
FillChar( RecvBuf, SizeOf( RecvBuf), 0);
RecvLen:=Recv( S, RecvBuf, 1024, 0);
if RecvLen<=0 then Break;
RecvData:= RecvData + RecvBuf;
Session:= GetSession(RecvData);
Cookie:= GetCookie(RecvData);
if (Trim(Session)<>'') and (Trim(Cookie)<>'') then Terminated:= True;
end;
end;
procedure SocketSend_2(S: TSocket; WSA: WSADATA; Phe
HostEnt; var Session, Cookie: String);
var
SendData, RecvData: String;
RecvBuf: array[0..1023] of Char;
RecvLen: Integer;
Terminated: Boolean;
begin
SendData:='GET /'+Session+'/object?wl_url=http%3A//www.worldlingo.com/en/products_services/worldlingo_translator.html HTTP/1.1'+#13#10+
'Referer: http://www.worldlingo.com/en/products_services/worldlingo_translator.html'+#13#10+
'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'+#13#10+
'Host: www.worldlingo.com'+#13#10+
'Connection: Keep-Alive'+#13#10+
'Cookie: '+Cookie+#13#10#13#10;
Send(S, PChar(SendData)^, Length(SendData), 0);
RecvLen:=1;
Terminated:= False;
Session:='';
While (RecvLen>0) and not (Terminated) do
begin
FillChar( RecvBuf, SizeOf( RecvBuf), 0);
RecvLen:=Recv( S, RecvBuf, 1024, 0);
if RecvLen<=0 then Break;
RecvData:= RecvData + RecvBuf;
Session:= GetSession(RecvData);
if Pos('Set-Cookie: JSESSIONID=',RecvData )<>0 then
Cookie:= GetCookie(RecvData);
if (Trim(Session)<>'') then Terminated:= True;
end;
end;
procedure SocketSend_3(S: TSocket; WSA: WSADATA; Phe
HostEnt; var Session, Cookie: String;
Text, SrcLang, TrgLang, Gl: String);
var
SendData, RecvData: String;
RecvBuf: array[0..1023] of Char;
RecvLen, SendLen: Integer;
Terminated: Boolean;
begin
SendLen:=171+Length(Text)+Length(SrcLang)+Length(TrgLang)+Length(Gl);
SendData:='POST /'+Session+'/texttranslate HTTP/1.1'+#13#10+
'Referer: http://www.worldlingo.com/en/products_services/worldlingo_translator.html'+#13#10+
'Content-Type: application/x-www-form-urlencoded'+#13#10+
'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'+#13#10+
'Host: www.worldlingo.com'+#13#10+
'Content-Length: '+inttostr(SendLen)+#13#10+
'Connection: Keep-Alive'+#13#10+
'Cookie: '+Cookie+#13#10#13#10+
'wl_url=http%3A%2F%2Fwww.worldlingo.com%2Fen%2Fproducts_services'+
'%2Fworldlingo_translator.html&wl_srcenc=utf-8&wl_text='+Text+
'&wl_srclang='+SrcLang+
'&wl_trglang='+TrgLang+
'&Submit=Translate'+
'&wl_glossary='+Gl;
Send(S,PChar(SendData)^,Length(SendData),0);
RecvLen:=1;
Terminated:= False;
While (RecvLen>0) and not (Terminated) do
begin
FillChar( RecvBuf, SizeOf( RecvBuf), 0);
RecvLen:=Recv( S, RecvBuf, 1024, 0);
if RecvLen<=0 then Break;
RecvData:= RecvData + RecvBuf;
if Pos('Set-Cookie: JSESSIONID=',RecvData )<>0 then
Cookie:= GetCookie(RecvData);
if pos('HTTP/1.1 302',RecvData)<>0 then Terminated:= True;
end;
end;
procedure SocketSend_33(S: TSocket; WSA: WSADATA; Phe
HostEnt; var Session, Cookie: String);
var
SendData, RecvData: String;
RecvBuf: array[0..1023] of Char;
RecvLen: Integer;
Terminated: Boolean;
begin
SendData:='HEAD /en/products_services/worldlingo_translator.html HTTP/1.1'+#13#10+
'Referer: http://www.worldlingo.com/en/products_services/worldlingo_translator.html'+#13#10+
'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'+#13#10+
'Host: www.worldlingo.com'+#13#10+
'Connection: Keep-Alive'+#13#10+
'Cookie: '+Cookie+#13#10#13#10;
Send(S,PChar(SendData)^,Length(SendData),0);
RecvLen:=1;
Terminated:= False;
Session:='';
While (RecvLen>0) and not (Terminated) do
begin
FillChar( RecvBuf, SizeOf( RecvBuf), 0);
RecvLen:=Recv( S, RecvBuf, 1024, 0);
if RecvLen<=0 then Break;
RecvData:= RecvData + RecvBuf;
if Pos('Set-Cookie: JSESSIONID=',RecvData )<>0 then
Cookie:= GetCookie(RecvData);
if (Trim(Session)<>'') then
Terminated:= True;
end;
end;
function GetHex(const aStr: string): string;
var
i: Integer;
begin
for i := 1 to Length(aStr) do
begin
Result := Result + '%' + IntToHex(Ord(aStr
),2);
end;
end;
procedure SocketSend_4(S: TSocket; WSA: WSADATA; PheHostEnt; var Session, Cookie: String);
var
SendData, RecvData: String;
RecvBuf: array[0..1023] of Char;
RecvLen: Integer;
Terminated: Boolean;
begin
SendData:='GET /S591.3/loader HTTP/1.1'+#13#10+
'Referer: http://www.worldlingo.com/en/products_services/worldlingo_translator.html'+#13#10+
'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'+#13#10+
'Host: www.worldlingo.com'+#13#10+
'Connection: Keep-Alive'+#13#10+
'Cookie: '+Cookie+#13#10#13#10;
Send(S,PChar(SendData)^,Length(SendData),0);
RecvLen:=1;
Terminated:= False;
Session:='';
While (RecvLen>0) and not (Terminated) do
begin
FillChar( RecvBuf, SizeOf( RecvBuf), 0);
RecvLen:=Recv( S, RecvBuf, 1024, 0);
if RecvLen<=0 then Break;
RecvData:= RecvData + RecvBuf;
Session:= GetSession(RecvData);
if Pos('Set-Cookie: JSESSIONID=',RecvData )<>0 then
Cookie:= GetCookie(RecvData);
if (Trim(Session)<>'') then
Terminated:= True;
end;
end;
procedure SocketSend_5(S: TSocket; WSA: WSADATA; PheHostEnt; var Session, Cookie: String;var Data:WideString);
var
SendData, RecvData, ss: String;
RecvBuf: array[0..1023] of Char;
RecvLen: Integer;
Terminated: Boolean;
Inicial, Final: Integer;
begin
SendData:='GET /'+Session+'/object?wl_url=http%3A//www.worldlingo.com/en/products_services/worldlingo_translator.html HTTP/1.1'+#13#10+
'Referer: http://www.worldlingo.com/en/products_services/worldlingo_translator.html'+#13#10+
'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'+#13#10+
'Host: www.worldlingo.com'+#13#10+
'Connection: Keep-Alive'+#13#10+
'Cookie: '+Cookie+#13#10#13#10;
Send(S,PChar(SendData)^,Length(SendData),0);
RecvLen:=1;
Terminated:= False;
While (RecvLen>0) and not (Terminated) do
begin
FillChar( RecvBuf, SizeOf( RecvBuf), 0);
RecvLen:=Recv( S, RecvBuf, 1024, 0);
if RecvLen<=0 then Break;
RecvData:= RecvData + RecvBuf;
{ if Pos('Type or paste here to translate text',RecvData)<>0 then
begin
SS:='翻译失败,请重试!';
Data:=SS;
Terminated:= True;
end else }
if pos('<select name=/"wl_srclang',RecvData)<>0 then
begin
if Pos('"wl_text_result/"',RecvData)<>0 then
begin
Inicial := Pos('"wl_text_result/"',RecvData)+82; //获得文本框开头文本
Final := Pos('//textarea>',RecvData)-1; //获得文本框结尾
ss := Copy(RecvData,Inicial,Final-Inicial);
Data:=UTF8Decode(SS);
end else
begin
Data:='翻译失败,请重试!';
end;
Terminated:= True;
end
end;
end;
procedure WorldLingoThread(hWnd: HWND); stdcall;
var
Sock: TSocket;
WSA: WSADATA;
Phe: PHostEnt;
Sai: sockaddr_in;
Session, Cookie :String;
TimeOut: Integer;
TrgData: WideString;
SrcData, SrcLang, TrgLang, Gl: String;
begin
EnableWindow(GetDlgItem(hWnd, ID_TRANS), False);
WSAStartup($101,WSA);
Sock:=Socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
Phe:=gethostbyname('www.worldlingo.com');
if Phe = nil then
begin
Messagebox(hWnd,PChar('解析域名失败!'),SysTitle,MB_ICONWARNING+MB_OK);
Exit;
end;
Sai.sin_family:=AF_INET;
Sai.sin_port:=htons(80);
Sai.sin_addr.S_addr:=PDWORD(PDWORD(Phe.h_addr)^)^;
SrcLang:=WInfo.SrcLang;
TrgLang:=WInfo.TrgLang;
Gl:=WInfo.Glossary;
SrcData:=GetHex(UTF8Encode(WInfo.Text));
Connect(Sock,Sai,SizeOf(Sai));
SocketSend_1(Sock, WSA, Phe, Session, Cookie); //获取WebSession和Cookie
CloseSocket(Sock);
Sock:=Socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
Connect(Sock,Sai,SizeOf(Sai));
SocketSend_2(Sock, WSA, Phe, Session, Cookie); //更新WebSession
CloseSocket(Sock);
Sock:=Socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
Connect(Sock,Sai,SizeOf(Sai));
SocketSend_3(Sock, WSA, Phe, Session, Cookie, SrcData, SrcLang, TrgLang, Gl); //提交翻译请求
CloseSocket(Sock);
Sock:=Socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
Connect(Sock,Sai,SizeOf(Sai));
SocketSend_4(Sock, WSA, Phe, Session, Cookie); //获取WebSession
CloseSocket(Sock);
Sock:=Socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
Connect(Sock,Sai,SizeOf(Sai));
SocketSend_5(Sock, WSA, Phe, Session, Cookie,TrgData); //获取翻译结果
CloseSocket(Sock);
WSACleanup();
SetDlgItemText(hWnd,ID_EDIT_T,nil);
SendMessage(GetDlgItem(hWnd, ID_EDIT_T), EM_SETSEL, -1, -1);
SendMessageW(GetDlgItem(hWnd, ID_EDIT_T), EM_REPLACESEL, WPARAM(True), integer((TrgData)));
EnableWindow(GetDlgItem(hWnd, ID_TRANS), True);
end;
procedure WLTranslate(hWnd: HWND);
var
ThreadIDWORD;
begin
CreateThread(nil,0,@WorldLingoThread,pointer(hWnd),0,ThreadID);
end;
end.