How do I encode the url in below code?
I am looking for how to escape/encode the special characters when I am
passing the url in Get 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?strval=");
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 am printing this out in the getRequest url.. The output string produced
is like this: This is embedding some wied charactrers
http://myhost.com/views%3strval=?strval=http%3A%2F%2cnn.com,http%3A%2F%2espn.com
I need to get the output something like this:
http://myhost.com/views?strval=http://cnn.com,http://espn.com
Can someone please help me fix, how do I encode special characters like ?
in the setpath? Also Can you please help me modify this code.
No comments:
Post a Comment