URISyntaxException - How to deal with urls with %

北战南征 提交于 2019-12-08 19:26:59

问题


I am fairly new to Java and came across this issue. I tried searching but never got a correct answer.

I have a string for example

String name = anything 10%-20% 04-03-07

Now I need to build up a url string with this String name like below.

http://something.com/test/anything 10%-20% 04-03-07

I tried replacing the spaces with %20 and now I am getting the new url as

http://something.com/test/anything%2010%-20%%2004-03-07

When I use this url and fire it in firefox it just works fine but while processing in Java it is apparently throwing

Exception in thread "main" java.lang.IllegalArgumentException
at java.net.URI.create(Unknown Source)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:69)
Caused by: java.net.URISyntaxException: Malformed escape pair at index 39 : 
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.scanEscape(Unknown Source)
at java.net.URI$Parser.scan(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parseHierarchical(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
... 6 more

This is the code throwing error

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);

回答1:


Encode also the percent sign with %25.

http://something.com/test/anything 10%-20% 04-03-07 would work with http://something.com/test/anything%2010%25-20%25%2004-03-07.

You should be able to use for example URLEncoder.encode for this - just remember, that you need to urlencode the path part, not anything before that, so something like

String encodedUrl =
    String.format("http://something.com/%s/%s",
      URLEncoder.encode("test", "UTF-8"),
      URLEncoder.encode("anything 10%-20% 04-03-07", "UTF-8")
    );

Note: URLEncoder encodes spaces to + instead of %20, but it should work equally well, both are ok.




回答2:


You could use java.net.URI to create a uri from your string

String url = "http://something.com/test/anything 10%-20% 04-03-07"

URI uri = new URI(
    url,
    null);
String request = uri.toASCIIString();

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(request);
HttpResponse response = httpclient.execute(httpget);


来源:https://stackoverflow.com/questions/12422256/urisyntaxexception-how-to-deal-with-urls-with

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!