Problem using SharpZipLib to decompress response from web service

D

Dante

Hello,

When I try to decompress a response from a web service I'm getting the
error:
"hexadecimal value 0x1F, is an invalid character. Line 1, position 1."

The web server is an apache server. Content-Encoding is set to "gzip,
deflate" in the request header.
I can see that the response encoding is gzip.

Has anybody else ever had this problem?

Dan
 
J

Jon Skeet [C# MVP]

Dante said:
When I try to decompress a response from a web service I'm getting the
error:
"hexadecimal value 0x1F, is an invalid character. Line 1, position 1."

The web server is an apache server. Content-Encoding is set to "gzip,
deflate" in the request header.
I can see that the response encoding is gzip.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
D

Dan Telschow

I appreciate your reply. It would be impossible to create a small and
complete program to describe my problem. As a compromize I am including
a link to a page that describes exactly what I'm trying to accomplish.

http://www.businessanyplace.net/?p=wscompress

I am also including the code that I have in place in the proxy class as
well as the decompress class.

Thanks,
Dante

#############################
start code
#############################

###### in proxy class ########

protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest request = base.GetWebRequest(uri);
request.Headers.Add("Accept-Encoding", "gzip, deflate");
return request;
}

protected override WebResponse GetWebResponse(WebRequest request)
{
HttpWebResponseDecompressed response = new
HttpWebResponseDecompressed(request);
return response;
}


######### Decompress class ##########

public class HttpWebResponseDecompressed : System.Net.WebResponse
{
private HttpWebResponse response;
public HttpWebResponseDecompressed(WebRequest request)
{
response = (HttpWebResponse)request.GetResponse();
}
public override void Close()
{
response.Close();
}
public override Stream GetResponseStream()
{
Stream compressedStream = null;
if(response.ContentEncoding == "gzip")
{
compressedStream = new GZipInputStream(response.GetResponseStream());
}
else if(response.ContentEncoding == "deflate")
{
compressedStream = new ZipInputStream(response.GetResponseStream());
}
if(compressedStream != null)
{
// Decompress
MemoryStream decompressedStream = new MemoryStream();
int totalSize = 0;
int size = 2048;
byte[] writeData = new byte[2048];
while(true)
{
size = compressedStream.Read(writeData, 0, writeData.Length);
totalSize += size;
if(size > 0) {
decompressedStream.Write(writeData, 0, size);
} else {
break;
}
}
decompressedStream.Seek(0, SeekOrigin.Begin);
return decompressedStream;
}
else
return response.GetResponseStream();
}
public override long ContentLength
{
get { return response.ContentLength; }
}
public override string ContentType
{
get { return response.ContentType; }
}
public override System.Net.WebHeaderCollection Headers
{
get { return response.Headers; }
}
public override System.Uri ResponseUri
{
get { return response.ResponseUri; }
}
}

#############################
end code
#############################
 
J

Jon Skeet [C# MVP]

Dan Telschow said:
I appreciate your reply. It would be impossible to create a small and
complete program to describe my problem.

I don't see why. Can you not download the file (compressed) and then
write a small console application that tries to decompress it? Then all
you'd need to do is provide a link to the file so we could download
that separately to run it, along with what you expect the decompressed
version *should* look like.

The code you've got looks okay, but without being able to run it, it's
hard to know what's wrong.
 

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

Top