我正在使用http api实现Bing翻译服务。我在实施中遇到了两个问题,经过数小时的研究后,我仍然无法解决。我希望那里的人可以给我一些指导。
问题:
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模板。我可能会以错误的方式进行这种体系结构,以及之前我没有尝试过实现此服务。非常感谢任何帮助!!!!
答案 0 :(得分:0)
我已经解决了下面解释的这些问题:
1)尝试使用各种数据将配置对象属性重新分配给自己是我自己的错。这会将我的配置文件设置为保留先前服务调用的数据。导致它解析相同的数据并将其作为新数据返回。
2)我的信念是,当传递格式错误的html脚本标签时会被翻译。另请注意,在您的Web服务调用配置中,您的内容类型参数应设置为“text / html”
希望这可以帮助有人在路上。
谢谢!