将GET参数中带有空格的url从Javaservlet传递给Perl
全部 我有一个Javaservlet程序来调用perl并从perl接收数据。我曾经将GET参数中带有空格的url从Javaservlet传递给Perl,java,perl,Java,Perl,全部 我有一个Javaservlet程序来调用perl并从perl接收数据。我曾经 String urlString = "http://[hostname]:[port]/cgi-bin/perl.pl?city="+city+"&chain="+chain; URL url = new URL(urlString); URLConnection urlConnection = url.openConnection(); urlConnection.setAllowUserIntera
String urlString = "http://[hostname]:[port]/cgi-bin/perl.pl?city="+city+"&chain="+chain;
URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
urlConnection.setAllowUserInteraction(false);
InputStream urlStream = url.openStream();
作为命中perl的url,对于perl端,使用
my $city = param("city");
my $chain = param("chain");
my $url_temp = "http://www.tripadvisor.com/Search?q=${chain}+${city}";
在GET参数中获取城市值的步骤
servlet和perl都能很好地处理没有空间的城市的名称,但是,对于“洛杉矶”和“纽约”,我无法想出一个解决方案使其工作。
我试过string.replace(''+'),它什么也不返回;替换为%20将使程序运行出错
任何想法都将不胜感激。如科内拉克和纪尧姆所说,使用url编码
// first
try {
city = URLEncoder.encode(city, "UTF-8");
chain = URLEncoder.encode(chain, "UTF-8")
} catch (Exception e) {}
// now your code
String urlString = "http://[hostname]:[port]/cgi-bin/perl.pl?city="+city+"&chain="+chain;
URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
urlConnection.setAllowUserInteraction(false);
InputStream urlStream = url.openStream();
在将查询字符串附加到URL之前,确实应该对其进行URL编码。空间确实将转换为
%20
。程序给出了什么错误?我尝试了string.replace(“+”),它什么也不返回是什么意思?如果您忘记了,请记住字符串是不可变的,所以您应该执行类似于String=String.replace(“+”)的操作。实现这一切的实际好方法是使用urlcoder.encode()
作为查询参数和查询值,这将把“”转换为“+”