java - How do I encode the url in below code? -


i looking how escape/encode special characters when passing url in request.

  public static void senddata(string strval) throws ioexception{    string dosend="https://myhost.com/views?strval="+strval;   httpclient httpclient = new defaulthttpclient();  try {      system.out.println("inside try");      uribuilder builder = new uribuilder();      system.out.println("builder="+builder);       builder.setscheme("http");      builder.sethost("myhost.com").setpath("/views?");      builder.addparameter("strval", strval);       system.out.println("add param,sethost,setpath complete");       uri uri = builder.build();      system.out.println("uri="+uri);        httpget httpget = new httpget(uri);       system.out.println("httpget"+httpget);       httpresponse response = httpclient.execute(httpget);      system.out.println(response.getstatusline().tostring());       if (response.getstatusline().getstatuscode() == 200) {         string responsetext = entityutils.tostring(response.getentity());         system.out.println("responsetext="+responsetext);         httpclient.getconnectionmanager().shutdown();      } else {        system.out.println("server returned http code "                 + response.getstatusline().getstatuscode());      }   } catch (java.net.urisyntaxexception bad) {      system.out.println("uri construction error: " + bad.tostring());   }   catch(exception e){ system.out.println("e.getmessage=>"+e.getmessage());  }  } 

i printing out in getrequest url.. output string produced this: embedding wied charactrers

    http://myhost.com/views%3strval=?strval=http%3a%2f%2fcnn.com,http%3a%2f%2fespn.com  

i need output this:

    http://myhost.com/views?strval=http://cnn.com,http://espn.com 

can please me fix, how encode special characters ? in setpath? can please me modify code.

you can use java.net.urlencoder that.

like this:

public static void main(final string[] args) throws unsupportedencodingexception {     final string encode = urlencoder.encode("http://www.test.de/ -", "utf8");     system.out.println(encode);      final string decode = urldecoder.decode(encode, "utf8");     system.out.println(decode);      final string decodeespn = urldecoder.decode("http://myhost.com/views%3fstrval=?strval=http%3a%2f%2fcnn.com,http%3a%2f%2fespn.com", "utf8");     system.out.println(decodeespn); } 

it print you:

http%3a%2f%2fwww.test.de%2f+- http://www.test.de/ - http://myhost.com/views?strval=?strval=http://cnn.com,http://espn.com 

adjustments have example:

in example can print decoded url this:

system.out.println("httpget "+ urldecoder.decode(httpget, "utf8")); 

alternative:

is there reason use urlbuilder? this:

public static void senddata(string strval) throws ioexception {     string dosend = "https://myhost.com/views?strval=" + strval;     httpclient httpclient = new defaulthttpclient();     try     {         httpget httpget = new httpget(dosend);          httpresponse response = httpclient.execute(httpget);          if (response.getstatusline().getstatuscode() == 200)         {             string responsetext = entityutils.tostring(response.getentity());             httpclient.getconnectionmanager().shutdown();         }         else         {             system.out.println("server returned http code " + response.getstatusline().getstatuscode());         }     }     catch (java.net.urisyntaxexception bad)     {         system.out.println("uri construction error: " + bad.tostring());     }     catch (exception e)     {         system.out.println("e.getmessage=>" + e.getmessage());     }  } 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -