Extract part of Dynamic Url? - java

I have the following URl http://127.0.0.1/?code=AQABAAIAAAAGV_bv21
I need to capture the chracters after code=
but every time the URL is loaded that code is different..
I had something like this but since its dynamic I can not do this..
String url = "http://127.0.0.1/?code=AQABAAIAAAAGV_bv21"
String code = url.substring(url.length() -10);

you can use something like below :-
String code = url.split("?code=")[1];

if you are on Android:
String url = "http://127.0.0.1/?code=AQABAAIAAAAGV_bv21"
Uri uri = Uri.parse(url);
String code = uri.getQueryParameter("code");
or try the following regexp:
(\?|\&)([^=]+)\=([^&]+)

Try this.
String url = "http://127.0.0.1/?code=AQABAAIAAAAGV_bv21";
String codeValue = url.replaceAll(".*code=([^&]*).*", "$1");
System.out.println(codeValue);
output:
AQABAAIAAAAGV_bv21
This method works even if other parameters are added. For example http://127.0.0.1/?id=123&code=AQABAAIAAAAGV_bv21&opt=yes

Related

Get values from URL in Java [duplicate]

This question already has answers here:
How to parse or split URL Address in Java?
(4 answers)
Closed 3 years ago.
I want to get "BusinessId" from this URL in Java: https://example.com/?link=https://play.google.com/store/apps/details?id=com.example.cp&hl=es&apn=com.picker.cp&st=Share+this+app&utm_source=AndroidApp?businessId=5d8648b561abf51ff7a6c189
What can i do?
I need some help, please :C
You could try using String#replaceAll for a one-line solution:
String url = "https://example.com/?link=https://play.google.com/store/apps/details?id=com.example.cp&hl=es&apn=com.picker.cp&st=Share+this+app&utm_source=AndroidApp?businessId=5d8648b561abf51ff7a6c189";
String businessId = url.replaceAll(".*[&?]businessId=([^=?&]+)\\b.*", "$1");
System.out.println(businessId);
This prints:
5d8648b561abf51ff7a6c189
Actually Apache has a number of libraries that can make handling your requirement much easier. On Android, the following might work:
Uri uri = Uri.parse(url);
String linkParam = uri.getQueryParameter("link");
Uri uri2 = Uri.parse(linkParam);
String businessId = uri2.getQueryParameter("businessId");
I noticed android tag in your question. You can parse the Url string to Uri and then you can get query param.
String myUrl = "https://example.com/?link=https://play.google.com/store/apps/details?id=com.example.cp&hl=es&apn=com.picker.cp&st=Share+this+app&utm_source=AndroidApp?businessId=5d8648b561abf51ff7a6c189";
Uri uri = Uri.parse(myUrl);
String businessId = uri.getQueryParameter("businessId");

Question about extracting parameter from URL inside WebDriver

I need help with extracting parameter from URL. I have used
String url = driver.getCurrentUrl()
to catch current URL.
After that, I need to extract value from the parameter called oauth_verifier from
http://localhost:4200/consent?oauth_token=something&oauth_verifier=something
so I can use it later in code.
Any help would be great. Thanks in advance!
Try This One , I Am not Sure Its Right Way , But Its Work...
String str = "http://localhost:4200/consent?oauth_token=something&oauth_verifier=something";
String[] arrOfStr = str.split("/");
String temp=arrOfStr[3];
String temp2[]=temp.split("\\?|\\=");
To extract the value associated with oauth_verifier you need to induce WebDriverWait with ExpectedConditions as urlContains() the text oauth_verifier and you can use the following solution:
Code Block:
new WebDriverWait(driver, 20).until(ExpectedConditions.urlContains("oauth_verifier"));
String fullUrl = driver.getCurrentUrl();
String[] UrlParts = fullUrl.split("=");
System.out.println(UrlParts[2]);
Console Output:
something
Assuming its Java ,you should use the substring function of String .
yourstring.substring(indexOf1stCharacter,indexOfLastCharacter)
For your case:
url.substring(url.indexOf("oauth_verifier=") + 15 , url.length());

How to read the public URL in GWT?

I m new in GWT and I m generating a web application in which i have to create a public URL.
In this public URL i have to pass hashtag(#) and some parameters.
I am finding difficulty in achieving this task.
Extracting the hashtag from the URL.
Extracting the userid from the URL.
My public URL example is :: http://www.xyz.com/#profile?userid=10003
To access the URL in GWT you can use the History.getToken() method. It will give you the entire string that follows the hashtag ("#").
In your case (http://www.xyz.com/#profile?userid=10003) it will return a string "profile?userid=10003". After you have this you can parse it however you want. You can check if it contains("?") and u can split it by "?" or you can get a substring. How you get the information from that is really up to you.
I guess you already have the URL. I'm not that good at Regex, but this should work:
String yourURL = "http://www.xyz.com/#profile?userid=10003";
String[] array = yourURL.split("[\\p{Lower}\\p{Upper}\\p{Punct}}]");
int userID = 0;
for (String string : array) {
if (!string.isEmpty()) {
userID = Integer.valueOf(string);
}
}
System.out.println(userID);
To get the parameters:
String userId = Window.Location.getParameter("userid");
To get the anchor / hash tag:
I don't think there is something, you can parse the URL: look at the methods provided by Window.Location.

Extract project name from URL

I have URL
../p/xmlProj/bugs/...
I'm looking for a way to extract the project name into single variable using java code
I tried this
String url = "/p/xmlProject/bugs/";
final String[] projName = url.split("/",3);
But i didn't work for me !!
You don't need this 3 there. Simply write:
final String[] projName = url.split("/");
And the project name will be projName[3].

url harvester string manipulation

I'm doing a recursive url harvest.. when I find an link in the source that doesn't start with "http" then I append it to the current url. Problem is when I run into a dynamic site the link without an http is usually a new parameter for the current url. For example if the current url is something like http://www.somewebapp.com/default.aspx?pageid=4088 and in the source for that page there is a link which is default.aspx?pageid=2111. In this case I need do some string manipulation; this is where I need help.
pseudocode:
if part of the link found is a contains a substring of the current url
save the substring
save the unique part of the link found
replace whatever is after the substring in the current url with the unique saved part
What would this look like in java? Any ideas for doing this differently? Thanks.
As per comment, here's what I've tried:
if (!matched.startsWith("http")) {
String[] splitted = url.toString().split("/");
java.lang.String endOfURL = splitted[splitted.length-1];
boolean b = false;
while (!b && endOfURL.length() > 5) { // f.bar shortest val
endOfURL = endOfURL.substring(0, endOfURL.length()-2);
if (matched.contains(endOfURL)) {
matched = matched.substring(endOfURL.length()-1);
matched = url.toString().substring(url.toString().length() - matched.length()) + matched;
b = true;
}
}
it's not working well..
I think you are doing this the wrong way. Java has two classes URL and URI which are capable of parsing URL/URL strings much more accurately than a "string bashing" solution. For example the URL constructor URL(URL, String) will create a new URL object in the context of an existing one, without you needing to worry whether the String is an absolute URL or a relative one. You would use it something like this:
URL currentPageUrl = ...
String linkUrlString = ...
// (Exception handling not included ...)
URL linkUrl = new URL(currentPageUrl, linkUrlString);

Categories

Resources