当我在 Get 请求中传递 url 时,我正在寻找如何对特殊字符进行转义/编码。
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()); }
}
我在getRequest url中打印出来。产生的输出字符串是这样的:这是嵌入一些wied charactrers
http://myhost.com/views%3strval=?strval=http%3A%2F%2Fcnn.com,http%3A%2F%2Fespn.com
我需要得到这样的输出:
http://myhost.com/views?strval=http://cnn.com,http://espn.com
有人可以帮我解决,我如何编码特殊字符,如?在设置路径中?另外你能帮我修改这段代码吗?