java print the redirected url - java

original url : http://pricecheckindia.com/go/store/ebay/52440?ref=velusliv
redirected url : http://www.ebay.in/itm/Asus-Zenfone-6-A600CG-A601CG-White-16-GB-/111471688863?pt=IN_Mobile_Phones&aff_source=DA
I need a program that will take the original url and print the redirected url.
How to get this done in java.
public static void main(String[] args) throws IOException, InterruptedException
{
String url = "http://pricecheckindia.com/go/store/ebay/52440?ref=velusliv";
Response response = Jsoup.connect(url).followRedirects(false).execute();
System.out.println(response.url());
}

It seems that you are being redirected via JavaScript code, which Jsoup doesn't support (it is simple HTML parser, not browser emulator). Your choice then is to either use tool which will support JavaScript like Selenium web driver, or parse your page to get url from click here link from
If it is taking too long to redirect, then please click here
text.
You can use Jsoup to get this link by adding to your current code
Document doc = response.parse();
String redirectUrl = doc.select("a:contains(click here)").attr("href");
System.out.println(redirectUrl);
which will return and print
http://rover.ebay.com/rover/1/4686-127726-2357-15/2?&site=Partnership_PRCCHK&aff_source=DA&mpre=http%3A%2F%2Fwww.ebay.in%2Fitm%2FAsus-Zenfone-6-A600CG-A601CG-White-16-GB-%2F111471688863%3Fpt%3DIN_Mobile_Phones%26aff_source%3DDA
so now all we need to do is parse query from this URL to get value of mpre key, which encoded version looks like
http%3A%2F%2Fwww.ebay.in%2Fitm%2FAsus-Zenfone-6-A600CG-A601CG-White-16-GB-%2F111471688863%3Fpt%3DIN_Mobile_Phones%26aff_source%3DDA
but after decoding it will actually represents
http://www.ebay.in/itm/Asus-Zenfone-6-A600CG-A601CG-White-16-GB-/111471688863?pt=IN_Mobile_Phones&aff_source=DA
To get value of this key and decode it you can use one of solutions from this question: Parse a URI String into Name-Value Collection. With help of method from accepted answer in previously mentioned question we can just invoke
URL address = new URL(redirectUrl);
Map<String,List<String>> urlQuerryMap= splitQuery(address);
String redirected = urlQuerryMap.get("mpre").get(0);
System.out.println(redirected);
to see result
http://www.ebay.in/itm/Asus-Zenfone-6-A600CG-A601CG-White-16-GB-/111471688863?pt=IN_Mobile_Phones&aff_source=DA

Related

What URL do I use to open a String object in a web browser

If I have a HTML String object, using Selenium in Java, how can I get the browser to open that String as a HTML page? I have seen this done before but I don't remember the format that the URL needs to be.
For this example, let's say the string is :
<h2>This is a <i>test</i></h2>
I looked through this page and couldn't find the answer but I might be overlooking it. For example I tried this URL and it didn't work for me:
data:<h2>This is a <i>test</i></h2>
Here is a link for documentation http://en.wikipedia.org/wiki/Data_URI_scheme. You need to specify MIME-type of data. Try data:text/html,<h2>This is a <i>test</i></h2>

Using Jsoup to extract text from specific class

i'm trying to work with a little android at the moment.
Before all the hate - Yes I have tried searching and found answers related to mine but I simply couldnt get mine to work with the way they did it :/.
I have found out that Jsoup is rather good for parsing data from HTML to use in an app.
So I have trying to recieve data from here Krak
So when I enter the input for a number lets say "86202710"
The link will be Number link
I have then tried to extract the name of the owner of the given number which is "Jens Fisker Automobiler A/S". But I cant seem to get this text out into my textview.
Hope you guys can guide me alittle...
I get an exception "NetworkOnMainThreadException" - AndroidBlockGuardPolicy.onNetwork"
Here is the code I have written to the method for extracting the owner of the number
public void getData() throws IOException{
URL url = new URL("http://mobil.krak.dk/h/#companyResult&searchWord=86202710");
Document doc = Jsoup.parse(url, 3000);
Element content = doc.select("p[header bold]").first();
text = (TextView) findViewById(R.id.tv);
text.setText(content.text());
}
You have to run your networkcode in a AsyncTask on Android. Everything else will fail.
See here: Error when parsing Html using Jsoup
Do you connect through a proxy?
Please check if you have the settings like posted here: Android NetworkOnMainThreadException inside of AsyncTask
Btw. better use connect() method than parse() for such connections:
public void getData() throws IOException{
// You can use a simple string as url
final String url = "http://mobil.krak.dk/h/#companyResult&searchWord=86202710";
// Connect to url and parse it's content
Document doc = Jsoup.connect(url).get(); // Timeout is set to 3 Sec. per default
// Everything else stays same
Element content = doc.select("p[header bold]").first();
text = (TextView) findViewById(R.id.tv);
text.setText(content.text());
}

Open a link from Java, how to hide GET parameter

I want to open a link from Java I tried this
public static void main(String[] args) {
try {
//Set your page url in this string. For eg, I m using URL for Google Search engine
String url = "http://myurl.com?id=xx";
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
}
catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
}
It is working fine but the problem is that the query string is in that url. I don't want to pass it as a query string because it is a secret key. It should be passed as hidden to webpage request. How can I do this?
You can't, directly
You'd need to use a POST instead of a GET to hide the value and the URL does not encode the method used to access it, so it will always use GET.
You could conceivably write out a HTML file that automagically does a POST to the desired URL (using some JavaScript) and open that (using a file:// URL).
But note that "hiding" the parameter like this adds no real security! An interested user that wants to know the value that his PC sends to some site will be able to see it. It might take slightly more effort to find it, but it's definitely not impossible.
If there is no need to show the particular url in a browser, then you could handle the link as an HttpURLConnection (see JavaDoc).
And here you have an example.

Extract a randomly generated ID from URL

I need to extract a randomly generated part of an URL for a Selenium Test in Java.
When the browser opens a page, e.g.:
/edit_person.html?id=eb58cea3a3772ff656987792eb0a8c0f
then I'm able to show the url with:
String url = driver.getCurrentUrl();
but now I need to get only the randomly generated ID after the equals sign.
How do I extract the value of parameter id once I have the entire URL as a string in variable url?
URL.getQuery() will give the query portion as a String it is a simple regular expression match to isolate the part you want.
id=(.*) will get you what you want as long as it is the only thing in the query string.
This is how managed to solve the problem:
String url = driver.getCurrentUrl();
URL aURL = new URL(url);
url = aURL.getQuery();
String[] id = url.split("=");
System.out.println(id[1]);
Thanks to Jarrod Roberson!

Returned URL as String is not valid in JSF

I'm trying to make use of google api as text-to-speech. So, I build a String then should pass it as a URL to a component to obtain a MP3 with the spoken words.
So, this is my code:
URI uri = new URI("http://translate.google.com/translate_tts?tl=es&q="+ URLEncoder.encode((String)this.text.getValue(), "UTF-8"));
When I make uri.toString() its return a well formed URL. If I copy and paste this output in the browser works pefectly.
But if I assign this returned String to the source property of a ice:outputMedia is not working. Then inspect the HTML generated in the page and the String in src property is:
http://translate.google.com/translate_tts?tl=es&q=Bobby+need+peanuts
The & symbol has been replaced by &.
How can I avoid this to make a valid URL?
You need to decode the url on the client side using Javascript.
var decoded = decodeURI(URI)

Categories