Note that there are some explanatory texts on larger screens.

plurals
  1. POPlease help me solve my W3C validation API timeout issue
    text
    copied!<p>I'm using the W3C validation service to check that the text I type into a TextBox is valid markup.</p> <p><a href="http://img15.imageshack.us/img15/5751/valid.gif" rel="nofollow noreferrer">Valid http://img15.imageshack.us/img15/5751/valid.gif</a></p> <p>It's almost working. But, under particular conditions my input results in an error and then endless timeout exceptions. I have to close an re-open the program to get it working again.</p> <p>Please glance over my code and help me to solve this issue.</p> <p>I've got a pretty simple WPF application with a TextBox and a StatusBar. The StatusBar updates as I type to let me know if my typed markup is or is not valid. So that I'm not hammering the service, validations occur only after one second or longer has elapsed with no keystrokes.</p> <p><a href="http://img9.imageshack.us/img9/3788/invalidr.gif" rel="nofollow noreferrer">Invalid http://img9.imageshack.us/img9/3788/invalidr.gif</a></p> <p>It StatusBar may show: "Validating...", "Valid", "Invalid", or--if there's been one--an exception's message.</p> <p><a href="http://img7.imageshack.us/img7/5842/validating.gif" rel="nofollow noreferrer">Validating http://img7.imageshack.us/img7/5842/validating.gif</a></p> <p>The following validates successfully:</p> <h2>XHTML Input</h2> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:lang="en"&gt; &lt;head&gt; &lt;title&gt;Test&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Test&lt;/h1&gt; &lt;p&gt;This is a test&lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>If I break my paragraph like <code>&lt;p&gt;This is a test&lt;/</code> then I get this exception while trying to process the response XML:</p> <blockquote> <p>Name cannot begin with the '"' character, hexadecimal value 0x22. Line 86, position 40.</p> </blockquote> <p><a href="http://img11.imageshack.us/img11/3066/namecannotbegin.gif" rel="nofollow noreferrer">XML Exception http://img11.imageshack.us/img11/3066/namecannotbegin.gif</a></p> <p>If validation fails like that twice in a row, then it seems I can't just fix my paragraph tags and continue on like normal. For some reason each subsequent validation fails with this exception:</p> <blockquote> <p>The operation has timed out</p> </blockquote> <p><a href="http://img21.imageshack.us/img21/7600/timedout.gif" rel="nofollow noreferrer">Timed Out http://img21.imageshack.us/img21/7600/timedout.gif</a></p> <p>This is very strange.</p> <p>I'm sorry to post my whole project, but I don't know where my problem is coming from. It might be my threading, web service communication, exception handling... I just can't seem to find it. Am I closing my StreamWriter, HttpWebRequest, and ResponseStreams correctly?</p> <h2>XAML</h2> <pre><code>&lt;Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="W3C Validation" Height="300" Width="300" Name="Window1"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBox Grid.Row="0" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" FontFamily="Consolas" TextChanged="TextBox_TextChanged" /&gt; &lt;StatusBar Grid.Row="1"&gt; &lt;StatusBarItem&gt; &lt;TextBlock x:Name="TextBlockResult" /&gt; &lt;/StatusBarItem&gt; &lt;/StatusBar&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <h2>Visual Basic</h2> <pre><code>Imports System.ComponentModel Imports &lt;xmlns:env="http://www.w3.org/2003/05/soap-envelope"&gt; Imports &lt;xmlns:m="http://www.w3.org/2005/10/markup-validator"&gt; Class Window1 Private WithEvents Worker As BackgroundWorker Private _WorkerArgument As String Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded InitializeWorker() End Sub Private Sub InitializeWorker() Worker = New BackgroundWorker Worker.WorkerSupportsCancellation = True AddHandler Worker.DoWork, AddressOf Worker_DoWork AddHandler Worker.RunWorkerCompleted, AddressOf Worker_RunWorkerCompleted End Sub Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) TryToWork(DirectCast(sender, TextBox).Text) End Sub Sub TryToWork(ByVal Argument As String) If _WorkerArgument IsNot Nothing Then _WorkerArgument = Argument Exit Sub End If If Not Worker.IsBusy Then TextBlockResult.Text = "Validating..." Worker.RunWorkerAsync(Argument) Exit Sub End If _WorkerArgument = Argument Worker.CancelAsync() Dim RetryTimer As New Windows.Threading.DispatcherTimer AddHandler RetryTimer.Tick, AddressOf RetryTicker RetryTimer.Interval = New TimeSpan(1) '1 tick' RetryTimer.Start() End Sub Sub RetryTicker(ByVal sender As Object, ByVal e As System.EventArgs) If Not Worker.IsBusy Then DirectCast(sender, Windows.Threading.DispatcherTimer).Stop() TextBlockResult.Text = "Validating..." Worker.RunWorkerAsync(_WorkerArgument) _WorkerArgument = Nothing End If End Sub Private Sub Worker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) 'wait for one second' Dim StartTime As DateTime = DateTime.Now() While Now.Subtract(StartTime) &lt; New TimeSpan(0, 0, 1) If DirectCast(sender, BackgroundWorker).CancellationPending Then e.Cancel = True Exit Sub End If System.Threading.Thread.Sleep(New TimeSpan(0, 0, 0, 0, 100)) 'tenth of a second' End While 'then validate' e.Result = Validate(DirectCast(e.Argument, String)) End Sub Private Function Validate(ByVal Text As String) As String Try Dim Url As String = "http://validator.w3.org/check" Dim Post As String = "&amp;fragment=" + Web.HttpUtility.UrlEncode(Text) + "&amp;output=soap12" Dim ResponseDocument As XDocument = XDocument.Load(New Xml.XmlTextReader(Communicate(Url, Post))) If ResponseDocument.Root.&lt;env:Body&gt;.&lt;m:markupvalidationresponse&gt;.&lt;m:validity&gt;.Value = "true" Then Return "Valid" Else Return "Invalid" End If Catch ex As Exception Return ex.Message End Try End Function Private Function Communicate(ByVal Url As String, ByVal Post As String) As System.IO.Stream Dim Writer As System.IO.StreamWriter = Nothing Dim Request As System.Net.HttpWebRequest = System.Net.WebRequest.Create(Url) Request.Method = "POST" Request.ContentLength = Post.Length Request.ContentType = "application/x-www-form-urlencoded" Request.Timeout = 2000 '2 seconds' Try Writer = New System.IO.StreamWriter(Request.GetRequestStream()) Writer.Write(Post) Catch Finally If Not Writer Is Nothing Then Writer.Close() End If End Try Return Request.GetResponse.GetResponseStream() End Function Private Sub Worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) If Not e.Cancelled Then TextBlockResult.Text = DirectCast(e.Result, String) End If End Sub End Class </code></pre> <p>Thanks for any help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload