一个关于socket接收post数据的问题

sunxing007 2009-06-25 10:40:41
加精
写了一个MyServer模拟http server 接收post请求。
java代码:

package socket;

import java.io.*;
import java.net.*;

public class MyServer {
public static void main(String args[]) throws Exception{
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("server is ok.");
while(true){
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = in.readLine();
while(line!=null){
System.out.println("Client: " + line);
line = in.readLine();
}
System.out.println("current user close the session.");
in.close();
socket.close();
}
}
}


用浏览器打开下面的html文件,并提交数据:

<head>
<title>test my server</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>upload</p>
<form name="UploadForm" method="post" action="http://localhost:8080/1.jsp">
<input type="text" name="myname" /><br>
<select name="myage">
<option value="18">18</option>
<option value="20">20</option>
<option value="22">22</option>
</select><br>
<input type="submit"value="Sutmit">
</form>
</body>
</html>

MyServer打印出来的数据开始还正常:
server is ok.
Client: POST /testupload.jsp HTTP/1.1
Client: Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Client: Referer: http://localhost:8080/post.html
Client: Accept-Language: zh-cn
Client: User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
Client: Content-Type: application/x-www-form-urlencoded
Client: Accept-Encoding: gzip, deflate
Client: Host: localhost:8080
Client: Content-Length: 28
Client: Connection: Keep-Alive
Client: Cache-Control: no-cache
Client:
走到这里的时候, 按http协议,接下来浏览器该传送post的数据了,但是程序走到这里就不走了。当我按esc之后,post数据才被提交上去
Client: myname=wwwwwwwwwwww&myage=18
current user close the session.

很奇怪!
高手出来!
...全文
8922 56 打赏 收藏 转发到动态 举报
写回复
用AI写文章
56 条回复
切换为时间正序
请发表友善的回复…
发表回复
securityzn 2012-06-28
  • 打赏
  • 举报
回复
羡慕LZ的学生啊,我也希望有个这么好的老师指导~~~
wangqi885 2011-08-15
  • 打赏
  • 举报
回复
学习!都是高手啊
shenwenxiu1009 2011-06-13
  • 打赏
  • 举报
回复
我想弱弱的问下,为什么 ,提交后回进入MyHttpServer 这个类啊。你告诉我吧。谢谢。
metsys 2011-04-14
  • 打赏
  • 举报
回复
csdn怎么能这样?
zjforandroid 2010-12-13
  • 打赏
  • 举报
回复
为看15楼...
xa445110675 2010-06-21
  • 打赏
  • 举报
回复
import java.io.File;


public class msbTree {
public static void main(String[] args){
File file = new File("C:/Documents and Settings/Admini" +
"strator/workspace/FileTree/src/a");
System.out.println(file.getName());
tree(file,1);
}

public static void tree(File file,int level){
String fr = "";
for(int k=0;k<level;k++){
fr += " ";
}
File[] children = file.listFiles();
for(int i=0;i<children.length ;i++){
System.out.println(fr+"/"+children[i].getName());
if(children[i].isDirectory()&&children[i].listFiles()!=null){
tree(children[i],++level);
}

}
}
}
fei1703 2010-06-01
  • 打赏
  • 举报
回复
学习了。
kerlos 2010-04-21
  • 打赏
  • 举报
回复
昨天刚犯了和楼主一样的错误,将socket丢给另外的方法,再重新new一个新的的ObjectOutputStream就出问题了,学习了,很有帮助
anxin1018_chen 2010-04-19
  • 打赏
  • 举报
回复
学习了,十分有用 谢谢楼主的帖子哦
huangwuyi 2009-10-29
  • 打赏
  • 举报
回复
楼主!
我是用J2ME调用怎么response返回的信息都是空的
我是调用你的例子
还有没有一些HTTP开发的标准协议规范没?
如果有能发给我的Emial:yige388@126.com
谢谢
ljsheng 2009-07-02
  • 打赏
  • 举报
回复
java 的不懂!!
晴天v1 2009-07-02
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 jiangnaisong 的回复:]
引用 13 楼 sunxing007 的回复:
谢谢jiangnaisong, jinxfei,这个问题折腾了两天了。昨天回去重写代码,还是有一个大问题,我描述在程序中,谢谢再帮忙看看。
先不看有附件的情况,现在doGet方法可以work, doPost还有问题,我描述在方法里面。谢谢几位高手再看看


答;楼主现在遇到的问题,我很清楚根源在何处。
1)说一句可能楼主生气的话:楼主对socket流的用法不是很规范。原因是:每一个socket,关联一个输入流及…
[/Quote]

经典,学习了。
CTCandQJ 2009-07-01
  • 打赏
  • 举报
回复
学习了
sunxing007 2009-06-29
  • 打赏
  • 举报
回复
关于 java实现http协议服务器端 比较详细的文档我放在blog里面
http://blog.csdn.net/sunxing007/archive/2009/06/29/4305956.aspx
sunxing007 2009-06-29
  • 打赏
  • 举报
回复
我也是个普通的dev
sunxing007 2009-06-29
  • 打赏
  • 举报
回复
[Quote=引用 33 楼 shijianwu1986 的回复:]
学习了 。。。
自己这段时间也准备复习socket,这个老师好啊,我们的老师上课都是照着课本读了一下,就结束了。
[/Quote]
我不是老师,是给别人做培训!
chudu 2009-06-29
  • 打赏
  • 举报
回复
teacher1998 2009-06-29
  • 打赏
  • 举报
回复
学习
云上飞翔 2009-06-28
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 sunxing007 的回复:]
我按照jiangnaisong的意思,改了程序, 因为考虑到要上传附件, 所以我用了DataInputStream, 虽然readLine方法已经推荐不使用,暂时将就一下。
上传附件的问题解决了。但是现在post还是有一个问题, 我描述在程序中,谢谢大家再帮忙看看。
[/Quote]
答:
1)楼主的整个doPost()与doMultiPart()没有严格按HTTP协议来写,这会有问题的。我认为要重新写。
2)现在首先针对楼主的那个小问题:“这里出问题了:如果提交普通的post(不带附件),程序走到这里不走了;就是说没有读到数据,”,原因或根源是什么?
还是因为楼主的代码没有按HTTP协议的规定要求写。将dopost()中的代码:

//这里出问题了:如果提交普通的post(不带附件),程序走到这里不走了;就是说没有读到数据,
//但是当我按浏览器ESC之后提交的数据正文就读出来了。
//dataLine = reader.readLine();
//我改为一个字符一个字符的读取也不可以。
char[] buf = {};
if (this.contentLength != 0) {
buf = new char[this.contentLength];
int size = 0;
char c = reader.readChar();
while(c!='\n'){
buf[size++] = c;
c = reader.readChar();
}
System.out.println("The data user posted: " + new String(buf, 0, size));
}


改为:

byte[] buf = {};
if (this.contentLength != 0) {
buf = new byte[this.contentLength];
int size = 0;

while(size<this.contentLength){
int c = reader.read();
buf[size++] = (byte)c;

}
System.out.println("The data user posted: " + new String(buf, 0, size));
}



则这个小问题就解决了。但我认为:整个doPost()与doMultiPart()要严格按HTTP协议来重写,这才是解决问题的根本之道。
jackbeibei 2009-06-28
  • 打赏
  • 举报
回复
看来不我的解释了
加载更多回复(34)

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧