打印本文 打印本文 关闭窗口 关闭窗口
Using ASP.NET Session State in a Web Service
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3061  更新时间:2009/4/23 10:43:17  文章录入:mintao  责任编辑:mintao
Try
ret = proxy.IncrementSessionCounter()
Catch we As WebException
'''' We need an HttpWebResponse if we expect to
'''' check the HTTP status code.
If TypeOf we.Response Is HttpWebResponse Then
Dim HttpResponse As HttpWebResponse
HttpResponse = we.Response
If HttpResponse.StatusCode = HttpStatusCode.Found Then
'''' This is a "302 Found" response. Prompt the user
'''' to see if it is okay to redirect.
If MsgBox(String.Format(redirectPrompt, _
HttpResponse.Headers("Location")), _
MsgBoxStyle.YesNo) = _
MsgBoxResult.Yes Then
'''' It is okay. Set the new location and
'''' try again.
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

And now the ASP.NET session code works as expected. For the purposes of your own application, you can determine whether you need to prompt a user for redirecting their HTTP POST request or not. For instance, if you were calling this code from a service, you would not want to create a dialog that could not be seen.

This may seem to be a lot of work for getting ASP.NET sessions to work properly, but be aware that the code shown is useful for other things as well. For instance, any Web service on any platform that uses HTTP cookies would require the cookie container code. Similarly, there may be a host of other reasons why you might receive a "302 Found" response in reply to your request to a Web service. In a robust application, there will probably be a number of special scenarios that you will want to handle when invoking a Web service. Handling cookies and redirects are two such scenarios you may want to include in your Web service invocation code on a regular basis.

Conclusion

ASP.NET sessions can be very useful for maintaining state between Web method calls in your Web service. You do need to be aware that there may be issues that must be handled by client applications that you may not see when testing your Web service with the convenient browser interface. Fortunately, these issues are not particularly hard to handle.

上一页  [1] [2] [3] 

打印本文 打印本文 关闭窗口 关闭窗口