如何将响应数据写入浏览器?

时间:2015-10-26 07:46:23

标签: c# asp.net microsoft-translator

System.Net.WebClient wc = new System.Net.WebClient(); 

string webData = wc.DownloadString(absuri); 

从浏览器下载html后,我正在使用Microsoft Translator翻译文本。之后,我想在浏览器中显示它。

我需要在浏览器中替换webRequest响应数据,请在此帮助我。

string txtToTranslate = TextBox1.Text;
string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text="
             + System.Web.HttpUtility.UrlEncode(txtToTranslate)
             + "&from=en&to=es";
System.Net.WebRequest translationWebRequest = System.Net.WebRequest.Create(uri);
translationWebRequest.Headers.Add("Authorization", headerValue);

System.Net.WebResponse response = null;
response = translationWebRequest.GetResponse();
System.IO.Stream stream = response.GetResponseStream();

System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
System.IO.StreamReader translatedStream = new System.IO.StreamReader(stream, encode)

System.Xml.XmlDocument xTranslation = new System.Xml.XmlDocument()
xTranslation.LoadXml(translatedStream.ReadToEnd());
string resdata = xTranslation.InnerText;

WebBrowser webBrowser = new WebBrowser();
webBrowser.DocumentStream = stream;

1 个答案:

答案 0 :(得分:0)

您现在使用的WebBrowser控件是Windows窗体或WPF控件,而不是ASP.NET控件,它似乎是您要求的。

如果要将任何内容返回给浏览器,则必须设置当前HTTP上下文的响应:

HttpContext.Current.Response.Write(resdata /*your desired output to the client*/);