[英]Recive POST data by http server
我有來自Android Samples的android上的簡單http服務器。 我喜歡這個服務器,所以我也想從瀏覽器中獲取POST數據。 我該如何使用標准的東西(沒有外部庫)來重新獲得它? 我嘗試像GET一樣接收它,但是js控制台顯示連接錯誤。
private void handle(Socket socket) throws IOException {
BufferedReader reader = null;
PrintStream output = null;
try {
String route = null;
// Read HTTP headers and parse out the route.
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while (!TextUtils.isEmpty(line = reader.readLine())) {
if (line.startsWith("GET /")) {
int start = line.indexOf('/') + 1;
int end = line.indexOf(' ', start);
route = line.substring(start, end);
break;
}
}
// Output stream that we send the response to
output = new PrintStream(socket.getOutputStream());
// Prepare the content to send.
if (null == route) {
writeServerError(output);
return;
}
byte[] bytes = loadContent(route);
if (null == bytes) {
writeServerError(output);
return;
}
// Send out the content.
output.println("HTTP/1.0 200 OK");
output.println("Content-Type: " + detectMimeType(route));
output.println("Content-Length: " + bytes.length);
output.println();
output.write(bytes);
output.flush();
} finally {
if (null != output) {
output.close();
}
if (null != reader) {
reader.close();
}
}
}
通常,http請求是一個文本片段。 請求的類型在文本中指出。 因此,在GET請求中,您提供的示例首先找到“ GET”字符串。 然后解析get請求。 對於POST應該執行相同的操作。 首先識別“ POST”,然后解析其余的請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.