WinMo > ASMX WebException - how to get details?

Posted by eidylon on Stack Overflow See other posts from Stack Overflow or by eidylon
Published on 2010-06-10T20:25:01Z Indexed on 2010/06/13 21:42 UTC
Read the original article Hit count: 369

Okay, we've got an application which consists of a website hosting several ASMX webservices, and a handheld application running on WinMo 6.1 which calls the webservices.

Been developing in the office, everything works perfect.

Now we've gone to install it at the client's and we got all the servers set up and the handhelds installed. However the handhelds are now no longer able to connect to the webservice.

I added in extra code in my error handler to specifically trap WebException exceptions and handle them differently in the logging to put out extra information (.Status and .Response).

I am getting out the status, which is returning a [7], or ProtocolError. However when I try to read out the ResponseStream (using WebException.Response.GetResponseStream), it is returning a stream with CanRead set to False, and I thus am unable to get any further details of what is going wrong.

So I guess there are two things I am asking for help with...

a) Any help with trying to get more information out of the WebException?

b) What could be causing a ProtocolError exception?

Things get extra complicated by the fact that the client has a full-blown log-in-enabled proxy setup going on-site. This was stopping all access to the website initially, even from a browser. So we entered in the login details in the network connection for HTTP on the WinMo device. Now it can get to websites fine.

In fact, I can even pull up the webservice fine and call the methods from the browser (PocketIE). So I know the device is able to see the webservices okay via HTTP. But when trying to call them from the .NET app, it throws ProtocolError [7].

Here is my code which is logging the exception and failing to read out the Response from the WebException.

Public Sub LogEx(ByVal ex As Exception)
    Try
        Dim fn As String = Path.Combine(ini.CorePath, "error.log")
        Dim t = File.AppendText(fn)
        t.AutoFlush = True
        t.WriteLine(<s>===== <%= Format(GetDateTime(), "MM/dd/yyyy HH:mm:ss") %> =====<%= vbCrLf %><%= ex.Message %></s>.Value)
        t.WriteLine()
        t.WriteLine(ex.ToString)
        t.WriteLine()
        If TypeOf ex Is WebException Then
            With CType(ex, WebException)
                t.WriteLine("STATUS: " & .Status.ToString & " (" & Val(.Status) & ")")
                t.WriteLine("RESPONSE:" & vbCrLf & StreamToString(.Response.GetResponseStream()))
            End With
        End If
        t.WriteLine("=".Repeat(50))
        t.WriteLine()
        t.Close()
    Catch ix As Exception : Alert(ix) : End Try
End Sub

Private Function StreamToString(ByVal s As IO.Stream) As String
    If s Is Nothing Then Return "No response found."

    // THIS IS THE CASE BEING EXECUTED
    If Not s.CanRead Then Return "Unreadable response found."

    Dim rv As String = String.Empty, bytes As Long, buffer(4096) As Byte
    Using mem As New MemoryStream()
        Do While True
            bytes = s.Read(buffer, 0, buffer.Length)
            mem.Write(buffer, 0, bytes)

            If bytes = 0 Then Exit Do
        Loop

        mem.Position = 0
        ReDim buffer(mem.Length)
        mem.Read(buffer, 0, mem.Length)
        mem.Seek(0, SeekOrigin.Begin)
        rv = New StreamReader(mem).ReadToEnd()

        mem.Close()
    End Using

    Return rv.NullOf("Empty response found.")
End Function

Thanks in advance!

© Stack Overflow or respective owner

Related posts about web-services

Related posts about .net-3.5