有道翻译测试demo

详细请搜索devstore

 

 

测试DEMO

namespace YouDaoAPI

{

    public partial class frmYouDao : Form

    {

        private static readonly string url = @"http://fanyi.youdao.com/openapi.do?keyfrom=sunuptech&key=1650695939&type=data&doctype=json&version=1.1&q=";

        public frmYouDao()

        {

            InitializeComponent();

        }

 

        private void btnTrans_Click(object sender, EventArgs e)

        {

            string source = this.txtSource.Text;

            if (!string.IsNullOrEmpty(source))

            {

                string urlRequst = url + HttpUtility.UrlEncode(source);

                WebRequest request = WebRequest.Create(urlRequst);

                WebResponse response = request.GetResponse();

                string resJson = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();

                int textIndex = resJson.IndexOf("translation") + 15;

                int textLen = resJson.IndexOf("\"]", textIndex) - textIndex;

                txtObject.Text= resJson.Substring(textIndex, textLen);

 

 

                

               

            }

            else

            {

                MessageBox.Show("原文不可以为空");

            }

        }

    }

}


你可能感兴趣的:(有道翻译测试demo)