I am trying to display a html string that this app receives from a server into a web browser. For some reason I am failing miserably. I am able to display the html string into a message box but I cant display it into a web browser. I am very new to c# so i apologize for unnecessary code or mistakes. Also thanks in advance.
namespace Test2 { public partial class MainPage : PhoneApplicationPage { private static readonly Dispatcher Dispatcher; // Constructor public MainPage() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { // Create a new HttpWebRequest object. HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1/examples/servlets/servlet/ReverseServlet"); request.ContentType = "application/x-www-form-urlencoded"; // Set the Method property to 'POST' to post data to the URI. request.Method = "POST"; // start the asynchronous operation request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request); } private static void GetRequestStreamCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; // End the operation Stream postStream = request.EndGetRequestStream(asynchronousResult); System.Diagnostics.Debug.WriteLine("Please enter the input data to be posted:");//Test code string postData = "string is this = Testing out this app for windows 7 phone";// Test string added // Convert the string into a byte array. byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Write to the request stream. postStream.Write(byteArray, 0, postData.Length); postStream.Close(); // Start the asynchronous operation to get the response request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); } private static void GetResponseCallback(IAsyncResult asynchronousResult) { Deployment.Current.Dispatcher.BeginInvoke( () => { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; // End the operation HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); string responseString = streamRead.ReadToEnd(); WebBrowser webBrowser1 = new WebBrowser(); MessageBox.Show(responseString);//Message box displays html webBrowser1.Visibility = Visibility.Visible; webBrowser1.NavigateToString(responseString);//Does NOT display html string System.Diagnostics.Debug.WriteLine(responseString+" FIXED IT"); // Close the stream object streamResponse.Close(); streamRead.Close(); // Release the HttpWebResponse response.Close(); //allDone.Set(); }); } } }
Answer: 1
You need to add the webBrowser to your UI. If your UI has a grid called ContentPanel, do ContentPanel.Children.Add(webBrowser);
also you can add the webBrowser directly to the UI insted of creating it via the code. To display HTML content from string use webBrowser.NAvigateToString(responseString);
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog