Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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 从inputstream读取Http Get请求并将其发送到主机。使用插座_Java_Http_Sockets_Io_Network Programming - Fatal编程技术网

Java 从inputstream读取Http Get请求并将其发送到主机。使用插座

Java 从inputstream读取Http Get请求并将其发送到主机。使用插座,java,http,sockets,io,network-programming,Java,Http,Sockets,Io,Network Programming,我的任务是编写一个代理服务器,它使用java套接字处理来自客户机的get请求。我现在陷入了困境,一直在谷歌上寻找答案,但没有成功 Christophers提供的解决方案帮助我解决了第一个问题。现在我已经更新了代码,这就是我正在使用的 问题是,在将数据包发送回客户端循环时,它只下载大部分网页的一部分。目前我无法解释为什么它会这样 public class MyProxyServer { //Set the portnumber to open socket on public

我的任务是编写一个代理服务器,它使用java套接字处理来自客户机的get请求。我现在陷入了困境,一直在谷歌上寻找答案,但没有成功

Christophers提供的解决方案帮助我解决了第一个问题。现在我已经更新了代码,这就是我正在使用的

问题是,在将数据包发送回客户端循环时,它只下载大部分网页的一部分。目前我无法解释为什么它会这样

public class MyProxyServer {  
    //Set the portnumber to open socket on
    public static final int portNumber = 5555;  

    public static void main(String[] args){  
//create and start the proxy
MyProxyServer myProxyServer = new MyProxyServer();  
myProxyServer.start();  
}  
public void start(){  
System.out.println("Starting MyProxyServer ...");  
try {  
    //create the socket 
    ServerSocket serverSocket = new ServerSocket(MyProxyServer.portNumber);  

    while(true)
    {    
        //wait for a client to connect
        Socket clientSocket = serverSocket.accept();  

        //create a reader to read the instream
        BufferedReader inreader = new BufferedReader( new InputStreamReader(clientSocket.getInputStream(), "ISO-8859-1"));  

        //string builder for preformance when we loop over the inputstream and read lines
        StringBuilder builder = new StringBuilder();
        String host = "";


        for (String buffer; (buffer = inreader.readLine()) != null;) {
        if (buffer.isEmpty()) break;
        builder.append(buffer.replaceAll("keep-alive", "close"));
        if(buffer.contains("Host"))
            {
            //parse the host
            host = buffer.replaceAll("Host: ", "");
            }
        System.out.println(buffer);
        }


        String req = builder.toString();
        System.out.println("finshed reading \n" + req);
        System.out.println("host: " + host);


        //new socket to send the information over
        Socket s = new Socket(InetAddress.getByName(host), 80);

        //printwriter to send text over the output stream
        PrintWriter pw = new PrintWriter(s.getOutputStream()); 

        //send the request from the client
        pw.println(req+"\r\n");
        pw.flush();

        //create inputstream to receive the web page from the host
        BufferedInputStream in = new BufferedInputStream(s.getInputStream());

        //create outputstream to send the web page to the client
        BufferedOutputStream outbuffer = new BufferedOutputStream(clientSocket.getOutputStream());

        byte[] bytebuffer = new byte[1024];
        int bytesread;

        //send the response back to the client
        while((bytesread = in.read(bytebuffer)) != -1) {
        System.out.println(bytesread);
        outbuffer.write(bytebuffer,0, bytesread);
        outbuffer.flush();
        }
        System.out.println("done sending");

        //close the streams
        inreader.close();
        s.close();
        pw.close();
        outbuffer.close();
        in.close();
    }


} catch (IOException e) {  
    e.printStackTrace();
}  catch(RuntimeException e){
    e.printStackTrace();
}
}  
}

如果有人能向我解释为什么我不能让它正常工作以及如何解决它,我将非常感激


提前感谢。

首先,您使用的是基于字符的PrintWriter,您需要一个基于字节的图像流谢谢您给我提示!我一直在四处寻找,但没有找到你所描述的问题的解决办法。不使用HttpURLConnection java类。如果Printwriter不工作,我应该使用什么发送请求?远离读写器,使用它们相应的InputStream和OutputStream。正如@karakuricoder所说,读写器用于字符,而InputStream和OutputStream用于字节。当您使用InputStreamReader或OutputStreamWriter而不指定编码时,您正在通过根据平台默认编码转换字节来破坏字节,这是一个全局随机变量。在使用它们时,您永远不应该指定编码,在这种情况下,您应该只通过它们传递头。为了澄清:头是字符数据,您可能应该为这些数据使用读写器,如果您这样做,您肯定需要指定您正在使用的编码,如果我没有记错的话,HTTP的编码是ISO-8859-1。其余的数据应被视为字节,并使用InputStream/OutputStream按原样传递。感谢您的帮助!这很有帮助!我更改了用于从服务器接收数据并将其发送回客户端的读写器,解决了最初的问题。