How to append parameters to a http url and encode it

public static String getEncodedUrl(String url, Map<String, String> parameters)
 {
 if (parameters == null)
 {
 return url;
 }
 else if (parameters.size()==0)
 {
 return url;
 }

StringBuilder parametersEncoded = new StringBuilder();
 boolean first = true;
 for (String key : parameters.keySet())
 {

 if (first) first = false; else parametersEncoded.append("&");
 try
 {
 String key_encoded = URLEncoder.encode(key,Charset.defaultCharset().displayName());
 String value_encoded = URLEncoder.encode(parameters.get(key),Charset.defaultCharset().displayName());
 parametersEncoded.append(key_encoded + "=" + value_encoded);
 }
 catch (Exception e)
 {
 e.printStackTrace();
 }
 }

 if(parametersEncoded.length()>0 && url!=null)
 {
 if(!url.endsWith("?"))
 url += "?";

 url += parametersEncoded.toString();
 }

 return url;
 }
Advertisements

Tags: ,

About krishjlk

http://www.facebook.com/krishna.kanth.121

One response to “How to append parameters to a http url and encode it”

  1. morpheus pratiksha says :

    good cooment posting

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: