简体   繁体   中英

Safe URI building

I'm writing a class in Java which access the url http://hostA/webProjectB/ServletC with some parameters. Since it is a get http call I can append the parameters to the URL ..

http://hostA/webProjectB/ServletC?parameter1=California&parameter2=yes

This returns a response in xml format <root><parameter3>123</parameter3></root>

Basically I'm interested in extracting the value of parameter3 in my http response which I know how to do.

My question is the WebUrl upto the servlet I'm storing in an external configuration which I retrieve as a String and can I simply append the get call parameters 1 and 2 to this string I retrieve ? Will this create any possible issues if I built the get URL as a string ?

You need to ensure that you are encoding the parameters you are defining correctly, ie using URLEncoder.encode(string,charset) .

You might be better served with one of the URI templating libraries, ie Handy URI Templates or the JAX-RS UriBuilder

Otherwise, as long as you are escaping characters correctly (ie URLEncoder.encode(string,charset) for both the param name and value) you should be fine

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