S
stroumf
Hi,
Simple question, I want to receive compressed data from a server using
AJAX.
At the client I make an xmlHttpRequest. Next I want to set the
accept-header to Gzip, but I keep getting errors on that under opera.
At server-side I first check the accept-header, if it supports gzip I
make a ZIP-stream and set the content-encoding to gzip. Normaly I would
think the browser itself can handle it and give the uncompressed
response.
Clientcode (javascript):
xmlHttp = =new XMLHttpRequest();
xmlHttp.onreadystatechange = stateChange;
//don't work
xmlHttp.setRequestHeader("Accept-Encoding", "gzip");
xmlHttp.open("GET", "servlet", false);
xmlHttp.send(null);
function stateChange() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
var response = xmlHttp.responseText;
alert(response);
var uncompressedResponse = response;
document.getElementById("code").innerHTML =
"<p>"+uncompressedResponse+"</p>";
}
}
Servercode (servlet):
public void doGet(HttpServletRequest request, HttpServletResponse
response) {
String text = "sometext";
response.setContentType("text/html");
OutputStream out = null;
String encoding = request.getHeader("Accept-Encoding");
if (encoding != null && encoding.indexOf("gzip") != -1)
{
response.setHeader("Content-Encoding" , "gzip");
out = new GZIPOutputStream(response.getOutputStream());
}
else if (encoding != null && encoding.indexOf("compress") != -1)
{
response.setHeader("Content-Encoding" , "compress");
out = new ZipOutputStream(response.getOutputStream());
}
else
{
out = response.getOutputStream();
System.out.println("gewone uitvoerstroom");
}
byte[] b = text.getBytes();
out.write(b);
}
What am I doing wrong?
thx in advance
Simple question, I want to receive compressed data from a server using
AJAX.
At the client I make an xmlHttpRequest. Next I want to set the
accept-header to Gzip, but I keep getting errors on that under opera.
At server-side I first check the accept-header, if it supports gzip I
make a ZIP-stream and set the content-encoding to gzip. Normaly I would
think the browser itself can handle it and give the uncompressed
response.
Clientcode (javascript):
xmlHttp = =new XMLHttpRequest();
xmlHttp.onreadystatechange = stateChange;
//don't work
xmlHttp.setRequestHeader("Accept-Encoding", "gzip");
xmlHttp.open("GET", "servlet", false);
xmlHttp.send(null);
function stateChange() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
var response = xmlHttp.responseText;
alert(response);
var uncompressedResponse = response;
document.getElementById("code").innerHTML =
"<p>"+uncompressedResponse+"</p>";
}
}
Servercode (servlet):
public void doGet(HttpServletRequest request, HttpServletResponse
response) {
String text = "sometext";
response.setContentType("text/html");
OutputStream out = null;
String encoding = request.getHeader("Accept-Encoding");
if (encoding != null && encoding.indexOf("gzip") != -1)
{
response.setHeader("Content-Encoding" , "gzip");
out = new GZIPOutputStream(response.getOutputStream());
}
else if (encoding != null && encoding.indexOf("compress") != -1)
{
response.setHeader("Content-Encoding" , "compress");
out = new ZipOutputStream(response.getOutputStream());
}
else
{
out = response.getOutputStream();
System.out.println("gewone uitvoerstroom");
}
byte[] b = text.getBytes();
out.write(b);
}
What am I doing wrong?
thx in advance