利用网上免费的WebService实现一个在线汉、英、日互译程序
www.webservicex.net 提供好多种语言的对译,这里只实现汉、英、日互译。
它提供汉、英互译;英、日互译。却没提供汉、日互译,所以要间接翻译。
具体步骤看:http://blog.csdn.net/chjk1/archive/2008/03/30/2231151.aspx
1、“网站”--“添加web引用”--在弹出窗口的‘URL’栏输入该WebService的wsdl链接:
http://www.webservicex.net/TranslateService.asmx?WSDL
2、【我保存如下重要信息】:
web引用名 :net.webservicex.www
方法:
Translate ( LanguageMode As Language , Text As string ) As string
Convert text from one lanaguage to another language .Supported languages are English to
Chinese,English to French,English to German,English to Italian,English to Japanese,English to
Korean,English to Portuguese,English to Spanish,Chinese to English,French to English,French to
German,German to English,German to French,Italian to English,Japanese to English,Korean to
English,Portuguese to English,Russian to English,Spanish to English.
2、点“添加引用”。打开wsdl文件,找出webservice的名字TranslateService。
3、现在已经有web引用名、方法、webservice名这三个要素,我就可以开始做事了,完整代码:
<%@ page language="C#" %>
<%@ import namespace="System.IO" %>
<%@ import namespace="net.webservicex.www" %>
<script runat="server">
TranslateService objservice = new TranslateService();
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedValue;
switch (Convert.ToInt32(DropDownList1.SelectedValue))
{
case 1:
//汉转英
Label1.Text = "";
Label1.Text = objservice.Translate(Language.ChineseTOEnglish, TextBox1.Text);
break;
case 2:
//英转汉
Label1.Text = "";
Label1.Text = objservice.Translate(Language.EnglishTOChinese, TextBox1.Text);
break;
case 3:
//汉转日
string str1;
Label1.Text = "";
str1 = objservice.Translate(Language.ChineseTOEnglish, TextBox1.Text);
Label1.Text = objservice.Translate(Language.EnglishTOJapanese, str1);
break;
case 4:
//日转汉
string str2;
Label1.Text = "";
str2 = objservice.Translate(Language.JapaneseTOEnglish, TextBox1.Text);
Label1.Text = objservice.Translate(Language.EnglishTOChinese, str2);
break;
case 5:
//英转日
Label1.Text = "";
Label1.Text = objservice.Translate(Language.EnglishTOJapanese, TextBox1.Text);
break;
case 6:
//日转英
Label1.Text = "";
Label1.Text = objservice.Translate(Language.JapaneseTOEnglish, TextBox1.Text);
break;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Height="64px" TextMode="MultiLine" Width="232px"></asp:TextBox>
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="120px">
<asp:ListItem Value="0">请选择...</asp:ListItem>
<asp:ListItem Value="1">汉转英</asp:ListItem>
<asp:ListItem Value="2">英转汉</asp:ListItem>
<asp:ListItem Value="3">汉转日</asp:ListItem>
<asp:ListItem Value="4">日转汉</asp:ListItem>
<asp:ListItem Value="5">英转日</asp:ListItem>
<asp:ListItem Value="6">日转英</asp:ListItem>
</asp:DropDownList><br />
<br />
<asp:Label ID="Label1" runat="server" Height="240px" Width="360px"></asp:Label></div>
</form>
</body>
</html>