Java ServerSocket未接收HTTP响应-超时

Java ServerSocket未接收HTTP响应-超时,java,Java,我正在为一个简单的客户机-服务器程序编写一个原型。客户端发出HTTP PUT请求(工作成功),但未收到HTTP响应,正在超时。我已经玩了几个小时的代码,尝试了InputStream和OutputStream,但是我遗漏了一些东西。可能很简单,因为我没有用Java编写太多代码 客户端代码: URL url = new URL("http://" + remoteFilepath); HttpURLConnection httpCon = (HttpURLConnection) ur

我正在为一个简单的客户机-服务器程序编写一个原型。客户端发出HTTP PUT请求(工作成功),但未收到HTTP响应,正在超时。我已经玩了几个小时的代码,尝试了InputStream和OutputStream,但是我遗漏了一些东西。可能很简单,因为我没有用Java编写太多代码

客户端代码:

URL url = new URL("http://" + remoteFilepath);
        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
        httpCon.setReadTimeout(5000);
        httpCon.setDoOutput(true);
        //httpCon.setDoInput(true);
        httpCon.setRequestMethod("PUT");
        OutputStreamWriter out = new OutputStreamWriter(
            httpCon.getOutputStream());
        String tmp = getContent(localFilepath);
        System.out.println("tmp: " + tmp);
        out.write(tmp + "\n"); 
        out.flush();
        httpCon.connect();
        System.out.println("before afterClient");
        BufferedReader fromClient = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
        System.out.println("after fromClient");
        String line;
        while ((line = fromClient.readLine()) != null) {
            System.out.println(line);
        }
        System.out.println("response code: " + httpCon.getResponseCode() + ", response message: " + httpCon.getResponseMessage());
服务器代码:内部运行方法

BufferedReader fromClient = new BufferedReader(new InputStreamReader(conn.getInputStream()));
if(line.startsWith("PUT")){
                    methodLine = line.split(" ");
                    this.put(fromClient, line, methodLine);
}
put()方法-包含http响应

public void put(BufferedReader fromClient, String line, String[] putLine){
        String[] contentLine;

        try{
        while((line = fromClient.readLine()) != null){
            if(line.startsWith("Content-Length:")){
                contentLine = line.split(" ");
                //get the content, then send back HTTP response
                int numChars = Integer.parseInt(contentLine[1]);
                char[] cbuf = new char[numChars]; 
                fromClient.readLine(); //skip over blank line
                fromClient.read(cbuf);  //read the specified number of bytes

                System.out.println("cbuf" + new String(cbuf));

                //store <file,content> in memory

                contentMap.put(putLine[1], new String(cbuf));

                //send back HTTP response then break
                PrintWriter out = new PrintWriter(
                        conn.getOutputStream());
                out.write("HTTP/1.1 200 OK" + "\n");  //response 
                out.flush();
                break;
            }
        }
public void put(BufferedReader fromClient,String line,String[]putLine){
字符串[]内容行;
试一试{
而((line=fromClient.readLine())!=null){
if(line.startsWith(“内容长度:”){
contentLine=line.split(“”);
//获取内容,然后发回HTTP响应
int numChars=Integer.parseInt(contentLine[1]);
char[]cbuf=新字符[numChars];
fromClient.readLine();//跳过空行
fromClient.read(cbuf);//读取指定的字节数
System.out.println(“cbuf”+新字符串(cbuf));
//存储在内存中
put(putLine[1],新字符串(cbuf));
//发送回HTTP响应,然后中断
PrintWriter out=新的PrintWriter(
conn.getOutputStream());
out.write(“HTTP/1.1 200 OK”+“\n”);//响应
out.flush();
打破
}
}

嗯,你可以在你的回复中写下这个,然后我用邮递员来测试它,这会告诉我我想看什么

HTTP/1.1 200 OK
Content-Type: text/html
COntent-Length: your length

在HTTP响应中使用两个“\n”来尝试…(表示没有标题),这似乎有帮助。它现在似乎正在阻止fromClient.readLine().ServerSockets不接收任何内容,服务器也不接收响应,而是发送响应。请解决此问题。@ON15 HTTP中的行终止符是\r\n,而不是\n。
ServerSockets
不接收任何内容,服务器发送响应,而不是接收响应。