从html表单读取POST数据发送到serversocket [英] Reading POST data from html form sent to serversocket
问题描述
我尝试在Java中编写最简单的服务器应用程序,显示带有textarea输入的html表单,在提交后,我可以解析在该textarea中键入的xml。现在我构建简单的基于服务器的服务器,如下所示:
i try to write simplest possible server app in Java, displaying html form with textarea input, which after submitting gives me possibility to parse xml typed in that textarea. For now i build simple serversocket based server like that:
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();
}
}
表单后(带有xml的textarea和一个额外的文本输入) )在'gets'中提交String-type变量我有我的变量的Urlencoded值(也显示在屏幕上,它看起来像:
After form (textarea with xml and one additional text input) is submitted in 'gets' String-type variable I have Urlencoded values of my variables (also displayed on the screen, it looks like that:
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)){它给我的字符串像
What can i do to change GET to POST method (if i simply change it in form and than put " if (str.contains("POST")){" it gives me string like
gets = POST / HTTP/1.1
没有变量。之后,我如何使用我的xml textarea字段(称为'we')?
with no variables. And after that, how i can use xml from my textarea field (called 'we')?
推荐答案
这是我对读取POST主体的实现:
This is my implementation for read POST body:
try {
Socket socket = params[0];
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// read request
String line;
line = in.readLine();
StringBuilder raw = new StringBuilder();
raw.append("" + line);
boolean isPost = line.startsWith("POST");
int contentLength = 0;
while (!(line = in.readLine()).equals("")) {
raw.append('\n' + line);
if (isPost) {
final String contentHeader = "Content-Length: ";
if (line.startsWith(contentHeader)) {
contentLength = Integer.parseInt(line.substring(contentHeader.length()));
}
}
}
StringBuilder body = new StringBuilder();
if (isPost) {
int c = 0;
for (int i = 0; i < contentLength; i++) {
c = in.read();
body.append((char) c);
Log.d("JCD", "POST: " + ((char) c) + " " + c);
}
}
raw.append(body.toString());
publishProgress(raw.toString());
// send response
out.write("HTTP/1.1 200 OK\r\n");
out.write("Content-Type: text/html\r\n");
out.write("\r\n");
out.write(new Date().toString());
if (isPost) {
out.write("<br><u>" + body.toString() + "</u>");
} else {
out.write("<form method='POST'>");
out.write("<input name='name' type='text'/>");
out.write("<input type='submit'/>");
out.write("</form>");
}
//
// do not in.close();
out.flush();
out.close();
socket.close();
//
} catch (Exception e) {
e.printStackTrace();
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
publishProgress('\n' + sw.toString());
}
我是为Android做的, publishProgres
在我的情况下意味着:
I do it for android and publishProgres
in my case means:
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
instance.logTextView.append(values[0]);
}
这篇关于从html表单读取POST数据发送到serversocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!