当前位置:编程学习 > JAVA >>

HttpServer中HttpExchange getRequestBody方法获取不到Post请求表单数据

// Java 类文件
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class JavaHttpServer {

public static void main(String[] args) {
try {
HttpServer hs = HttpServer.create(new InetSocketAddress(8888), 0);// 设置HttpServer的端口为8888
hs.createContext("/get/user", new MyHandler());// 用MyHandler类内处理到
hs.setExecutor(null); // creates a default executor
hs.start();
} catch (Exception e) {

}
}
}

class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String line = null;
while((line = in.readLine()) != null){
System.out.println(line); // 是空的???为啥啊
}
String response = "<font color='#ff0000'>come on baby</font>";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
// 测试表单
<html>
<body>
<form action="http://127.0.0.1:8888/get/user" method="POST">  
<button type="submit">提交</button>
<input id="txtX1" type="text" value="111111"/>
<input id="txtY1" type="text" value="2222"/>
</form>
</body>
</html> java HttpServer HttpExchange  --------------------编程问答-------------------- request.getParameter --------------------编程问答-------------------- 这个大哥,请看一下标题好吗,不是直接使用Web的,而是使用HttpServer,目前只能获取到超链接后面的参数,获取不到表单传输的数据.... --------------------编程问答-------------------- HttpHandler  HttpExchange 这两个类是自己写的吗?t.getRequestBody();这里面是怎么写的 --------------------编程问答--------------------
引用 3 楼 qq864680621 的回复:
HttpHandler  HttpExchange 这两个类是自己写的吗?t.getRequestBody();这里面是怎么写的


HttpHandler  HttpExchange 都是jdk1.6特有的,不是我自己写的,t.getRequestBody()也不是我自己写的,都是Java的,我需要获取以表单形式提交的数据。。。。 --------------------编程问答-------------------- 已经解决了,谢谢大家回帖! --------------------编程问答-------------------- 把解决办法贴出来,现在在发愁呢。。
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,