关键字: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

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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

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

使用HttpWebRequest post数据时要注意UrlEncode

今天在用HttpWebRequest类向一个远程页面post数据时,遇到了一个怪问题,总是出现500的内部服务器错误.

post和get提交服务器编码过程

先说出一个知识点: 如果浏览器端编码是UTF-8,那在服务器端解决乱码问题的方法有两种: 浏览器提交数据到web服务器的方式: Post方式 request.setCharacterEncoding...

Url encode和decode 相关问题

今天在弄url解码时,弄了好久。比如我的URL带有中文,浏览器默认将中文转成UTF-8编码,但是我进行UTF-8解码时,发现解析出来的中文是乱码,并不是我想要的值。经过查找资料,发现再将UTF-8解析...

http_build_query — 生成 URL-encode 之后的请求字符串(http_build_query最新中文翻译)

http_build_query — 生成 URL-encode 之后的请求字符串 说明 string http_build_query ( mixed $query_data [, st...

【Node.js-1】NodeJS简单入门、http、fs、url、querystring、接收客户端get/post数据

1、使用Sublime Text,最好安装一下Node这个package,安装之后不仅有代码提示这些,而且可以直接右击run,不再需要到cmd里面node xxx去执行。当然,里面的代码基本就是JS的...

简单的HTTP请求处理器,可以处理POST,GET方式访问URL

总结平时用到的处理POST,GET的方式访问URL的方法,若有错之处,请大家帮忙指正。下面是代码 package com.api.mapping; import java.io.Buffere...

Http协议与请求, Post请求与Get请求的区别,Base64和URL编码,md5,sha-1加密,对称和非对称加密,支付宝微信第三方支付

Http协议与请求 Post请求 Post请求与Get请求的区别 Get请求的参数是直接放在url后面的,而Post请求是放在请求体中的 Get请求参数的长度...

用POST和GET获取URL后的参数问题

近期在学习的时候,碰到一个关于post和get的小问题,故拿出来分享一下。jsp: 当method是get时: 111当meth

HTTP Status 405 - HTTP method POST is not supported by this URL

程序出现: HTTP Status 405 - HTTP method POST is not supported by this URL 发现原因如下 源程序: request.getR...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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