HttpURLConnection accessing WEBAPI Code

Source: Internet
Author: User

Although the code is similar, but slightly changed some, hehe ... Thank you very much for the original owner.

Here is the code I changed. (hehe, although the change is not small)

private static string url =  "Http://xxx/api/v1/video/Register";p ublic void  Regist () {map<string, object> requestparamsmap = new hashmap<string,  Object> ();           requestparamsmap.put ("username",   "test11");           requestparamsmap.put ("Password",   "111");           printwriter printwriter =  null;          httpurlconnection conn =  null;        bufferedreader bufferedreader = null ;           stringbuffer responseresult = new  stringbuffer ();           stringbuffer params  = new stringbuffeR ();          //  organization request Parameters            iterator it = requestparamsmap.entryset (). Iterator ();           while  (It.hasnext ())  {               Map.Entry element =  (map.entry)   It.next ();               params.append ( Element.getkey ());               Params.append ("=");               Params.append (Element.getvalue ());               params.append ("&");          }           if  (Params.length ()  > 0)  {               params.deletecharat (Params.length ()  - 1);           }  try {         string newurl = url + "?" + params;   //First contact with this thing, directly stitching on the address Url url = new url (Newurl);         conn =  (HttpURLConnection)  url.openconnection ();         conn.setrequestmethod ("POST");//  commit mode          conn.setrequestproperty ("Content-type",  "Plain/text; charset=utf-8");                   //  Set the common request properties              &nbSp;conn.setrequestproperty ("Accept",  "*/*");               conn.setrequestproperty ("Connection",  "keep-alive");               //  send a POST request must set the following two lines                conn.setdooutput (True);               conn.setdoinput (True);             conn.connect ();             int responsecode = conn.getresponsecode ();               if  (responsecode != 200)  {              system.out.println ( "Not success");             } else {               system.out.println ("Success");             }               //  define the BufferedReader input stream to read the responsedata        of the URL        bufferedreader = new bufferedreader (new  InputStreamReader (                       conn.getinputstream ()));               String line;               while  ((Line = bufferedreader.readline ())  != null )  {    &nbsP;             responseresult.append ("/n"). Append (line);              }   /*  Displays response in Dialog */showdialog ("Registered success"  + responseresult.tostring (). Trim ());/*  Close dataoutputstream */} catch  (ioexception e)  {e.printstacktrace ();}  catch  (exception e)  {e.printstacktrace ();} Finally{conn.disconnect ();               try {                   if  (printwriter != null)  {                       printwriter.close ();                   }                   if  (bufferedreader != null)  {                       bufferedreader.close ();                   }               } catch  ( IOEXCEPTION&NBSP;EX)  {                   ex.printstacktrace ();               } }}

Originally do not want to directly splicing parameters in the address, but the use of OutputStream input stream is always reported 404, lazy directly spell on.

Hope to help others.

HttpURLConnection accessing WEBAPI Code

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.