带有标头的Java套接字问题

时间:2013-11-16 12:32:53

标签: java http proxy

我正在使用Java中的套接字编写代理,现在我遇到了一个奇怪的问题(我认为问题是标题)... 当我尝试将URL发送回客户端(在本例中为webbrowser)时,如果我连接到www.google.com,我会收到此错误:

  1. 这是一个错误。 您的客户发出了格式错误或非法的请求。这就是我们所知道的。
  2. 但奇怪的是,还有其他网络工作正常并且没有出现任何错误......

    对此有任何帮助将不胜感激......

    感谢。

    这是代码:

    import java.io.*;
    import java.net.*;
    import java.nio.charset.Charset;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Server {
    
    
    public void startServer() {
        final ExecutorService clientProcessingPool = Executors.newFixedThreadPool(10);
    
        Runnable serverTask = new Runnable() {
            @Override
            public void run() {
                try {
                    @SuppressWarnings("resource")
                    ServerSocket serverSocket = new ServerSocket(80);
                    while (true) {
                        Socket clientSocket = serverSocket.accept();
                        clientProcessingPool.submit(new ClientTask(clientSocket));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread serverThread = new Thread(serverTask);
        serverThread.start();
    
    }
    
    private class ClientTask implements Runnable {
        private final Socket clientSocket;
    
        private ClientTask(Socket clientSocket) {
            this.clientSocket = clientSocket;
        }
    
        @Override
        public void run() {
    
            try {
    
                String url = null;
                BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "ISO-8859-1"));
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
    
    
    
    
                StringBuilder builder = new StringBuilder();
                for (String buffer; (buffer = in.readLine()) != null;) {
                if (buffer.isEmpty()) break;
                builder.append(buffer.replaceAll("keep-alive", "close")+"\r\n");
                if(buffer.contains("Host"))
                    {
                    //parse the host
                    url = buffer.replaceAll("Host: ", "");
                    }
                //System.out.println(builder);
                }
    
       if (url.contains("elotrolado.net")){         
    
                out.write("HTTP/1.1 200 OK\r\n");
                out.write("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
                out.write("Server: Apache/0.8.4\r\n");
                out.write("Content-Type: text/html\r\n");
                out.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
                out.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
                out.write("\r\n");
                out.write("<TITLE>DENEGADO</TITLE>");
                out.write("<P>Acceso a "+url+" denegado</P>");
                out.close();
                in.close();
                clientSocket.close();         
    
       }
       else
       {
    
                String IP = InetAddress.getByName(url).getHostAddress().toString();                
    
                //new socket to send the information over
    
                Socket ss = new Socket(url, 80);
                //printwriter to send text over the output stream
                PrintWriter pw = new PrintWriter(ss.getOutputStream()); 
    
                //send the request from the client
                String req = builder.toString();
                System.out.println(req);
                String reqq=URLEncoder.encode(req, "UTF-8");
                pw.println(reqq);
                pw.flush();
    
                //create inputstream to receive the web page from the host
                BufferedInputStream inn = new BufferedInputStream(ss.getInputStream());
    
                //create outputstream to send the web page to the client
                BufferedOutputStream outt = new BufferedOutputStream(clientSocket.getOutputStream());
    
                byte[] bytebuffer = new byte[1024];
                int bytesread;
    
                //send the response back to the client
                while((bytesread = inn.read(bytebuffer)) != -1) {
                outt.write(bytebuffer,0, bytesread);
                outt.flush();
                }
    
                ss.close();
                pw.close();
                outt.close();
                inn.close();
                out.close();
                in.close();
                clientSocket.close();
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            }catch(RuntimeException e){
                e.printStackTrace();
            }
        }
    }
    

0 个答案:

没有答案