views:

325

answers:

2

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

A: 

Hey,

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...

Brian
Thanks Brian. The only problem with this is that I can not be guaranteed that the the request I get is the same one that is associated with that success method. It there are a lot of asyn requests going on at once, I could end up with a different request, or the request itself may even have finished by the time the success method is called. I really want a way to access the response headers for that particular call. I have the option to do this if I recode to use the jQuery library to make the call, but I really would like to use the .net library for consistency.
James
+2  A: 

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.

Andras Zoltan
Thank you Lord Zoltan. That is very helpful information!
James
glad to be of use ;)
Andras Zoltan