HttpURLConnection the Web interface does not receive a value when using the Post pass value __web

Source: Internet
Author: User
Tags readline stringbuffer

Haven't written a blog for a long time recently, because of the reason for work. I would like to make a point system, mobile version and server version today. Data is transmitted using JSON data. But the challenge is to get the mobile side to transfer data to the server and then update to the database. To get to the point:
Because I'm using httpurlconnection, we all know that the parameters of a GET request are placed behind the URL. Opening & splitting. But if we have more parameters or parameters that we don't want people to know, we're going to use post, because his parameters are placed in the corresponding head, get transmission data is very simple do not speak, the most important is the post pass parameters, because I met a few pits here, in fact, is not difficult, mainly some small details we will not pay attention to.
Code to pass parameters:

URL u=new url (url);
            String param= "pw=" +urlencoder.encode ("Ding Ding", "UTF-8");
            HttpURLConnection h= (httpurlconnection) u.openconnection ();
            H.setdoinput (true);
            H.setdooutput (true);
            H.setrequestmethod ("POST");
            H.setrequestproperty ("Connection", "keep-alive");
             H.setrequestproperty ("Charset", "UTF-8");
             H.connect ();
                Creates an input stream and passes the parameter
                dataoutputstream dos=new dataoutputstream (H.getoutputstream ())
                to the URL to point to. Dos.writebytes (param);
                Dos.flush ();
                Dos.close ();

You will find in fact that we are using this upload parameters, the meaning of this does not need to introduce it, you think this is OK, in fact, we found that Tomcat will not print "ding ding" this value. But our code is correct Ah, also executed, at first I thought is the interface problem, but get why get no problem, so I think it should be our own problem, and then I have a line of code to try, found no problem. I just added a few lines:

int Resultcode=h.getresponsecode ();
                if (httpurlconnection.http_ok==resultcode) {
                  stringbuffer sb=new stringbuffer ();
                  String Readline=new string ();
                  BufferedReader responsereader=new BufferedReader (New InputStreamReader (H.getinputstream (), "UTF-8"));
                  while ((Readline=responsereader.readline ())!=null) {
                    sb.append (readLine). Append ("\ n")
                  ;
                  Responsereader.close ();
                  LOG.D ("haha", sb.tostring ());

This discovery is useful, of course, I have a line to comment execution, and later found that, as long as the above line of code can be executed

int Resultcode=h.getresponsecode ();

The Tomacat can print out the value of the

as long as it can pass the parameter after the transmission is completed. As for the reason, I am not too clear. It is because of the less add this, I can not get out, are almost collapsed. Then this is the line that will be useful. Maybe some people have met the pit, hope to be useful to everyone

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.