Bring a url with special characters

1

I have a web service made in java in the Netbeans IDE, in which I want to bring back a url that contains special characters like the following + url example: link

When I use the get method to return the value to consult (ie the name of the course "course-v1% 3AU + AIRPOLLUTION + 2017_ENE"), it returns the following: course-v1: U AIRPOLLUTION 2017_ENE making the + signs appear as blank spaces. How can I make the url that returns the get method include the signs + that is (course-v1: U + AIRPOLLUTION + 2017_ENE)

Code

@GET
@Path("/buscarporcurso")
@Produces(MediaType.APPLICATION_JSON)
public Response getBycourseDisplayName(@QueryParam("courseDisplayName") String curso, @QueryParam("callback") String callback) throws UnsupportedEncodingException {

    EdxTrackEventDao obj = new EdxTrackEventDao();
    EdxTrackEvent c = null;
    try{
     c = obj.buscar("courseDisplayName", curso);
    }catch( Exception ex){
        System.out.println(ex.getMessage());
    }


    JSONArray array = new JSONArray();

    array.add(c.getAnonScreenName());



    Gson g = new Gson();
    String formatoJSON = g.toJson(array);
        if (callback != null) {
            return Response.ok(callback + "(" + formatoJSON + ")", "application/json;charset=UTF-8").status(Response.Status.OK).build();
        }
        return Response.ok(formatoJSON, "application/json;charset=UTF-8").status(Response.Status.OK).build();
}
    
asked by Gerardo Gutiérrez 20.06.2017 в 23:56
source

1 answer

1

The server part is correct, the blanks are not valid characters in a URL 1 , they are replaced by + in the URL and the server does the reverse conversion.

You can do a quick test by visiting google and doing a search that contains a blank space, you will see how the space becomes + .

What you have to do is send the value correctly encoded in the URL. For example, if you do it from Java, you can use the class java.net.URLEncoder ; for example

 String urlCodificada = URLEncoder.encode(
   "http://localhost:8080/WSv2/webresources/generic/buscarporcurso?courseDisplayName=course-v1%3AU+AIRPOLLUTION+2017_ENE",
   "UTF-8");

1 Or at least they were, with Unicode URLs I'm not so sure. But in any case the browsers still convert the blanks to + when passing parameters, so the server keeps interpreting the + as the value is a blank space encoded.     
answered by 21.06.2017 в 01:13