/**
* @param method is transferred as GET or post
* @param urlstring Basic URL
* @param parameters parameter Map
* @return
* @throws IOException
*/
public string GetToken (String method,string urlstring,map<string, string> parameters) throws ioexception{
HttpURLConnection urlconnection = null;
StringBuffer temp = null;
if (Method.equalsignorecase ("GET") && Parameters! = null) {
StringBuffer param = new StringBuffer ();
int i = 0;
For (String Key:parameters.keySet ()) {
if (i = = 0)
Param.append ("?");
Else
Param.append ("&");
Param.append (Key). Append ("="). Append (Parameters.get (key));
i++;
}
URLString + = param;
}
Establish a connection
URL url = new URL (urlstring);
URLConnection = (httpurlconnection) url.openconnection ();
Setting parameters
Set Connection mode
Urlconnection.setrequestmethod (method);;
Requires output
Urlconnection.setdooutput (TRUE);
Need to enter
Urlconnection.setdoinput (TRUE);
Cache not allowed
Urlconnection.setusecaches (FALSE);
Set request Properties
Urlconnection.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
Urlconnection.setrequestproperty ("Charset", "UTF-8");
Urlconnection.setconnecttimeout (40000);
Urlconnection.setreadtimeout (40000);
Urlconnection.connect ();
if (Method.equalsignorecase ("POST") && Parameters! = null) {
StringBuffer param = new StringBuffer ();
For (String Key:parameters.keySet ()) {
Param.append ("&");
Param.append (Key). Append ("="). Append (Parameters.get (key));
}
String json=json.tojsonstring (parameters);
Urlconnection.getoutputstream (). Write (Json.tostring (). GetBytes ());
Urlconnection.getoutputstream (). Flush ();
Urlconnection.getoutputstream (). Close ();
}
Get a response
int Resultcode=urlconnection.getresponsecode ();
if (Httpurlconnection.http_ok==resultcode) {
InputStream in = Urlconnection.getinputstream ();
BufferedReader BufferedReader = new BufferedReader (
New InputStreamReader (in));
temp = new StringBuffer ();
String line = Bufferedreader.readline ();
while (line! = null) {
Temp.append (line) append ("\ r \ n");
line = Bufferedreader.readline ();
}
Bufferedreader.close ();
}
if (temp! = null) {
return temp.tostring ();
}else{
return null;
}
}
Java HttpURLConnection Interface Call