同系列文章:
源代码下载
源代码
MandarinThriftClientForm.cs
/* ---------------------------------------------------------- * 文件名称:MandarinThriftClientForm.cs * * 作者:秦建辉 * * 微信:splashcn * * 博客:http://www.firstsolver.com/wordpress/ * * 开发环境: * Visual Studio 2017 * .NET Framework 4.5.2 * Apache Thrift v0.10.0 * * 版本历史: * V1.0 2017年07月16日 * 基于Thrift框架实现汉字拼音查询服务-客户端 * * 参考资料: * http://thrift.apache.org ------------------------------------------------------------ */ using Com.FirstSolver.Mandarin.Thrift; using System; using System.Collections.Generic; using System.Windows.Forms; using Thrift.Protocol; using Thrift.Transport; namespace Splash { public partial class MandarinThriftClientForm : Form { public MandarinThriftClientForm() { InitializeComponent(); } private void buttonQuery_Click(object sender, EventArgs e) { string Question = textBoxQuestion.Text; if (string.IsNullOrEmpty(Question)) return; try { using (TTransport transport = new TSocket(textBoxServerIP.Text, int.Parse(textBoxServerPort.Text))) { using (TProtocol protocol = new TBinaryProtocol(transport)) { using (MandarinService.Client client = new MandarinService.Client(protocol)) { transport.Open(); try { if (checkBoxHomophone.Checked) { // 查询同音字 textBoxAnswer.Clear(); Dictionary<string, string> Source = client.QueryHomophone(Question, GBCharset.GB2312); if (Source != null) { foreach (KeyValuePair<string, string> Item in Source) { textBoxAnswer.AppendText(Item.Key + "\r\n"); textBoxAnswer.AppendText(Item.Value + "\r\n"); } } } else { // 查询拼音 textBoxAnswer.Text = client.QueryPinyin(Question, checkBoxDigitTone.Checked, checkBoxUppercase.Checked); } } finally { transport.Close(); } } } } } catch (Exception exception) { textBoxAnswer.Text = exception.Message; } } } }