/** * * * @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