How to send plus operator in url in java - java

I am fetching data from server in my project. In some condition there is a need to send + operator in url with parameter. How can i send "+" in url with parameter.
here is my url
http://www.needsthesupermarket.com/webservice/dp/addCart.php?cart_id=43530&cust_id=13936&pid=11303&qty=1&combination=2 ltr + 1 kg&guest_id=2509245
In blank space i replace with %20. but problem with + sign. How can i send it in url?

%26 -> &
%2B -> +
You can decode/encode here

You should encode your GET parameters:
Uri.encode(someParam)
For example if you have some Map paramsGet with GET parameters:
final StringBuilder url = new StringBuilder("http://example.com");
String delim = "?";
for (final Map.Entry<String, String> entry : paramsGet.entrySet()) {
url.append(delim).append(entry.getKey()).append("=").append(Uri.encode(entry.getValue()));
delim = "&";
}

To Encode use
String encodedInput = java.net.URLEncoder.encode(inputText, "UTF-8");
To Decode use
String decodedInput = java.net.URLDecoder.decode(encodedInput, "UTF-8");

Related

How to read query param value if special character (&) part of query param value

Url: https://myproject.dev.com/methodology/Culture?contentName=abc & def&path=Test&type=folder
Need to fetch only query params from above URL but problem is '&' in contentName=abc & def so while fetching the contentName getting the value in two parts like abc, def.
Please suggest the approach to get contentName is abc & def instead of abc,def.
If the & character is part of the name or value in the query string then it has to be percent encoded. In the given example: contentName=abc%26def&path=Test&type=folder.
If we pass any type of special character we have to encode those values. Java provides a URLEncoder class for encoding any query string or form parameter into URL encoded format. When encoding URI, one of the common pitfalls is encoding the complete URI. Typically, we need to encode only the query portion of the URI.
public static void main(String[] args) {
Map<String, String> requestParameters = new LinkedHashMap<>();
requestParameters.put("contentName", "abc & def");
requestParameters.put("path", "Test");
requestParameters.put("type", "folder");
String encodedURL = requestParameters.keySet().stream()
.map(key -> key + "=" + encodeValue(requestParameters.get(key)))
.collect(Collectors.joining("&", "https://myproject.dev.com/methodology/Culture?", ""));
System.out.println(encodedURL);
}
private static String encodeValue(String value) {
String url = "";
try {
url = URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
} catch (Exception ex) {
System.out.println(ex);
}
return url;
}
Output:
https://myproject.dev.com/methodology/Culture?contentName=abc+%26+def&path=Test&type=folder

UrlDecoder getParameter not able to decode password containing '%'

I have a java servlet application where I am using URLDecoder to decode the value of password
Here is the part of my code:
Map<String, String[]> paramMap = request.getParameterMap();
Set<String> keySet = paramMap.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext())
{
String key = iterator.next();
if (!key.equals("_")){
try {
String value = URLDecoder.decode(request.getParameter(key),"UTF-8");
System.out.println("Getting parameter("+ key +" = '" + value +"')");
}
}
The value doesn't get decoded if it contains '%'
I tried to use getQueryString() which return strings containing percentage, but it doesn't have method to extract particular parameter
String requestParamValue = URLDecoder.decode(request.getQueryString(),"UTF-8");
This returns:
Request raw param decoded is feature=check&userid=xyz#gmail.com&password=Asp%8]a/Asp%8]a/
Is there any way I can get the url decoded using request.getParameter(key) for the string containing '%'
Thanks
Percent symbol is a reserved character in URL encoding so it must be encoded itself. The servlet should fail with BAD REQUEST.
The string provided for password parameter should be URL encoded. Doing that in java:
String result = java.net.URLEncoder.encode("Asp%8]a/Asp%8]a/",java.nio.charset. StandardCharsets.UTF_8.name());
result ==> "Asp%258%5Da%2FAsp%258%5Da%2F"

StringSubstitutor not working with JSON text

I am trying to do some String Substitution using the StringSubstitutor . My payload is often JSOn and it doesnt replaces the token always.
Example
String ss = "{\"media\":[{\"channels2\":\"[Token2]\",\"channels\":\"[Token1]\"}]}";
final Map<String, Object> tokenReplacementValues = new HashMap<>();
tokenReplacementValues.put("Token2", "33");
tokenReplacementValues.put("Token1", "22");
System.out.println("Tokens to tokenReplacementInstruction = {}" + tokenReplacementValues);
StringSubstitutor sub = new StringSubstitutor(tokenReplacementValues, "[", "]");
ss = sub.replace(ss);
System.out.println("After Token Replacement: " + ss);
But when i print , only one token is replaced.
After Token Replacement: {"media":[{"channels2":"[Token2]","channels":"22"}]}
Tried with various options like different prefix, suffix and token names. Nothing seems to work.
I think it is because of the nested [, the first token becomes [{\"channels2\":\"[Token2] which won't get replaced.
Without nesting I get:
Before Token Replacement: {"media":{"channels2":"[Token2]","channels":"[Token1]"}}
After Token Replacement: {"media":{"channels2":"33","channels":"22"}}
You should use a JSON processing library instead I guess.

Is there a clean way to check if a String contains a char from an other String

I'm currently trying to catch special chars from an URL to change them to their hex value (for example : "This shop" should transform into "This%20shop").
I was wondering if there was some clean was of looking into the string to find each special chars and replace them to their ascii values.
I try to do it because I have to pass PHP arguments into the url using GET.
Here would be an example of an url :
www.mysite.com/page?adress=I am Living here!
My code actually reaplace '%' and ' ' from URLs, but I'd like to change any special chars I've defined.
This is what I've done so far :
private final String specialChars = "!#\\[]`#$%&'()*+-<>=?";
public URL getURL(){
String tempUrl = baseURL;
Set<String> keys = this.arguments.keySet();
for (Map.Entry<String, String> entry : arguments.entrySet()) {
String value = entry.getValue();
if(entry.getValue().contains(specialChars)){
Log.e("INFO", "THIS URL CONTAINS SPECIAL CHARS");
}
//Replacing % char
if(entry.getValue().contains("%")){
Log.i("URL BUILD INFO", "URL ARGS CONTAINS '%'");
value = entry.getValue().replace("%", "%25");
}
//Replacing spaces
if(entry.getValue().contains(" ")){
Log.i("URL BUILD INFO", "URL ARGS CONTAINS SPACE");
value = entry.getValue().replace(" ", "%20");
}
baseURL += entry.getKey() + "=" + value + "&";
}
try{
this.url = new URL(baseURL);
} catch(MalformedURLException e){
Log.e("URL MALFORMED", "YOUR IS MALFORMED");
e.printStackTrace();
}
Log.d("URL IS VALID", url.toString());
return url;
}
From what I understood from String documentation, matches method should only return true if my URL matches exactly my specialChars charSequence, which isn't what I want. But I can't find any other method to do what I'm trying to achieve on the String documentation.
If you're just trying to handle url encoding there are existing solutions for that - see URLEncoder and URLDecoder (good previous answer here)

URL percent encoding in Java

URL url = new URL("http://www.example.com/data.php?q=%FD");
logger.info("url: " + url);
URI uri = url.toURI();
logger.info("uri ASCII: " + uri.toASCIIString());
logger.info("uri str : " + uri.toString());
logger.info("query : " + uri.getQuery());
logger.info("decoded : " + URLDecoder.decode(ur.getRawQuery(), "WINDOWS-1252"));
String scheme = uri.getScheme();
String auth = uri.getAuthority();
String path = uri.getPath();
String query = uri.getQuery();
URI cleanedURI = new URI(scheme, auth, path, query, null);
logger.info("cleaned uri ASCII: " + cleanedURI.toASCIIString());
logger.info("cleaned uri str : " + cleanedURI.toString());
The output is:
url: http://www.example.com/data.php?q=%FD
uri ASCII: http://www.example.com/data.php?q=%FD
uri str : http://www.example.com/data.php?q=%FD
query: q=�
decoded: q=ý
cleaned uri ASCII: http://www.example.com/data.php?q=%EF%BF%BD
cleaned uri str : http://www.example.com/data.php?q=�
So, when I split the URI into parts, and then construct again, I cannot get back the original URL. How can I get back the original URL, which is a correctly percent-encoded, valid URL.
Instead of getting %EF%BF%BD I need to get the original %3F.
(Actually what I am trying to achieve is to manipulate certain parts of the URL in a clean way, such as removing the fragment, but this has not much relation to my question.)
The URL http://www.example.com/data.php?q=? is same as http://www.example.com/data.php?q=%3F
%3F (or numeric 63) is nothing but the ascii code for character '?'.
Check it here: http://grox.net/utils/encoding.html
So, if you hit a browser with URL %3f or '?'; it should behave the same.
If you are much concerned with the how it displays on the console, you could try this.
String query = uri.getQuery();
char charData = query.charAt(0); // fetch the character from String
int asciiValue = (int)charData;
or
you could look into String's getByte() method. A short tutorial is here - http://www.tutorialspoint.com/java/java_string_getbytes.htm

Categories