【发布时间】:2019-08-26 19:07:03
【问题描述】:
读完整个字符串后,读卡器卡在while,甚至不抛出异常。我正在通过 curl 向服务器发送请求。
我尝试更改 curl 的 Content-Type,更改字符串的内容,并使用另一种方式读取输入,例如扫描仪,但总是卡在 while。
curl -H "Content-Type: text/plain" -d "asdasdasdashdiasdasgdasgduascuyasvccccc" 192.168.0.59:8080
// Read characters from the client via input stream on the socket
in = new BufferedReader(new
InputStreamReader(connect.getInputStream()));
// Get character output stream to client (for headers)
out = new PrintWriter(connect.getOutputStream());
// Get binary output stream to client (for requested data)
dataOut = new BufferedOutputStream(connect.getOutputStream());
// Get first line of the request from the client
input = in.readLine();
// Parse the request with a string tokenizer
if (input == null || input.isEmpty()) {
System.err.println("Invalid input");
return;
}
parse = new StringTokenizer(input);
method = parse.nextToken().toUpperCase();
input = in.readLine();
do {
parse = new StringTokenizer(input);
if (parse.hasMoreTokens()) {
header = parse.nextToken().toUpperCase(); // we get the
HTTP method of the client
}
if (parse.hasMoreTokens()) {
hValue = parse.nextToken().toLowerCase();
}
// Support only GET and HEAD methods, we check
out.println(header + hValue);
System.out.println(header + hValue);
if (header.equals("CONTENT-TYPE:")) {
readFile(in);
}
input = in.readLine();
} while (!input.isEmpty());
private void readFile(BufferedReader file) throws IOException {
try {
int count;
count = 0;
while ((count = file.read()) != -1) {
System.out.print((char) count);
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
file.close();
}
}
日志:
Server started.
Listening for connections on port : 8080 ...
Connection opened. (Mon Aug 26 15:44:49 BRT 2019)
HOST:192.168.0.59:8080
USER-AGENT:curl/7.58.0
ACCEPT:*/*
CONTENT-TYPE:text/plain
Content-Length: 39
asdasdasdashdiasdasgdasgduascuyasvccccc
【问题讨论】:
标签: java curl server inputstream bufferedreader