简体   繁体   中英

How to make empty space in URL be readable by passing json URL

> String url = "http://localhost:61819/RestServiceImpl.svc/json/empty url";

如何在传递网址时删除空白区域。

String url = "http://localhost:61819/RestServiceImpl.svc/json/empty url".replaceAll(" ","%20");

(要么)

  String url=URLEncoder.encode("http://localhost:61819/RestServiceImpl.svc/json/empty url", "UTF-8");

try this class

import java.net.UrlEncoder;
String newUrl = URLEncoder.encode(oldUrl);

It is not a good way to replace the spaces in url. It may change the url.

Instead, I'd prefer to encode the url by using URLEncoder.encode() method. That way, the spaces and also the special characters will be handled accordingly.

In a J2EE application, if you are sending a url as a response then you should use encodeRedirectUrl() method of HttpServletResponse.

Prefer Encoding than Pattern Matching. Just encode the String as below:

String url = "http://localhost:61819/RestServiceImpl.svc/json/empty url";
       url = URLEncoder.encode(url, "UTF-8");

Use can check All Unicode URL From this Site !

for space just use:-

URI uri = new URI(string.replace(" ", "%20"));  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM