This question already has answers here:
How to obtain the last path segment of a URI
(13 answers)
Closed 7 years ago.
I wanted to get the last part of the referrer URL. How would be the best way to obtain this information?
String referrer = request.getHeader("referer");
Currently this is how I am getting the referrer URL. This is giving me the entire URL, but I only want part of the URL.
For Example: Requested URL: http://localhost:8080/TEST/ABC.html
If this is the referrer URL, I only want the ABC.html.
Thank you for the assistance, please let me know if there are any misunderstandings to my question.
This will give you XYZ.html :
String url = "http://localhost:8080/TEST/XYZ.html";
url = url.substring(url.lastIndexOf("/") + 1);
Use java.net.URL.getFile()
String path = new URL( request.getHeader( "referer" )).getPath();
int sep = path.lastIndexOf( '/' );
return ( sep < 0 ) ? path : path.substring( sep + 1 );
It's basic string manipulation, treat it like any other string. You can use String.indexOf/String.lastIndexOf and String.substring, or you can use String.split, or you can use StringTokenizer, or you can use Regular Expressions (the most flexible option, but requires learning regex).
Just get the substring after the last / occurence.
Related
This question already has answers here:
Parse a URI String into Name-Value Collection
(30 answers)
Closed 4 years ago.
In selenium, is there a way to grab partial dynamically changing data from a URL? Example, I ran driver.getCurrentUrl() in my code and retrieved the below URL. I only want to grab the numeric portion of it and concatenate it so I end up with 819499-1. However, next time I run it the values will be different. Is there a way to capture this information without capturing the entire URL?
http://custmaint/clnt_estimate_overview.jsp?estimateNbr=819499&versionNbr=1&estimateMgmt=true&clntAutoAssign=true
I think this is a more generic question, not only related to Selenium...
Basically, You could use regular expression matching like so:
String url = "http://custmaint/clnt_estimate_overview.jsp?estimateNbr=819499&versionNbr=1&estimateMgmt=true&clntAutoAssign=true";
final Pattern p = Pattern.compile("^.*\\?estimateNbr=(\\d+)&versionNbr=(\\d+).*$");
Matcher m = p.matcher(url);
if (m.find()) {
System.out.print(m.group(1) + "-" + m.group(2));
}
Another approach (more flexible) is to use a dedicated library like httpcomponents; see How to convert map to url query string?
String url = driver.getCurrentUrl();
String estimateNbr = url.substring(url.indexOf("eNbr=")+5, url.indexOf("&v"));
String versionNbr = url.substring(url.indexOf("nNbr=")+5, url.indexOf("&e"));
System.out.println(estimateNbr + "-" + versionNbr);
I need to replace the spaces inside a string with the % symbol but I'm having some issues, what I tried is:
imageUrl = imageUrl.replace(' ', "%20");
But It gives me an error in the replace function.
Then:
imageUrl = imageUrl.replace(' ', "%%20");
But It still gives me an error in the replace function.
The I tried with the unicode symbol:
imageUrl = imageUrl.replace(' ', (char) U+0025 + "20");
But it still gives error.
Is there an easy way to do it?
String.replace(String, String) is the method you want.
replace
imageUrl.replace(' ', "%");
with
imageUrl.replace(" ", "%");
System.out.println("This is working".replace(" ", "%"));
I suggest you to use a URL Encoder for Encoding Strings in java.
String searchQuery = "list of banks in the world";
String url = "http://mypage.com/pages?q=" + URLEncoder.encode(searchQuery, "UTF-8");
I've ran into issues like this in the past with certain frameworks. I don't have enough of your code to know for sure, but what might be happening is whatever http framework you are using, in my case it was spring, is encoding the URL again. I spent a few days trying to solve a similar problem where I thought that string replace and the URI.builder() was broken. What ended up being the problem was that my http framework had taken my encoded url, and encoded it again. that means that any place it saw a "%20", it would see the '%' charictor and switch it out for '%' http code, "%25", resulting in. "%2520". The request would then fail because %2520 didn't translate into the space my server was expecting. While the issue apeared to be one of my encoding not working, it was really an issue of encoding too many times. I have an example from some working code in one of my projects below
//the Url of the server
String fullUrl = "http://myapiserver.com/path/";
//The parameter to append. contains a space that will need to be encoded
String param 1 = "parameter 1"
//Use Uri.Builder to append parameter
Uri.Builder uriBuilder = Uri.parse(fullUrl).buildUpon();
uriBuilder.appendQueryParameter("parameter1",param1);
/* Below is where it is important to understand how your
http framework handles unencoded url. In my case, which is Spring
framework, the urls are encoded when performing requests.
The result is that a url that is already encoded will be
encoded twice. For instance, if you're url is
"http://myapiserver.com/path?parameter1=param 1"
and it needs to be read by the server as
"http://myapiserver.com/path?parameter1=param%201"
it makes sense to encode the url using URI.builder().append, or any valid
solutions listed in other posts. However, If the framework is already
encoding your url, then it is likely to run into the issue where you
accidently encode the url twice: Once when you are preparing the URL to be
sent, and once again when you are sending the message through the framework.
this results in sending a url that looks like
"http://myapiserver.com/path?parameter1=param%25201"
where the '%' in "%20" was replaced with "%25", http's representation of '%'
when what you wanted was
"http://myapiserver.com/path?parameter1=param%201"
this can be a difficult bug to squash because you can copy the url in the
debugger prior to it being sent and paste it into a tool like fiddler and
have the fiddler request work but the program request fail.
since my http framework was already encoding the urls, I had to unencode the
urls after appending the parameters so they would only be encoded once.
I'm not saying it's the most gracefull solution, but the code works.
*/
String finalUrl = uriBuilder.build().toString().replace("%2F","/")
.replace("%3A", ":").replace("%20", " ");
//Call the server and ask for the menu. the Menu is saved to a string
//rest.GET() uses spring framework. The url is encoded again as
part of the framework.
menuStringFromIoms = rest.GET(finalUrl);
There is likely a more graceful way to keep a url from encoding twice. I hope this example helps point you on the right direction or eliminate a possability. Good luck.
Try this:
imageUrl = imageUrl.replaceAll(" ", "%20");
Replace spaces is not enought, try this
url = java.net.URLEncoder.encode(url, "UTF-8");
This question already has answers here:
XSS prevention in JSP/Servlet web application
(10 answers)
Closed 1 year ago.
My URL looks like the following:
http://example.com/value=S%0a%0d"><'script>alert(0)<'/script'>%20"
When I print value of the value request parameter in my Servlet, using the following code:
String Value=request.getParameter("value");
System.out.print("URL :"+Value);
The output is as follows:
URL :S
"><'script>alert(0)<'/script'>%20
It printed two lines (which makes this possibly vulnerable to XSS attacks). I tried the code below to replace the %0A%0D characters:
Value = value.replace('\n', ' ');
Value = value.replace('\r', ' ');
Value = value.replace("\n", "");
value = value.replace("\r", "");
value = value.replaceAll("0A", "");
value = value.replaceAll("0D", "");
value = value.replaceAll("%0A", "");
value = value.replaceAll("%0D", "");
value = value.replaceAll("%0A%0D", "");
But it only checks the first line of the output. How can I remove these characters from the URL, in order to mitigate XSS attacks?
To protect against XSS, I would advise against a character by character replace as you have used in the example snippet. You are bound to forget something from your list of characters you are substituting and you application may continue to be vulnerable.
Instead, I would recommend using:
https://www.owasp.org/index.php/OWASP_Java_Encoder_Project
This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
I want to validate url started with http/https/www/ftp and checks for /\ slashes and checks for .com,.org etc at the end of URL using regular expression. Is there any regex patttern for URL validation?
This works:
Pattern p = Pattern.compile("(#)?(href=')?(HREF=')?(HREF=\")?(href=\")?(http://)?[a-zA-Z_0-9\\-]+(\\.\\w[a-zA-Z_0-9\\-]+)+(/[#&\\n\\-=?\\+\\%/\\.\\w]+)?");
Matcher m = p.matcher("your url here");
I am use the following code for that
String lRegex = "^(https?|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]";
btw a search in google and you would find the solution by yourself.
Below is my regular expression :-
\\bhttps?://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]\\b
when the request url is of type http://www.example.com/ , the last character is not replaced in my shortner url and / is appended at end.
The regex is not able to find the last /.
Please help with this.
I think that / would be a word boundary, so maybe it works better if you add a ? to the and, so it reads:
\\bhttps?://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]\\b?
what about:
if(url.endsWith("/"))
url = url.substring(0,url.length()-1);
or if you need to use regular expressions you can do something like this:
url = url.replaceAll("(\\bhttps?://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*)/(\\b?)","$1$2");
If all you want is to replace the trailing / (which is what your question directly asks), you can simply do:
url = url.substring(0, url.lastIndexOf('/'));
Remember to KISS often.
You could simply use:
url = url.replaceAll("\/+$","");