1

我正在使用 http api 实现必应翻译服务。我在实施过程中遇到了两个问题,经过数小时的研究,我仍然无法解决。我希望那里有人可以为我提供一些指导。

问题:

1) 发送到 web 服务的初始页面被翻译得几乎完美。但是,在呈现第一页之后,对服务器的任何后续调用都会收到来自第一个调用的响应。意味着 html 字符串已正确发送到服务,但服务返回对原始请求的原始响应,而不是对新请求的预期新响应。(服务可以缓存我的请求吗?)

2) Javascript 被翻译以及内部 html:-/ 见下面的例子:

Javascript(翻译:-/)

$(función () {}

elformulario var document.forms['ctl01 ='];

Si (typeof(Sys) == 'undefined') tira Error nuevo ('ASP NET Ajax client-side fram...  

我的翻译课(网络服务调用)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Web;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Json;
using Services.Config;
using Services.Models;


namespace Services
{
    public class TranslationService
    {
        private readonly RegisterServicesSection _config;
        private readonly Service _btsrv;

        public TranslationService()
        {
            _config = (RegisterServicesSection)ConfigurationManager.GetSection("registerServicesSection");
            if (_config != null)
            {
                foreach (var item in _config.Services)
                {
                    _btsrv = (Service)item;
                    if (_btsrv.Title == "BingTranslator")
                        break;
                }
            }
        }

        public string GetAuthToken()
        {
            string response;
            _btsrv.AuthRequest = string.Format(_btsrv.AuthRequest, HttpUtility.UrlEncode(_btsrv.ClientId), HttpUtility.UrlEncode(_btsrv.ClientSecret));
            try
            {
                //Create Web Client
                var web = new WebClient();
                //Set Web Client Encoding
                web.Encoding = Encoding.UTF8;
                //Get response from Web Client request
                response = web.UploadString(_btsrv.AuthUrl, _btsrv.AuthRequest);
            }
            catch (Exception ex)
            {
                return null;
            }

            //Create Javasrcipt Object Serializer
            var ser = new JavaScriptSerializer();
            //Deserialize response into token poco object
            var token = ser.Deserialize<BingAuthToken>(response);
            if (token != null)
                return token.access_token;
            else
                return null;


        }
        public string Post(string text, string fromCulture, string toCulture)
        {

            string response;
            try
            {
                var accessToken = GetAuthToken();
                if (accessToken == null)
                    return null;

                //Create Web Client
                var web = new WebClient();
                //Set Web Client Authorization Headers
                web.Headers.Add("Authorization", "Bearer " + accessToken);
                //Get UrlEncoded Post Parameters from Web.config
                _btsrv.Request = String.Format(_btsrv.Request, HttpUtility.UrlEncode(text), fromCulture, toCulture, HttpUtility.UrlEncode(_btsrv.ContentType));
                //Set Web Client Encoding
                web.Encoding = Encoding.UTF8;
                //Get response from Web Client Request
                response = web.DownloadString(_btsrv.Url + _btsrv.Request);

            }
            catch (Exception ex)
            {
                return null;
            }
            //Create xml document to load Web Client response
            var doc = new XmlDocument();
            //Load Web Client response
            doc.LoadXml(response);
            //Return Web Client response string
            return doc.DocumentElement.InnerText;            
        }
    }
}

我的页面渲染调用:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using Services;

namespace TestTranslation
{
    public class CustomPage: Page
    {
        private TranslationService _tsrv = new TranslationService();

        protected override void Render(HtmlTextWriter writer)
        {
            using (StringWriter output = new StringWriter())
            {
                base.Render(new HtmlTextWriter(output));
                //This is the rendered HTML of your page.
                string outputAsString = output.ToString();
                outputAsString = _tsrv.Post(outputAsString, "en", "es");
                writer.Write(outputAsString);
                output.Close();
                writer.Close();
                writer.Dispose();
            }            
        }
    }
}

我在 vs2012 中使用默认的 webforms 模板。我可能会以错误的方式来处理这个架构,而且我之前没有尝试过实现这个服务。任何帮助是极大的赞赏!!!!

4

1 回答 1

0

我已经解决了以下解释的这些问题:

1)我自己的错误是试图用不同的数据将配置对象属性重新分配给自己。这将我的配置文件设置为保留先前服务调用的数据。导致它解析相同的数据并将其作为新数据返回。

2)我的信念是,当传递格式错误的 html 脚本标签时会被翻译。另请注意,在您的网络服务调用配置中,您的内容类型参数应设置为“text/html”

希望这对未来的人有所帮助。

谢谢!

于 2013-09-25T15:43:44.700 回答