The difference between HTTP Content_Type Multipart/form-data and application/x-www-form-urlencoded

Source: Internet
Author: User

Recently in the making of an app, the server needs to log, record all the requested URL, IP and parameters, say the parameters, then the problem, I use filters to record the log, to see how I write in the filter:


public void DoFilter (ServletRequest arg0, Servletresponse arg1,filterchain arg2) throws IOException, Servletexception { HttpServletRequest request = (httpservletrequest) arg0;    Get urlstring URL = Request.getrequesturl (). toString ();//Get parameter/*stringbuffer info=new java.lang.StringBuffer ();    InputStream In=request.getinputstream ();    Bufferedinputstream buf=new Bufferedinputstream (in);    Bufferedinputstream Bufbak=buf;     Byte[] Buffer=new byte[1024];    int iread;    while ((Iread=buf.read (buffer))!=-1) {info.append (new String (Buffer,0,iread, "UTF-8"));     } logger.info (info.tostring () + "------xxxxx------"); *//* collection<part> ps= request.getparts ();     For (part P:ps) {collection<string> cs=p.getheadernames ();     for (String S:cs) {logger.info ("------Collection----" +s); }}*/string Name=request.getparameter ("UserId"); Logger.info ("--------name------------" +name); map<string,string[]> map = Request.getparametermap (); String queryString = "";              For (String Key:map.keySet ()) {string[] values = Map.get (key);                  for (int i = 0; i < values.length; i++) {String value = Values[i];              QueryString + = key + "=" + Value + "&"; }} logger.info ("--------------------" +querystring); String userid=null;//get user ID string[] userids = (string[]) map.get ("UserId"); if (null!=userids&&0<userids.length) {userid=userids[0];} Active area ipstring IP = this.getclientip (Request), Logger.info (URL + "" + Gson.tojson (map) + "+ IP +" "+userid);//logger.i NFO (Arg0.hashcode () + "-------hashcode-------------" +req.hashcode ()); Arg2.dofilter (arg0, arg1);}

But after running to find Request.getparametermap () get no data, what is the matter?

Later, I looked for andriod client open staff to look at their client's request code, found that they are using httpurlconnection to make the request, the approximate code is as follows:

URL url = new URL (u); HttpURLConnection urlconn = (httpurlconnection) url.openconnection ();//Set whether to httpurlconnection output, because this is a POST request, parameters to be placed in/ /HTTP body, so it needs to be set to True, False;urlconn.setdooutput (true) by default;//Whether the setting is read from HttpURLConnection and true by default; Urlconn.setdoinput (TRUE);//Post request cannot use cache Urlconn.setusecaches (FALSE);//Set the content type to be a serializable Java object//(if this item is not set, when the serialized object is transferred, When the Web service default is not this type may throw java.io.EOFException) Urlconn.setrequestproperty ("Content-type", "multipart/form-data;        boundary= "+" 00content0boundary00\r\n "); Application/x-java-serialized-object//urlconn.setrequestproperty ("Content-type", "application/ X-www-form-urlencoded ");//The method for setting the request is" POST ", the default is Geturlconn.setrequestmethod (" post ");//Connection,    All of the above configuration for urlconn must be done before connect, urlconn.connect ();/* * Post parameter method */outputstream OS = Urlconn.getoutputstream (); string param = new string (p);//parameter comma-delimited string eg.name=xiaoming,age=23,sex=0 os.write (Param.getbytes ());//    Call the getInputStream () function of the HttpURLConnection connection object,//Send the complete HTTP request message encapsulated in the memory buffer to the server. StringBuffer info=nEW Java.lang.StringBuffer (); InputStream instrm = Urlconn.getinputstream ();    <=== Note that the code snippet that actually sends the request is here Bufferedinputstream buf=new Bufferedinputstream (INSTRM);     Byte[] Buffer=new byte[1024];    int iread;    while ((Iread=buf.read (buffer))!=-1) {info.append (new String (Buffer,0,iread, "UTF-8"));    } System.out.println (info.tostring () + "------xxxxx------"); return info.tostring ();

The client's request Content-type is set to the

Multipart/form-data; boundary= "+" 00content0boundary00\r\n "

This is a format for media MIME , which is typically used to transfer large text or binary file uploads. Therefore, in the Request.getparametermap () can not get the parameters of the client request

If the Content-type is set to

application/x-www-form-urlencoded
parameters can be obtained through REQUEST.GETPARAMETERMAP (), but this encoding is inefficient when sending large amounts of text to the server, including text or binary data that contains non-ASCII characters




The difference between HTTP Content_Type Multipart/form-data and application/x-www-form-urlencoded

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.