2012-09-13 86 views
0

我想顯示一個HTML字符串,這個應用程序從服務器接收到Web瀏覽器。出於某種原因,我失敗了。我能夠將HTML字符串顯示在消息框中,但我無法將其顯示到Web瀏覽器中。我對c#很陌生,所以我對不必要的代碼或錯誤表示歉意。也事先感謝。顯示一個HTML字符串到網絡瀏覽器

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(); 
     }); 
    } 
} 
} 
+0

webBrowser1.DocumentText = responseString; ?或者在winForm,WPF表單中缺少webBrowser控件? – tschmit007

回答

0

您需要將webBrowser添加到您的用戶界面。如果您的UI有一個名爲ContentPanel的網格,請執行ContentPanel.Children.Add(webBrowser);,您也可以直接將webBrowser添加到通過代碼創建它的UI中。要顯示來自字符串的HTML內容,請使用webBrowser.NAvigateToString(responseString);