Controlling HHTP headers of Web Service

G

Gary Williams

My application uses a Web service that can return a very large data result.
In some cases, the data returned from the Web service could be 25MB or more.
The web service supports the ability to compress the data by setting the
Accept-Encoding HTTP header. How can I do this on my client program?
 
A

Andrew Hopper

Hi, Gary-

You should be able to accomplish this by overriding the
GetWebRequest/GetWebResponse methods of the proxy and creating a class that
derives from WebResponse that performs the decompression (if necessary) in
its GetResponseStream method. An example would be the following:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.WebRequest output = base.GetWebRequest (uri);
//
// Tell the WebService that we'll understand compressed
// results using the gzip compression algorithm
//
output.Headers["Accept-Encoding"] = "gzip";
return output;
}
protected override System.Net.WebResponse
GetWebResponse(System.Net.WebRequest request)
{
System.Net.WebResponse output = base.GetWebResponse(request);
//
// Safety check here - make sure the response object derives
// from HttpWebResponse
//
System.Net.HttpWebResponse potentiallyCompressedResponse = output as
System.Net.HttpWebResponse;
//
// If the response is an HttpWebResponse and it's compressed,
// return an instance of our custom WebResponse inheritor that
// performs decompression in GetResponseStream. If it's not
// an HttpWebResponse or the response is not compressed,
// simply return the original stream.
//
if(potentiallyCompressedResponse != null &&
potentiallyCompressedResponse.ContentEncoding == "gzip")
{
output = new UnzippedResponse(potentiallyCompressedResponse);
}
return output;
}

Where UnzippedResponse is a class that derives from System.Net.WebResponse
and stores the original (compressed) response in a member variable. When its
implementation of GetResponseStream is invoked, it then simply takes the
stream returned from the original WebResponse object's GetResponseStream,
unzips that stream, and then proceeds to return the decompressed stream as
the result.

Incidentally, you may want to stuff all of this into a class that inherits
from SoapHttpClientProtocol and have your proxies inherit from that class -
it'll make your life easier if you ever need to re-generate your proxies
using WSDL.exe or VS' /"Add Web Reference."

Hope this helps!
-Andy Hopper
http://www.dotnetified.com
 
G

Gary Williams

Thank you for a quick and great response!

Andrew Hopper said:
Hi, Gary-

You should be able to accomplish this by overriding the
GetWebRequest/GetWebResponse methods of the proxy and creating a class that
derives from WebResponse that performs the decompression (if necessary) in
its GetResponseStream method. An example would be the following:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.WebRequest output = base.GetWebRequest (uri);
//
// Tell the WebService that we'll understand compressed
// results using the gzip compression algorithm
//
output.Headers["Accept-Encoding"] = "gzip";
return output;
}
protected override System.Net.WebResponse
GetWebResponse(System.Net.WebRequest request)
{
System.Net.WebResponse output = base.GetWebResponse(request);
//
// Safety check here - make sure the response object derives
// from HttpWebResponse
//
System.Net.HttpWebResponse potentiallyCompressedResponse = output as
System.Net.HttpWebResponse;
//
// If the response is an HttpWebResponse and it's compressed,
// return an instance of our custom WebResponse inheritor that
// performs decompression in GetResponseStream. If it's not
// an HttpWebResponse or the response is not compressed,
// simply return the original stream.
//
if(potentiallyCompressedResponse != null &&
potentiallyCompressedResponse.ContentEncoding == "gzip")
{
output = new UnzippedResponse(potentiallyCompressedResponse);
}
return output;
}

Where UnzippedResponse is a class that derives from System.Net.WebResponse
and stores the original (compressed) response in a member variable. When its
implementation of GetResponseStream is invoked, it then simply takes the
stream returned from the original WebResponse object's GetResponseStream,
unzips that stream, and then proceeds to return the decompressed stream as
the result.

Incidentally, you may want to stuff all of this into a class that inherits
from SoapHttpClientProtocol and have your proxies inherit from that class -
it'll make your life easier if you ever need to re-generate your proxies
using WSDL.exe or VS' /"Add Web Reference."

Hope this helps!
-Andy Hopper
http://www.dotnetified.com

Gary Williams said:
My application uses a Web service that can return a very large data result.
In some cases, the data returned from the Web service could be 25MB or more.
The web service supports the ability to compress the data by setting the
Accept-Encoding HTTP header. How can I do this on my client program?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top