WinMo> ASMX WebException - 如何获取详细信息?

发布于 2024-09-05 00:18:29 字数 2672 浏览 1 评论 0原文

好的,我们已经有了一个应用程序,它由一个托管多个 ASMX Web 服务的网站和一个在 WinMo 6.1 上运行的调用 Web 服务的手持应用程序组成。

在办公室里一直在发展,一切都很完美。

现在我们已经在客户端安装了它,我们设置了所有服务器并安装了手持设备。然而,手持设备现在无法再连接到网络服务。

我在错误处理程序中添加了额外的代码,专门捕获 WebException 异常,并在日志记录中以不同的方式处理它们,以输出额外的信息(.Status 和 .Response)。

我正在获取状态,该状态返回 [7]ProtocolError。但是,当我尝试读出 ResponseStream(使用 WebException.Response.GetResponseStream)时,它返回一个 CanRead 设置为 False 的流,因此我无法获得有关问题所在的更多详细信息。

所以我想我有两件事需要帮助...

a) 尝试从 WebException 中获取更多信息有什么帮助吗?

b) 什么可能导致 ProtocolError 异常?

由于客户端在现场有一个成熟的、支持登录的代理设置,事情变得更加复杂。这最初阻止了对该网站的所有访问,即使是通过浏览器也是如此。因此,我们在 WinMo 设备上的 HTTP 网络连接中输入登录详细信息。现在它可以正常访问网站了。

事实上,我什至可以很好地启动 Web 服务并从浏览器 (PocketIE) 调用方法。所以我知道设备能够通过 HTTP 正常查看网络服务。但当尝试从 .NET 应用程序调用它们时,它会抛出 ProtocolError [7]

这是我的代码,它正在记录异常,但无法从 WebException 中读出 Response

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

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟织青萝梦 2024-09-12 00:18:29

我无法从 WebException 类中获取更多信息。由于某种原因,GetResponseStream 总是返回不可读的流。

通过编写一个单独的小程序,我能够将问题范围缩小到代理身份验证,该程序只是尝试请求网页并读取响应并将其放入文本框中。

该程序返回了需要代理身份验证的响应。我觉得这真的很奇怪,因为我将代理信息放入设备上的网络连接设置中。我认为这适用于该连接上发出的所有网络流量;显然它仅适用于通过浏览器的流量,而不适用于应用程序请求。奇怪的。

I was not able to get any more information out of the WebException class. For some reason that GetResponseStream always returns an unreadable stream.

I was able to narrow the problem down to proxy authentication though by writing a separate little program which simply tried to request a web page and read the response and put it into a textbox.

This program returned a response of proxy authentication required. I find this really weird though because I put the proxy information into the network connection settings on my device. I would've though this would apply to all network traffic going out on that connection; apparently it only applies to traffic through the browser, not application requests. Odd.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文