当前位置:编程学习 > C#/ASP.NET >>

【求助】微软翻译API的使用方法 或者其他的翻译API

--------------------编程问答--------------------  木有人回答 自己顶顶。。  --------------------编程问答--------------------  求助 ing --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.IO; 

namespace WindowsFormsApplication1
{
    public class FanYi
    {
        public static string TranslateMethod(string authToken,string ENString)
        {

           
            string from = "en";
            string to = "de";

            string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + ENString + "&from=" + from + "&to=" + to;

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest.Headers.Add("Authorization", authToken);
            WebResponse response = null;
            try
            {
                response = httpWebRequest.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
                   return  (string)dcs.ReadObject(stream);
                     

                }
                
            }
            catch
            {
                throw;
                
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                    
                }
            }
            
           
        }
        public static void ProcessWebException(WebException e)
        {
            Console.WriteLine("{0}", e.ToString());
            // Obtain detailed error information
            string strResponse = string.Empty;
            using (HttpWebResponse response = (HttpWebResponse)e.Response)
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
                    {
                        strResponse = sr.ReadToEnd();
                    }
                }
            }
            Console.WriteLine("Http status code={0}, error message={1}", e.Status, strResponse);
        }
    }
    [DataContract]
    public class AdmAccessToken
    {
        [DataMember]
        public string access_token { get; set; }
        [DataMember]
        public string token_type { get; set; }
        [DataMember]
        public string expires_in { get; set; }
        [DataMember]
        public string scope { get; set; }
    }

    public class AdmAuthentication
    {
        public static readonly string DatamarketAccessUri = "https://api.datamarket.azure.com/Bing/MicrosoftTranslator/v1/Translate";
        //https://api.datamarket.azure.com/Bing/MicrosoftTranslator/v1/Translate

        private string clientId;
        private string cientSecret;
        private string request;

        public AdmAuthentication(string clientId, string clientSecret)
        {
            this.clientId = clientId;
            this.cientSecret = clientSecret;
            //If clientid or client secret has special characters, encode before sending request
            this.request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", clientId,clientSecret);
        }

        public AdmAccessToken GetAccessToken()
        {
            return HttpPost(DatamarketAccessUri, this.request);
        }

        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;
            }
        }
    }
}
--------------------编程问答-------------------- 首先你需要注册一个 微软翻译的ID, 话说免费的好像有调用次数的限制, --------------------编程问答-------------------- 这个我已经注册了。密钥什么的我也都填写上了。  --------------------编程问答-------------------- 还有木有可以帮忙的亲们 --------------------编程问答-------------------- 试试这个注册,我经常会遇到的
1.项目的proterties/AssemblyInfo.cs注册上去“[assembly: AssemblyFileVersion("XXXX")]”
2.网站站点:Global.asax 添加“[assembly:XXX, Watch = true)]” --------------------编程问答-------------------- 这是我以前写过的一个,参考一下
http://blog.csdn.net/davinciyxw/article/details/7907975
参考后半部分。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,