Java request URL can take parameters

Source: Internet
Author: User

/** * * * @param urlstr * URL * @param content * Parameters submitted * @param encoding * Encoding format * @return*/     Public StaticString Geturlresult (String urlstr, map<string,object>map,string method) {URL URL=NULL; HttpURLConnection Connection=NULL; Try{URL=NewURL (URLSTR); Connection= (httpurlconnection) url.openconnection ();//Create a new connection instanceConnection.setconnecttimeout (10000);//Set the connection time-out, in millisecondsConnection.setreadtimeout (10000);//set the Read data time-out time in millisecondsConnection.setdooutput (true);//whether to open the output stream True|falseConnection.setdoinput (true);//whether to open the input stream True|falseConnection.setrequestmethod (method);//Submission Method post| GETConnection.setusecaches (false);//whether to cache True|falseConnection.connect ();//Open the connection portDataOutputStream out=NewDataOutputStream (Connection.getoutputstream ());//Open the output stream to write data to the peer serverString content=buildurlquery (map);  out. writebytes (content);//write the data, that is, submit your form name=xxx&pwd=xxx             out. Flush ();//Refresh             out. Close ();//turn off the output streamBufferedReader reader =NewBufferedReader (NewInputStreamReader (Connection.getinputstream (),"UTF-8"));//Data- to-end server returns data to the peer//To read the BufferedReader stream.StringBuffer buffer =NewStringBuffer (); String Line="";  while(line = Reader.readline ())! =NULL) {buffer.append (line);            } reader.close (); returnbuffer.tostring (); } Catch(IOException e) {e.printstacktrace (); } finally {            if(Connection! =NULL) {connection.disconnect ();//Close Connection            }        }        return ""; }    Private StaticString buildurlquery (map<string, object>Querys) throws unsupportedencodingexception {StringBuilder sbquery=NewStringBuilder (); if(NULL!=Querys) {             for(Map.entry<string, object>Query:querys.entrySet ()) {                if(0<sbquery.length ()) {Sbquery.append ("&"); }                if(String.valueof ((Query.getkey ())). Equals ("NULL") &&! String.valueof ((Query.getvalue (). toString ()). Equals ("NULL") {sbquery.append (Query.getvalue ()); }                if(! String.valueof ((Query.getkey ())). Equals ("NULL") {sbquery.append (Query.getkey ()); if(! String.valueof (Query.getvalue ()). Equals ("NULL") {sbquery.append ("="); Sbquery.append (Urlencoder.encode (Query.getvalue (). toString (),"Utf-8")); }                                    }            }                    }                returnsbquery.tostring (); }

Java request URL can take parameters

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.