本地启动一个服务public class Server { public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8888);
Socket client= server.accept();
System.out.println("一个客户端建立了连接....");
InputStream in = client.getInputStream();
byte[] temp = new byte[1024*1024];
int len = in.read(temp);
System.out.println(len);
String requestinfo = new String(temp,0,len);
System.out.println(requestinfo);
in.close();
client.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }}
谷歌浏览器和postman请求时的结果是:一个客户端建立了连接....
-1
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.checkBounds(String.java:381)
at java.lang.String.<init>(String.java:545)
at com.httpserver.Server.main(Server.java:19)
根据报错信息可以知道是输入流没有东西。
但是用IE浏览器请求的时候返回结果是:一个客户端建立了连接....
391
GET /aaaa?uname=zee&pwd=123456 HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763
Accept-Encoding: gzip, deflate
Host: localhost:8888
Connection: Keep-Alive是能正常获取请求信息的,造成这个差异结果的原因是什么呢?
try {
ServerSocket server = new ServerSocket(8888);
Socket client= server.accept();
System.out.println("一个客户端建立了连接....");
InputStream in = client.getInputStream();
byte[] temp = new byte[1024*1024];
int len = in.read(temp);
System.out.println(len);
String requestinfo = new String(temp,0,len);
System.out.println(requestinfo);
in.close();
client.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }}
谷歌浏览器和postman请求时的结果是:一个客户端建立了连接....
-1
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.checkBounds(String.java:381)
at java.lang.String.<init>(String.java:545)
at com.httpserver.Server.main(Server.java:19)
根据报错信息可以知道是输入流没有东西。
但是用IE浏览器请求的时候返回结果是:一个客户端建立了连接....
391
GET /aaaa?uname=zee&pwd=123456 HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763
Accept-Encoding: gzip, deflate
Host: localhost:8888
Connection: Keep-Alive是能正常获取请求信息的,造成这个差异结果的原因是什么呢?
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8888);
while(true) {
Socket socket = server.accept();
InputStream is= socket.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
while(true) {
String temp=null;
if(socket!=null) {
temp= br.readLine();
}
System.out.println(temp);
}
}
}
}
ServerSocket server;
try {
server = new ServerSocket(8888);
} catch (IOException e1) {
e1.printStackTrace();
return;
}
while (true) {
try {
Socket client = server.accept();
System.out.println("一个客户端建立了连接....");
InputStream in = client.getInputStream();
byte[] temp = new byte[1024 * 1024];
int len = in.read(temp);
System.out.println("接收请求长度为:"+len+"字节");
if(len!=-1){
System.out.println(new String(temp,0,len));
}
in.close();
client.close();
} catch (IOException e) {
System.out.println("错误了");
}
} }}
postman请求一次的结果:一个客户端建立了连接....
接收请求长度为:-1字节
一个客户端建立了连接....
接收请求长度为:247字节
GET /aaaa?uname=zee&pwd=123456 HTTP/1.1
User-Agent: PostmanRuntime/7.19.0
Accept: */*
Cache-Control: no-cache
Postman-Token: 7b9d87c6-720c-4620-9dbc-3512b8516565
Host: localhost:8888
Accept-Encoding: gzip, deflate
Connection: keep-alive
IE请求一次的结果:一个客户端建立了连接....
接收请求长度为:391字节
GET /aaaa?uname=zee&pwd=123456 HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763
Accept-Encoding: gzip, deflate
Host: localhost:8888
Connection: Keep-Alive
谷歌浏览器请求一次返回:一个客户端建立了连接....
接收请求长度为:-1字节
一个客户端建立了连接....
接收请求长度为:555字节
GET /aaaa?uname=zee&pwd=123456 HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36
Sec-Fetch-User: ?1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
一个客户端建立了连接....
接收请求长度为:555字节
GET /aaaa?uname=zee&pwd=123456 HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36
Sec-Fetch-User: ?1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9那时什么原因造成这种差异的呢?