网上下载的在线翻译的源码,本来可以翻译的,现在不能了。看看是怎么回事?(附源码)(100分)

  • 主题发起人 主题发起人 zgp2000
  • 开始时间 开始时间
Z

zgp2000

Unregistered / Unconfirmed
GUEST, unregistred user!
网上下载的在线翻译的源码,本来可以翻译的,现在不能了。看看是怎么回事?(附源码如下)
procedure TForm1.BitBtn1Click(Sender: TObject);
var
s, s1: TStringStream;
gl,srl,trl,ss:String;
Inicial,Final: Integer;
begin
Case wl_glossary.ItemIndex of
0: gl := 'gl1';
1: gl := 'gl2';
2: gl := 'gl3';
3: gl := 'gl4';
4: gl := 'gl5';
5: gl := 'gl6';
6: gl := 'gl7';
7: gl := 'gl8';
8: gl := 'gl9';
9: gl := 'gl10';
10: gl := 'gl11';
11: gl := 'gl12';
12: gl := 'gl13';
13: gl := 'gl14';
14: gl := 'gl15';
15: gl := 'gl16';
16: gl := 'gl17';
17: gl := 'gl18';
18: gl := 'gl19';
19: gl := 'gl20';
20: gl := 'gl21';
end;
Case wl_srclang.ItemIndex of
0: srl := 'none';
1: srl := 'EN';
2: srl := 'ZH_CN';
3: srl := 'ZH_TW';
4: srl := 'NL';
5: srl := 'FR';
6: srl := 'DE';
7: srl := 'EL';
8: srl := 'IT';
9: srl := 'JA';
10: srl := 'KO';
11: srl := 'PT';
12: srl := 'RU';
13: srl := 'ES';
end;
Case wl_trglang.ItemIndex of
0: trl := 'none';
1: trl := 'EN';
2: trl := 'ZH_CN';
3: trl := 'ZH_TW';
4: trl := 'NL';
5: trl := 'FR';
6: trl := 'DE';
7: trl := 'EL';
8: trl := 'IT';
9: trl := 'JA';
10: trl := 'KO';
11: trl := 'PT';
12: trl := 'RU';
13: trl := 'ES';
end;
if (srl='none') then
begin
showmessage('请选择原文语言!');
exit;
end;
if (trl='none') then
begin
showmessage('请选择译文语言!');
exit;
end;
if (srl=trl) then
begin
showmessage('原文语言和译文语言不能相同!');
exit;
end;
s := TStringStream.Create('');
s1 := TStringStream.Create('');
s.WriteString('wl_url=&wl_text='+GetHex(AnsiToUtf8(memo1.Text))+'&wl_glossary='+gl+'&wl_srclang='+srl+'&wl_trglang='+trl+'&wl_ucp=1');
// s.WriteString('wl_url=&wl_text='+AnsiToUtf8(memo1.Text)+'&wl_glossary='+gl+'&wl_srclang='+srl+'&wl_trglang='+trl+'&wl_ucp=1');
http.Request.ContentType := 'application/x-www-form-urlencoded';
try
http.Post('http://www.worldlingo.com/wl/translate', s, s1)
except
http.Get(http.Response.Location, s1);
end;
// ss:=s1.ReadString;
Inicial := Pos('"wl_result"',s1.DataString)+125; //获得文本框开头文本
Final := Pos('</textarea>',s1.DataString); //获得文本框结尾
ss := Copy(s1.DataString,Inicial,Final-Inicial);
Memo2.Lines.Text := Utf8Toansi(ss);
end;


在这段代码报错为
Project Project1.exe raised exception class EIdHTTPProtocolException with message'HTTP/1.1 302 Found'.Process stopped.Use Step or Run to continue.

以前是可以翻译没有错误的,但现在报错。我将控件HandleRedirects设为true试
不报错了,但是不能翻译!请高手看看!
 
'http://www.worldlingo.com/wl/translate'
因为这个地址打不开了
或是换了一种提交方式了。
这个代码其实只是一个壳,实际的翻译是通过把内容提交到InterNet上翻译并取得结果的
 
http://www.worldlingo.com/wl/translate
地址可以打开的,本来就是通过网上的在线翻译系统实现翻译的.
前阵子一直用得好好的,不知道怎么回事现在翻译不了.但他们
网站现在还是可以在线翻译的!
 
错误码302
转向或者302重定向(302 redirect)指的是当浏览器要求一个网页的时候,主机所返回的状态码。302状态码的意义是暂时转向到另外一个网址。

有个值得注意的情况:浏览器错误地请求http://host/~user(缺少了后面的斜杠),有的服务器返回301,有的也会返回302

你可以看看现在网页的代码,看它提交数据的方式是否改变了?
 
我好像加了斜杠也没用!
我估计是提交数据方式变了,有办法解决吗?
 
浏览器数据的提交就两种方式,get 或者 post 没其他方式的
 
这个代码,我好久以前在盒子发布的,前些天http://www.worldlingo.com已更新,原来的的方法已经没有用。
第一步:GET方式 www.worldlingo.com/S591.3/loader ,取得COOKIE和SESSION
第二步:GET方式 www.worldlingo.com/'+刚才获得的Session+'/object?wl_url=http%3A//www.worldlingo.com/en/products_services/worldlingo_translator.html ,取得新的SESSION,如果有新的cookie就获取新的cookie
第三步 POST方式 www.worldlingo.com/'+刚才获得的Session+'/texttranslate ,这一步提交翻译内容,如果有新的cookie就获取新的cookie
第四步 GET方式 www.worldlingo.com/S591.3/loader ,取得新的SESSION,如果有新的cookie就获取新的cookie
第五步 GET方式 www.worldlingo.com/'+刚才获得的Session+'/object?wl_url=http%3A//www.worldlingo.com/en/products_services/worldlingo_translator.html ,这一步获得翻译结果
现在的worldlingo翻译一次要做5次处理
可以看一下
http://www.qzone.com.ru/article.asp?id=20
我也把我以前的写的翻译做了更新,另外因worldlingo的目前不稳定,加了Google翻译,可以测试下,抓下包看看,就大致清楚翻译的流程了
 
呵呵,.原创作者都来了啊。我研究一下。更新的有源码没?
发到我邮箱看看啊。zgp2000me@sina.com
 
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:PHostEnt; 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:PHostEnt; 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:PHostEnt; 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:PHostEnt; 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; Phe:PHostEnt; 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; Phe:PHostEnt; 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=/&quot;wl_srclang',RecvData)<>0 then
begin
if Pos('&quot;wl_text_result/&quot;',RecvData)<>0 then
begin
Inicial := Pos('&quot;wl_text_result/&quot;',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
ThreadID:DWORD;
begin
CreateThread(nil,0,@WorldLingoThread,pointer(hWnd),0,ThreadID);
end;

end.
 
后退
顶部