namespace YouDaoAPIweb
{json
public partial class frmYouDao : Formapi
{測試
private static readonly string url = @"http://fanyi.youdao.com/openapi.do?keyfrom=sunuptech&key=1650695939&type=data&doctype=json&version=1.1&q=";this
public frmYouDao()url
{spa
InitializeComponent();code
}orm
private void btnTrans_Click(object sender, EventArgs e)get
{
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("原文不能夠爲空");
}
}
}
}