在Web Service中使用ASP.net状态保持(6)

我对代码做了如下修改,捕获“302 Found”异常,提示用户同意重定向他们的请求,然后再次在新的位置调用我的Web方法。

' 同时使用基于Cookie和Cookie的Session
Private Cookies As System.Net.CookieContainer
Private webServiceUrl as Uri

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim proxy As New localhost.Service1()
Dim ret As Integer
' 设置proxy类的Cookie容器
If Cookies Is Nothing Then
Cookies = New System.Net.CookieContainer()
End If
proxy.CookieContainer = Cookies
' 设置proxy类的URL
If webServiceUrl Is Nothing Then
webServiceUrl = New Uri(proxy.Url)
Else
proxy.Url = webServiceUrl.AbsoluteUri
End If
Try
ret = proxy.IncrementSessionCounter()
Catch we As WebException
' 如果我们想检测HTTP状态码
' 那么就需要一个HttpWebResponse类的实例
If TypeOf we.Response Is HttpWebResponse Then
Dim HttpResponse As HttpWebResponse
HttpResponse = we.Response
If HttpResponse.StatusCode = HttpStatusCode.Found Then
' 这是一个“302 Found”响应,提示用户是否进行重定向
If MsgBox(String.Format(redirectPrompt, _
HttpResponse.Headers("Location")), _
MsgBoxStyle.YesNo) = _
MsgBoxResult.Yes Then
' 用户选择Yes,重新尝试新的URL
webServiceUrl = New Uri(webServiceUrl, _
HttpResponse.Headers("Location"))
Button1_Click(sender, e)
Return
End If
End If
End If
Throw we
End Try
Label1.Text = "Result: " & CStr(ret)
End Sub

Published At
Categories with 服务器类
Tagged with
comments powered by Disqus