java中的代理:Firefox中无法识别的编码

时间:2012-01-08 16:39:44

标签: java html http sockets proxy

这又是我。 所以今天我用Java编写代理服务器。 我已完成连接处理和套接字操作,甚至下载。

现在我遇到这样的问题,Firefox连接到代理并要求显示谷歌网站返回我:

Error in content encoding.

我希望它能正确显示页面。

这就是我阅读网页内容的方式:

BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line;

            System.out.println("            READING RESPONSE!");
            int contentLength = 0;
            while ((line = rd.readLine()) != null) {
                ret+=line+"\r\n";
                System.out.println("                "+line);
                if(line.startsWith("Content-Length:"))
                {
                    String[] tmpCL = line.split(" ");
                    contentLength = Integer.parseInt(tmpCL[1]);
                }
                if(line.length() == 0)
                {
                    body = new char[contentLength];
                    for(int r= 0; r < contentLength; r++)
                    {
                        body[r] = (char) rd.read();

                    }
                }
            }
            System.out.println("            GOT RESPONSE!");

在这里我是如何将它发回给客户的:

  System.out.println("      SENDING PAGE TO CLIENT");   
                //System.out.println(page);
                OutputStream Cout = clientSocket.getOutputStream();

                Cout.write(page.getBytes(Charset.forName("UTF-8")));
                System.out.println("        Page Sent..."); 

这是我的代理调试输出的示例:(一切似乎都很好)

    REQUEST: GET http://www.rfc-editor.org/rfc/rfc4501.txt HTTP/1.1
        getPage!
                GET http://www.rfc-editor.org/rfc/rfc4501.txt HTTP/1.1
                Host: www.rfc-editor.org
                User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
                Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
                Accept-Language: pl,en-us;q=0.7,en;q=0.3
                Accept-Encoding: gzip, deflate
                Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7
                Proxy-Connection: keep-alive
                Cache-Control: max-age=0
            REQUEST SENT!
            READING RESPONSE!
                HTTP/1.1 200 OK
                Date: Sun, 08 Jan 2012 16:32:09 GMT
                Server: Apache/2.2.10 (Linux/SUSE) mod_ssl/2.2.10 OpenSSL/0.9.8h PHP/5.2.13 with Suhosin-Patch mod_python/3.3.1 Python/2.6 mod_perl/2.0.4 Perl/v5.10.0
                Last-Modified: Wed, 03 May 2006 21:42:10 GMT
                ETag: "376faa-51fe-412e928ead080"-gzip
                Accept-Ranges: bytes
                Vary: Accept-Encoding
                Content-Encoding: gzip
                Content-Length: 7481
                Content-Type: text/plain

            GOT RESPONSE!
HTTP/1.1 200 OK
Date: Sun, 08 Jan 2012 16:32:09 GMT
Server: Apache/2.2.10 (Linux/SUSE) mod_ssl/2.2.10 OpenSSL/0.9.8h PHP/5.2.13 with Suhosin-Patch mod_python/3.3.1 Python/2.6 mod_perl/2.0.4 Perl/v5.10.0
Last-Modified: Wed, 03 May 2006 21:42:10 GMT
ETag: "376faa-51fe-412e928ead080"-gzip
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 7481
Content-Type: text/plain
你能指点我的错误吗? 我认为我使用它作为字符串是错误的但是...... HTML只是基于字符串。

0 个答案:

没有答案