关键字:HTTP POST URL-encode(困扰我5天的问题)

原创 2006年05月31日 10:41:00

事情的起因得从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:


package dodo;
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

Code:

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?

 

--------------------------------------------------------------------

Code:
Transfer-Encoding: chunked


Quote
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

Code:
name:



/ Lion

----------------------------------------------------------------------------

Thank you lion Wink.The question has puzzled me for five days.the result is just

Quote
remove the flush()

.
And why is it?

-------------------------------------------------------------------------------

Quote
the result is just

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

------------------------------------------------------------------------


HttpClient URL编码方法

HTTP请求格式告诉我们,有两个位置或者说两种方式可以为request提供参数:request-line方式与request-body方式。 request-line   reque...
  • lifj07
  • lifj07
  • 2013年02月20日 16:15
  • 14574

URL中带加号的处理

问题起因:       客户订购了一关键字为"e+h 变送器" , 在首页推荐广告中,会根据用户在search 搜索过的关键字进行一个匹配投放。技术实现是UED 通过JS 获取cookie 中的h_...
  • z69183787
  • z69183787
  • 2014年06月30日 10:55
  • 32523

URL编码与二次encodeURI的区别和用法

URL编码 一般来说,URL只能使用英文字母、阿拉伯数字和某些标点符号,不能使用其他文字和符号。比如,世界上有英文字母的网址 "http://www.abc.com",但是没有希腊字母的网址...
  • alicegreat
  • alicegreat
  • 2013年12月21日 00:08
  • 1160

关于URL encode

在HTML Javascript中,对URL进行加解码时要点: 1.在Javascript中String.charCodeAt()获得的编码为Unicode(big-endian);在进行URL...
  • hongweigg
  • hongweigg
  • 2014年02月19日 23:52
  • 3319

http url转义字符,特殊字符

空格 - %20 " - %22 # - %23 % - %25 & - %26 ( - %28 ) - %29 + - %2B , - %2C / - %2F : - %3A ...
  • ios_xumin
  • ios_xumin
  • 2016年02月18日 14:31
  • 4934

URL编码以及GET和POST提交乱码解决方案

1.  什么是URL编码。 URL编码是一种浏览器用来打包表单输入的格式,浏览器从表单中获取所有的name和其对应的value,将他们以name/value编码方式作为URL的一部分或者分离的发...
  • lfsf802
  • lfsf802
  • 2012年02月04日 09:39
  • 48377

关于URL的encode和decode

URL无法显示某些特殊符号,这个时候就要使用编码了。编码的格式为:一个百分号,后面跟对应字符的ASCII(16进制)码值。例如 空格的编码值是"%20"。(ASCII参考)   ...
  • yangyunqi2011
  • yangyunqi2011
  • 2013年01月12日 04:11
  • 2607

URL编码与二次encodeURI

URL编码 一般来说,URL只能使用英文字母、阿拉伯数字和某些标点符号,不能使用其他文字和符号。比如,世界上有英文字母的网址 "http://www.abc.com",但是没有希腊字母的网址"h...
  • LJHABC1982
  • LJHABC1982
  • 2011年12月22日 11:41
  • 8775

GET和POST的区别及get和post关于请求的编解码的问题

GET和POST的本质区别是什么?         使用GET,form中的数据将编码到url中,而使用POST的form中的数据则在http协议的header中传输。在使用上,当且仅当请求幂等(字...
  • abc19900828
  • abc19900828
  • 2012年08月01日 19:35
  • 3418

https post调用以及前端到后台中传输数据encode和decode的自动转化问题

https post调用以及前端到后台中传输数据encode和decode的自动转化问题
  • LiThinkingBoy
  • LiThinkingBoy
  • 2017年05月22日 11:51
  • 1021
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:关键字:HTTP POST URL-encode(困扰我5天的问题)
举报原因:
原因补充:

(最多只允许输入30个字)