Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java套接字与头的问题_Java_Http_Proxy - Fatal编程技术网

Java套接字与头的问题

Java套接字与头的问题,java,http,proxy,Java,Http,Proxy,我正在用Java中的套接字编写一个代理,现在我遇到了一个关于头的奇怪问题(我认为问题在于头)。。。 当我尝试将URL发送回客户端(在本例中为webbrowser)时,如果我连接到www.google.com,就会出现以下错误: 那是个错误。 您的客户端发出了格式错误或非法的请求。我们只知道这些 但奇怪的是,还有其他网站运行良好,没有出现任何错误 在此方面的任何帮助都将不胜感激 谢谢 代码如下: import java.io.*; import java.net.*; import java.ni

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

  • 那是个错误。 您的客户端发出了格式错误或非法的请求。我们只知道这些
  • 但奇怪的是,还有其他网站运行良好,没有出现任何错误

    在此方面的任何帮助都将不胜感激

    谢谢

    代码如下:

    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();
            }
        }
    }
    
    import java.io.*;
    导入java.net。*;
    导入java.nio.charset.charset;
    导入java.util.concurrent.ExecutorService;
    导入java.util.concurrent.Executors;
    公共类服务器{
    public void startServer(){
    final ExecutorService clientProcessingPool=Executors.newFixedThreadPool(10);
    Runnable serverTask=new Runnable(){
    @凌驾
    公开募捐{
    试一试{
    @抑制警告(“资源”)
    ServerSocket ServerSocket=新的ServerSocket(80);
    while(true){
    Socket clientSocket=serverSocket.accept();
    提交(新的ClientTask(clientSocket));
    }
    }捕获(IOE异常){
    e、 printStackTrace();
    }
    }
    };
    线程serverThread=新线程(serverTask);
    serverThread.start();
    }
    私有类ClientTask实现可运行{
    私有最终套接字clientSocket;
    专用客户端任务(套接字客户端套接字){
    this.clientSocket=clientSocket;
    }
    @凌驾
    公开募捐{
    试一试{
    字符串url=null;
    BufferedReader in=新的BufferedReader(新的InputStreamReader(clientSocket.getInputStream(),“ISO-8859-1”);
    BufferedWriter out=新的BufferedWriter(新的OutputStreamWriter(clientSocket.getOutputStream());
    StringBuilder=新的StringBuilder();
    for(字符串缓冲区;(buffer=in.readLine())!=null;){
    if(buffer.isEmpty())中断;
    builder.append(buffer.replaceAll(“保持活动”、“关闭”)+“\r\n”);
    if(buffer.contains(“主机”))
    {
    //解析主机
    url=buffer.replaceAll(“主机:”,“”);
    }
    //System.out.println(生成器);
    }
    if(url.contains(“elotrolado.net”){
    out.write(“HTTP/1.1 200 OK\r\n”);
    写出(“日期:1999年12月31日星期五23:59:59 GMT\r\n”);
    out.write(“服务器:Apache/0.8.4\r\n”);
    out.write(“内容类型:text/html\r\n”);
    out.write(“Expires:Sat,2000年1月1日00:59:59 GMT\r\n”);
    out.write(“上次修改:1996年8月9日星期五14:21:40 GMT\r\n”);
    out.write(“\r\n”);
    写出(“DENEGADO”);
    写出(“

    Acceso a”+url+“denegado

    ”); out.close(); in.close(); clientSocket.close(); } 其他的 { 字符串IP=InetAddress.getByName(url.getHostAddress().toString(); //通过新套接字发送信息 套接字ss=新套接字(url,80); //printwriter通过输出流发送文本 PrintWriter pw=新的PrintWriter(ss.getOutputStream()); //从客户端发送请求 String req=builder.toString(); 系统输出打印项次(要求); 字符串reqq=URLEncoder.encode(req,“UTF-8”); pw.println(需求量); pw.flush(); //创建inputstream以从主机接收网页 BufferedInputStream inn=新的BufferedInputStream(ss.getInputStream()); //创建outputstream以将网页发送到客户端 BufferedOutputStream outt=新的BufferedOutputStream(clientSocket.getOutputStream()); byte[]bytebuffer=新字节[1024]; int字节读取; //将响应发送回客户端 而((bytesread=inn.read(bytebuffer))!=-1){ 输出写入(字节缓冲,0,字节读取); out.flush(); } ss.close(); 关闭(); out.close(); 克洛斯酒店(); out.close(); in.close(); clientSocket.close(); } }捕获(IOE异常){ e、 printStackTrace(); }捕获(运行时异常e){ e、 printStackTrace(); } } }
    JavaScript与此问题无关。您的