求大神帮忙 有关java.io.IOException: Invalid Http response怎么解决

qq_25486551 2015-01-21 10:33:55
费学习光盘
积分商城

帖子
package socket;
//这是服务端的代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server implements Runnable{
Socket socket;

public Server(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
System.out.println(socket.getInetAddress());
try {
InputStream inputStream=socket.getInputStream();
byte buf[]=new byte[1024];
int length=0;
length=inputStream.read(buf);

System.out.println(new String(buf,0,length));



OutputStream outputStream=socket.getOutputStream();
PrintWriter printWriter=new PrintWriter(outputStream);
printWriter.println("<html><head></head><body><font size='30' color='red'>你好</font></body></html>");
printWriter.flush();
socket.shutdownInput();

System.out.println("发送数据成功");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
ServerSocket serverSocket=new ServerSocket(9090);
while(true)
{
Socket socket=serverSocket.accept();
Thread t=new Thread(new Server(socket));
t.start();
}
}

}

通过浏览器访问服务端,可以正常访问,不过运行的下面客户端的代码就抛出
java.io.IOException: Invalid Http response
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at socket.Client.main(Client.java:17)




//这是客户端的代码
package socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class Client {
public static void main(String[] args) {
try {
URL url=new URL("http://192.168.0.105:9090/");
URLConnection h =url.openConnection();
InputStream inputStream=h.getInputStream();
byte[] buf=new byte[1024];
int length=0;
length=inputStream.read(buf);
System.out.println(new String(buf,0,length));

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

}
...全文
4949 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sq__cdut 2017-04-18
  • 打赏
  • 举报
回复
在我的错误中,我是在我的输出语句前加入这条语句就好了:
printWriter.println("HTTP/1.1 200 OK\r\n");
这是我的代码

....
Socket socket = server.accept();
PrintWriter out = new PrintWriter(new OutputStreamWriter(
socket.getOutputStream()), true);
if (socket.isConnected()) {
    out.println("HTTP/1.1 200 OK\r\n");
    out.println(updateInfo());
}
socket.close();
....
echo86_ 2015-01-23
  • 打赏
  • 举报
回复
引用 3 楼 xtayhicbladwin 的回复:
由于 你是用HttpURLConnection 去读取内容 ,HttpURLConnection 解析的是http 协议 ,你服务端的内容里面不符合http协议 比较 http1.1版本 ,content-length 等等 没有 httpURLConnection 是无法解析的。所以会报 Invalid Http response 。 解决方案 按照http协议给返回头 设置content-length . 浏览器为什么不报错 。在于他的容错能力。
3楼正解。 服务端代码要修改一下:

public class Server implements Runnable{
	Socket socket;
	   
    public Server(Socket socket) {
            this.socket = socket;
    }

    @Override
    public void run() {
            System.out.println(socket.getInetAddress());
            try {
            InputStream inputStream=socket.getInputStream();
            byte buf[]=new byte[1024];
            int length=0;
            length=inputStream.read(buf);
           
        System.out.println(new String(buf,0,length));
           
           
        
        OutputStream outputStream=socket.getOutputStream();
        PrintWriter printWriter=new PrintWriter(outputStream);
        printWriter.println("HTTP/1.1 200 OK\r\n");
        printWriter.print("Date: Sat, 31 Dec 2005 23:59:59 GMT\r\n");
        printWriter.print("Content-Type: text/html;charset=ISO-8859-1\r\n");
        printWriter.println("Content-Length: " + "<html><head></head><body><font size='30' color='red'>你好</font></body></html>".getBytes().length + "\r\n");
        printWriter.println("<html><head></head><body><font size='30' color='red'>你好</font></body></html>");
        printWriter.flush();
       socket.shutdownInput();
       socket.close();
      
            System.out.println("发送数据成功");
            } catch (IOException e) {
                    e.printStackTrace();
            }
    }
    public static void main(String[] args) throws Exception {
            ServerSocket serverSocket=new ServerSocket(9090);
            while(true)  
            {
                    Socket socket=serverSocket.accept();
                    Thread t=new Thread(new Server(socket));
                    t.start();
            }
    }

}
highnewrain 2015-01-22
  • 打赏
  • 举报
回复
我运行了一下你上面的客户端和服务器程序,通过浏览器和客户端访问都是没有问题的! 若你是在同一台电脑上运行的话,你把客户端程序访问服务器的IP改为127.0.0.1试试看
xtayhicbladwin 2015-01-22
  • 打赏
  • 举报
回复
由于 你是用HttpURLConnection 去读取内容 ,HttpURLConnection 解析的是http 协议 ,你服务端的内容里面不符合http协议 比较 http1.1版本 ,content-length 等等 没有 httpURLConnection 是无法解析的。所以会报 Invalid Http response 。 解决方案 按照http协议给返回头 设置content-length . 浏览器为什么不报错 。在于他的容错能力。
一大三千 2015-01-22
  • 打赏
  • 举报
回复
要先运行服务器端,才能启动客户端。只启动客户端的话是得不到服务器响应连接的。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧