关键字:HTTP POST URL-encode(困扰我5天的问题)
来源:互联网 发布:excel表格相同数据筛选 编辑:程序博客网 时间:2024/06/04 17:51
事情的起因得从Content-Type: application/x-www-form-urlencoded(用于发送name-value对)说起:)
Servlet can't get HttpServletRequest's Parameter ?
http://www.j2meforums.com/forum/index.php?topic=11249.msg55068#msg55068
I use Http POST method to connect to servlet, but the servlet can't get HttpServletRequest's Parameter.The req.getParameter("name") method always return NULL.The code is below.
Midlet:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class POSTTest extends MIDlet {
protected void startApp() throws MIDletStateChangeException {
String message ="name="+URLEncoder.encode("dodo");
System.out.println("encode String is "+message);
try {
HttpConnection con = (HttpConnection)Connector.open("http://127.0.0.1:8080/jsmartsales/ReverseServlet");
//指定POST
con.setRequestMethod(HttpConnection.POST);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Content-Language", "en-US" );
con.setRequestProperty("Content-Length",Integer.toString(message.getBytes().length));
DataOutputStream dos = con.openDataOutputStream();
dos.write(message.getBytes());
dos.flush();
dos.close();
DataInputStream in = con.openDataInputStream();
int input;
while((input = in.read())!=-1){
System.out.print((char)input);
}
in.close();
con.close();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
}
Servlet
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
public class ReverseServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
String s = req.getParameter("name");
System.out.println("The name is"+s);
out.println("success!");
}
}
---------------------------------------------------------------------------------------
--------------------------------------------------
I use HTTPLook(sniffer software) to see what was send out, The Http Package is below:
---------start----------
POST /jsmartsales/ReverseServlet HTTP/1.1
IF-Modified-Since: 20 Jan 2001 16:19:14 GMT
Content-Type: application/x-www-form-urlencoded
User-Agent: Profile/MIDP-1.0 Configuration/CLDC-1.0
Content-Language: en-US
name:
User-Agent: UNTRUSTED/1.0
Host: 202.100.22.226:8080
Transfer-Encoding: chunked
9
name=dodo
0
--------------end----------------
I don't know the '9' and '0' come from?
--------------------------------------------------------------------
They are part of the chunked mode!
But anyway remove the flush() calls in your code
and invoke getResponseCode() so you get rid of
/ Lion
----------------------------------------------------------------------------
Thank you lion .The question has puzzled me for five days.the result is just
.
And why is it?
-------------------------------------------------------------------------------
Does this mean that your Servlet now can understand your urlencoded data?
How does the the HTTPlook output looks like now?
My intention was to try to get your code to NOT use chunked encoding since I expected that you use an old (pre 2.4) version of Servlet that doesn't handle HTTP/1.1 (and hence not understand chunked encoding)
/ Lion
PS
I couldn't resist to check what Servlet you are running...
Is seems to be Apache Tomcat 5.5 that corresponds tp Servlet 2.4 so now I don't
know... Please show the HTTPlook output...
-----------------------------------------------------------------------------
Yes, Now the servlet can receive parameter.The HTTPLook output is below
-------------start------------
POST /jsmartsales/ReverseServlet HTTP/1.1
IF-Modified-Since: 20 Jan 2001 16:19:14 GMT
Content-Type: application/x-www-form-urlencoded
User-Agent: Profile/MIDP-1.0 Configuration/CLDC-1.0
Content-Language: en-US
Content-Length: 9
User-Agent: UNTRUSTED/1.0
Host: 202.100.22.226:8080
name=dodo
--------------end------------
--------------start-----------
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 10
Date: Mon, 05 Jun 2006 05:57:12 GMT
success!
--------------end--------------
and I use Tomcat 5.5.17
Thank u , Lion
------------------------------------------------------------------------------
Thank you for reporting the result.
I can see that,
1) The name: header disappered
2) The Content-length header appered
3) No more chunking...
/ Lion
------------------------------------------------------------------------
- 关键字:HTTP POST URL-encode(困扰我5天的问题)
- 困扰我多天的问题
- 解决困扰我很多天的调试问题
- 一个困扰了我2天的问题。
- 困扰我多时的500错误问题
- 又一个困扰我的简单问题。
- 困扰我一个小时的问题 SetToolTip
- 困扰我几天的BPEL问题--soap
- 最近一直困扰我的问题
- 困扰我多年的数据库索引问题
- 一个让我很困扰的问题
- http url encode/decode 总结
- 我的思想困扰
- 我的困扰
- 困扰我的多线程
- http编程URL的Get与post
- 关于java的URL编码的问题(URLencode,encode)
- HTML Encode 和URL Encode的区别
- AjaxAnyWhere的原理
- MIME
- 去掉Intel芯片组鼠标右键菜单集成显卡菜单
- Hibernate中in expected错误的原因之一。
- 业务知识
- 关键字:HTTP POST URL-encode(困扰我5天的问题)
- RSS阅读中潜在的安全问题 【 综合文 】
- Velocity学习笔记3——Velocity 模板语言(VTL: Velocity Template Language)简介
- 第一篇
- 使用AQTime 例子
- 如何获得当前所在的DLL模块名称
- c/s结构和b/s结构的区别或其优缺点
- 超越图灵机(二)——神秘的不可数无穷大
- 电脑报的HackGame,没意思