我尝试用 Java 编写最简单的服务器应用程序,显示带有 textarea 输入的 html 表单,提交后让我可以解析在该 textarea 中键入的 xml。现在我像这样构建简单的基于serversocket的服务器:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class WebServer {
protected void start() {
ServerSocket s;
String gets = "";
System.out.println("Start on port 80");
try {
// create the main server socket
s = new ServerSocket(80);
} catch (Exception e) {
System.out.println("Error: " + e);
return;
}
System.out.println("Waiting for connection");
for (;;) {
try {
// wait for a connection
Socket remote = s.accept();
// remote is now the connected socket
System.out.println("Connection, sending data.");
BufferedReader in = new BufferedReader(new InputStreamReader(
remote.getInputStream()));
PrintWriter out = new PrintWriter(remote.getOutputStream());
String str = ".";
while (!str.equals("")) {
str = in.readLine();
if (str.contains("GET")){
gets = str;
break;
}
}
out.println("HTTP/1.0 200 OK");
out.println("Content-Type: text/html");
out.println("");
// Send the HTML page
String method = "get";
out.print("<html><form method="+method+">");
out.print("<textarea name=we></textarea></br>");
out.print("<input type=text name=a><input type=submit></form></html>");
out.println(gets);
out.flush();
remote.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
public static void main(String args[]) {
WebServer ws = new WebServer();
ws.start();
}
}
在“gets”字符串类型变量中提交表单(带有 xml 的 textarea 和一个额外的文本输入)后,我有我的变量的 Urlencoded 值(也显示在屏幕上,它看起来像这样:
gets = GET /?we=%3Cnetwork+ip_addr%3D%2210.0.0.0%2F8%22+save_ip%3D%22true%22%3E%0D%0A%3Csubnet+interf_used%3D%22200%22+name%3D%22lan1%22+%2F%3E%0D%0A%3Csubnet+interf_used%3D%22254%22+name%3D%22lan2%22+%2F%3E%0D%0A%3C%2Fnetwork%3E&a=fooBar HTTP/1.1
我该怎么做才能将 GET 更改为 POST 方法(如果我只是在表单中更改它而不是放入“if(str.contains(“POST”)){“它给了我类似的字符串
gets = POST / HTTP/1.1
没有变量。之后,我如何使用我的 textarea 字段(称为“我们”)中的 xml?