欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.NET调用百度翻译的方法

程序员文章站 2024-02-21 21:01:52
本文实例讲述了asp.net调用百度翻译的方法。分享给大家供大家参考。具体分析如下: asp.net调用百度翻译,如下图所示: html代码如下: 复制代码 代码...

本文实例讲述了asp.net调用百度翻译的方法。分享给大家供大家参考。具体分析如下:

asp.net调用百度翻译,如下图所示:

Asp.NET调用百度翻译的方法

html代码如下:

复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codefile="baidu.aspx.cs" inherits="fanyi_baidu" %> 
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>oa翻译</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <asp:textbox id="sourceword" runat="server" columns="50" rows="15" style="width:100%;" 
            textmode="multiline"></asp:textbox> 
        <br /> 
        源语言:<asp:dropdownlist id="ddlfrom" runat="server"> 
            <asp:listitem value="auto">自动检测</asp:listitem> 
            <asp:listitem value="zh">中文</asp:listitem> 
            <asp:listitem value="en">英文</asp:listitem> 
            <asp:listitem value="jp">日文</asp:listitem> 
        </asp:dropdownlist> 
        目标语言:<asp:dropdownlist id="ddlto" runat="server"> 
            <asp:listitem value="auto">自动检测</asp:listitem> 
            <asp:listitem value="zh">中文</asp:listitem> 
            <asp:listitem value="en">英文</asp:listitem> 
            <asp:listitem value="jp">日文</asp:listitem> 
        </asp:dropdownlist> 
        <asp:button id="translate" 
            runat="server" text="翻译" onclick="translate_click" /> 
        <br /> 
        <asp:textbox id="resulttext" runat="server" textmode="multiline" rows="15" columns="50" style="width:100%;"></asp:textbox> 
    </div> 
    </form> 
</body> 
</html>

c#代码如下:

复制代码 代码如下:
using system; 
using system.collections.generic; 
using system.io; 
using system.net; 
using system.runtime.serialization; 
using system.runtime.serialization.json; 
using system.text; 
using system.web; 
 
public partial class fanyi_baidu : system.web.ui.page 

    string url = @"http://openapi.baidu.com/public/2.0/bmt/translate"; 
    string requestdetail = "client_id=申请的id"; 
    protected void page_load(object sender, eventargs e) 
    { 
 
    } 
    [datacontract] 
    public class admaccesstoken 
    { 
        [datamember] 
        public string from { get; set; } 
        [datamember] 
        public string to { get; set; } 
        [datamember] 
        public string error_code { get; set; } 
        [datamember] 
        public string error_msg { get; set; } 
        [datamember] 
        public string query { get; set; } 
        [datamember] 
        public list<tokenresult> trans_result { get; set; } 
    } 
 
    [datacontract] 
    public class tokenresult 
    { 
        [datamember] 
        public string src { get; set; } 
        [datamember] 
        public string dst { get; set; } 
    } 
 
    //百度翻译返回数据结构 
    //{ 
    //"from": "en", 
    //"to": "zh", 
    //"trans_result": [ 
    //    { 
    //        "src": "today", 
    //        "dst": "今天" 
    //    }, 
    //    { 
    //        "src": "tomorrow", 
    //        "dst": "明天" 
    //    } 
    //], 
    //"error_code": "52001", 
    //"error_msg": "timeout", 
    //"query": "he's" 
    //} 
 
    /// <summary> 
    /// 采用post方式提交数据 
    /// </summary> 
    /// <param name="datamarketaccessuri">目标网址</param> 
    /// <param name="requestdetails">参数字符串</param> 
    /// <returns></returns> 
    private admaccesstoken httppost(string datamarketaccessuri, string requestdetails) 
    { 
        //prepare oauth request  
        webrequest webrequest = webrequest.create(datamarketaccessuri); 
        webrequest.contenttype = "application/x-www-form-urlencoded"; 
        webrequest.method = "post"; 
        byte[] bytes = encoding.ascii.getbytes(requestdetails); 
        webrequest.contentlength = bytes.length; 
        using (stream outputstream = webrequest.getrequeststream()) 
        { 
            outputstream.write(bytes, 0, bytes.length); 
        } 
        using (webresponse webresponse = webrequest.getresponse()) 
        { 
            datacontractjsonserializer serializer = new datacontractjsonserializer(typeof(admaccesstoken)); 
            //get deserialized object from json stream 
            admaccesstoken token = (admaccesstoken)serializer.readobject(webresponse.getresponsestream()); 
            return token; 
        } 
    } 
 
    protected void translate_click(object sender, eventargs e) 
    { 
        resulttext.text = ""; 
        if (sourceword.text.trim() != "") 
        { 
            string requeststr = requestdetail + "&from=" + ddlfrom.selectedvalue 
                + "&to=" + ddlto.selectedvalue 
                + "&q=" + httputility.urlencode(sourceword.text); 
 
            admaccesstoken token = httppost(url, requeststr); 
            if (token.error_code != null) 
            { 
                resulttext.text = token.error_msg; 
            } 
            else 
            { 
                int n = token.trans_result.count; 
                for (int i = 0; i < n; i++) 
                { 
                    resulttext.text += token.trans_result[i].dst + (i < n-1 ? "\n" : ""); 
                } 
            } 
        } 
        else 
        { 
            resulttext.text = "请输入要翻译的内容"; 
        } 
    } 
}

希望本文所述对大家的c#程序设计有所帮助。