I am implementing Bing Translation services using the http api. I have run into two issues in implementation that after hours of research I still have not been able to resolve. I hope that someone out there can provide me with a little guidance.
The issues:
1) The initial page sent to the webservice is translated almost perfectly. However, after the first page is rendered any following calls to the server receive the response from the first call. Meaning html string is sent to the service correctly, but the service returns the original response to the original request instead of the expected new response to the new request.(Could the service be caching my request?)
2) Javascript is translated as well as inner html:-/ see below example:
Javascript(Translated :-/)
$(función () {}
elformulario var document.forms['ctl01 ='];
Si (typeof(Sys) == 'undefined') tira Error nuevo ('ASP NET Ajax client-side fram...
My Translation class(Web service call)
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;
}
}
}
My Page render call:
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();
}
}
}
}
I am using the default webforms template in vs2012. I may be going about this achitecture in the wrong way as well as I have not tried to implement this service before. Any help is greatly appreciated!!!!