Tutorial :Access Response Headers from ASP.Net PageMethod Call



Question:

When using ASP.Net Ajax to call PageMethods, how can I access the Http response headers from the "success" method?

For example:

PageMethods.DoSomething(     function(result){successMethod(result)},     function(error){errorMethod(error)}  );    function successMethod(result){      //------how can I access the Http response headers from here? ------  }  

Thanks for any help


Solution:1

In your example, PageMethods.DoSomething should have a return value equal to WebRequest if it's an asp.net web service proxy. This is provided so that you can manipulate the request after you've initiated it (i.e. cancel it etc).

With this class you have an add_completed method which you can use to add a handler for when the web request completes. The signature for the callback is function OnWebRequestCompleted(executor, eventArgs), and the executor parameter in this enables you to get hold of extra response information. For example, you can get hold of the response headers with executor.getAllResponseHeaders(); which should be a map (named collection) of header names and values.

So if you add a handler to the web request's completed event immediately after making the service method call, it should work (there's no web service in the world that can respond faster than two consecutive lines of code!).

The previous hyperlink to WebRequest contains a full example of how wire this up. Notice, however, that this code uses the WebRequest directly.

Asp.Net Ajax Web Service proxy classes use the WebServiceProxy class, and each proxy method ultimately call its invoke method, which returns the WebRequest instance.


Solution:2

A web request has a headers collection

http://msdn.microsoft.com/en-us/library/bb383774.aspx

The webrequestmanager is a static object that you may be able to extract this information from:

http://msdn.microsoft.com/en-us/library/bb397435.aspx

Hopefully, between the two links, it makes sense :-;

I'm not saying recode to use this necessarily, but page methods is a wrapper and as such I think it would access information from a web request, which can be affected from the WebRequestManager...


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »