fill a form in a asp dynamic page with htmlunit - java

I'm making a little script in java to check iPhone IMEI numbers.
There is this site from Apple :
https://appleonlinefra.mpxltd.co.uk/search.aspx
You have to enter an IMEI number. If this number is OK, it drives you to this page :
https://appleonlinefra.mpxltd.co.uk/Inspection.aspx
Else, you stay on /search.aspx page
I want to open the search page, enter an IMEI, submit, and check if the URL has changed. In my code there is a working IMEI number.
Here is my java code :
HtmlPage page = webClient.getPage("https://appleonlinefra.mpxltd.co.uk/search.aspx");
HtmlTextInput imei_input = (HtmlTextInput)page.getElementById("ctl00_ContentPlaceHolder1_txtIMEIVal");
imei_input.setValueAttribute("012534008614194");
//HtmlAnchor check_imei = page.getAnchorByText("Rechercher");
//Tried with both ways of getting the anchor, none works
HtmlAnchor anchor1 = (HtmlAnchor)page.getElementById("ctl00_ContentPlaceHolder1_imeiValidate");
page = anchor1.click();
System.out.println(page.getUrl());
I can't find out where it comes from, since i often use HTMLUnit for this and i never had this issue. Maybe because of the little loading time after submiting ?
Thank you in advance

You can do this by using a connection wrapper that HTMLUnit provides
Here is an example
new WebConnectionWrapper(webClient) {
public WebResponse getResponse(WebRequest request) throws IOException {
WebResponse response = super.getResponse(request);
if (request.getUrl().toExternalForm().contains("Inspection.aspx")) {
String content = response.getContentAsString("UTF-8");
WebResponseData data = new WebResponseData(content.getBytes("UTF-8"), response.getStatusCode(),
response.getStatusMessage(), response.getResponseHeaders());
response = new WebResponse(data, request, response.getLoadTime());
}
return response;
}
};
With the connection wrapper above, you can check for any request and response that is passing through HTMLUnit

Related

How to build a short Dynamic Link with a Desktop Fallback URL on Android?

I have implemented deep link in my Android App to share content. The problem is on Android I can't find a way to set a Fallback URL when the user open the short link on his desktop.
With the Firebase DynamicLink.Builder I can set iOS fallback URL because my app doesn't exist on iOS but I can't find a way to set the dfl parameters in my link.
Which lead the user to an error page like this :
Here how I build my short dynamic link:
//link example : https://app.example.com/details/ebLvAV9fi9S7Pab0qR3a
String link = domainUri + "/details/" + object.getUid();
FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse(link))
.setDomainUriPrefix(domainUri)
.setAndroidParameters(new DynamicLink.AndroidParameters.Builder().setMinimumVersion(1).build())
// Fallback Url for iOS
.setIosParameters(new DynamicLink.IosParameters.Builder("").setFallbackUrl(Uri.parse(RMP_WEB_BASE_URL)).build())
.setSocialMetaTagParameters(
new DynamicLink.SocialMetaTagParameters.Builder()
.setTitle(title)
.setDescription(description)
.setImageUrl(Uri.parse(imageUrl))
.build())
.buildShortDynamicLink()
.addOnCompleteListener(new OnCompleteListener<ShortDynamicLink>() {
#Override
public void onComplete(#NonNull Task<ShortDynamicLink> task) {
if (task.isSuccessful() && task.getResult() != null) {
shortLink = task.getResult().getShortLink();
//Create Shareable Intent
//...
}
}
});
I have read that I need to specify a Desktop Fallback URL like the iOS one but DynamicLink.Builder doesn't seems to include one.
I would like to redirect my user to the home page https://example.com when they open the link from a non-android device.
I have tried to use setLongLink(longLink) in the DynamicLink.Builder with the parameters ?dfl=https://example.com but it doesn't seems to work and it even break my dynamic link on android.
This is a Swift solution but may be helpful to others-
Unfortunately, there is currently no built-in method to handle this programmatically through the Firebase url editor. You must manually add an 'ofl' parameter to the link. The easiest way to do this:
// Grab link from Firebase builder
guard var longDynamicLink = shareLink.url else { return }
// Parse URL to string
var urlStr = longDynamicLink.absoluteString
// Append the ofl fallback (ofl param specifies a device other than ios or android)
urlStr = urlStr + "&ofl=https://www.google.com/"
// Convert back to a URL
var urlFinal = URL(string: urlStr)!
// Shorten the url & check for errors
DynamicLinkComponents.shortenURL(urlFinal, options: nil, completion:{ [weak self] url,warnings,error in
if let _ = error{
return
}
if let warnings = warnings{
for warning in warnings{
print("Shorten URL warnings: ", warning)
}
}
guard let shortUrl = url else {return}
// prompt the user with UIActivityViewController
self?.showShareSheet(url: shortUrl)
})
The final URL can then be used to present the shareable panel with another function like:
self.showShareSheet(url: finalUrl) which triggers the UIActivityViewController
Credit to http://ostack.cn/?qa=168161/ for the original idea
More about ofl: https://firebase.google.com/docs/dynamic-links/create-manually?authuser=3#general-params

Simple login with Jsoup not working

I'm trying to do a simple login to a website and at the end I print out the title to check if it's logged in, however for some reason I keep getting the title of the login screen.
Response res = Jsoup
.connect("http://moj.tvz.hr")
.data("login", "gost", "passwd", "gost")
.method(Method.POST)
.execute();
Map<String, String> cookies = res.cookies();
Document subjectPage = Jsoup.connect("https://moj.tvz.hr")
.cookies(cookies)
.get();
String subjectTitle = subjectPage.title();
System.out.println("##### Printing webpage title #####\n" + subjectTitle + "\n");
Testing login on the actual website works just fine with the user/pw combination, so I assume something is wrong with the rest of the code, but I can't seem to find what.
If you examine what data are send with a form request, for example with browser debugging tool you will find out, that for this site there is additional parameter TVZ. It is generated for your initial request. You have to parse it out and then add to login form request.
When you are connecting to other pages you have to add TVZ as a parameter to your request. Also you have to use cookies from initial request, because login response does not return any.
See code below.
Response initResponse = Jsoup.connect("http://moj.tvz.hr").execute();
Document doc = initResponse.parse();
String tvz = doc.select("input[name=TVZ]").attr("value");
Map<String, String> cookies = initResponse.cookies();
Response res = Jsoup.connect("https://moj.tvz.hr").data("login", "gost", "passwd", "gost")//
.data("TVZ", tvz)//
.cookies(cookies)//
.method(Method.POST).execute();
System.out.println("##### Printing webpage title #####\n" + res.parse().title() + "\n");
Document subjectPage = Jsoup.connect("https://moj.tvz.hr").data("TVZ", tvz).cookies(cookies).get();
String subjectTitle = subjectPage.title();
System.out.println("##### Printing webpage title #####\n" + subjectTitle + "\n");

Can't get JSON from a GET request

New to Play! Framework and web development in general, I'm trying to do a simple REST GET to a web service and just get some straight-forward JSON in response. Typing the URL into a browser, I get a perfect response, with nicely formatted JSON. Calling it via code, it just blows up:
WS.WSRequest wsRequest = WS.url( serviceURL );
wsRequest.timeout( timeoutTime );
wsRequest.setHeader("Accept", "application/json");
wsRequest.headers.put( "Content-type","application/json" );
wsRequest.mimeType = "application/json";
WS.HttpResponse response = wsRequest.get();
String graphServiceResponse = response.getJson().toString();
Everything executes fine, until the last line where it throws an exception and errors out. I know I have what looks like a lot of redundant code; those are my attempts to fix it. Like I said, typing the "serviceURL" into a browser, it works fine.
Anyone know what I'm doing wrong? Thanks in advance!
Okay, solved this. Just omitted all the sets and such, added authentication and it worked perfectly. Weird.
String stringResponse = "";
try {
// execute GET to graph service
WS.WSRequest wsRequest = WS.url( serviceURL ).authenticate( USERNAME, PASSWORD );
WS.HttpResponse response = wsRequest.get();
stringResponse = response.getString();
... more cool stuff ...
Thanks for looking!
I have tried and found this below code working. 10000 is the timeout parameter in ms.
String baseUrl = "your url";
F.Promise<WSResponse> response = ws.url(baseUrl)
.setQueryParameter(param, value)
.get();
return response.get(10000).asJson().toString();

Input values to textfields, stream back to website

I connected to a website, used JSoup to find the "textfield" ID's, input the values, now i need to stream it out.
Can someone please help me with the correct coding to stream the "modified" doc back to the website?
if (source == enter2)
{
String URL = "http://www.clubvip.co.za/Login.aspx";
Element number;
Element pass;
Element keyword;
try {
Document doc = Jsoup.connect(URL).get();
number = doc.getElementById("ctl00_ContentPlaceHolder1_CellNumberRadText").attr("value", "number");
System.out.println(number);
pass = doc.getElementById("ctl00_ContentPlaceHolder1_PasswordRadText").attr("value", "password");
System.out.println(pass);
keyword = doc.getElementById("ctl00_ContentPlaceHolder1_KeyWordRadText").attr("value", "keyword");
System.out.println(keyword);
Why are you doing it like this?
If you need to login to that webpage, simply take arguments and send them via HTTP POST request to page where <form> points to
which is <form method="post" action="login.aspx">
Instead of what you are doing:
Jsoup.connect("http://www.clubvip.co.za/Login.aspx")//
.data("ctl00_ContentPlaceHolder1_CellNumberRadText", "number",
"ctl00_ContentPlaceHolder1_PasswordRadText", "password",
"ctl00_ContentPlaceHolder1_KeyWordRadText", "password").post();
not tested, so possibly not 100% correct...

Writing a simple web crawler that interacts with the browser (Java)

I need to create an automated process (preferably using Java) that will:
Open browser with specific url.
Login, using the username and password specified.
Follow one of the links on the page.
Refresh the browser.
Log out.
This is basically done to gather some statistics for analysis. Every time a user follows the link a bunch of data is generated for this particular user and saved in database. The thing I need to do is, using around 10 fake users, ping the page every 5-15 min.
Can you tink about simple way of doing that? There has to be an alternative to endless login-refresh-logout manual process...
Try Selenium.
It's not Java, but Javascript. You could do something like:
window.location = "<url>"
document.getElementById("username").value = "<email>";
document.getElementById("password").value = "<password>";
document.getElementById("login_box_button").click();
...
etc
With this kind of structure you can easily cover 1-3. Throw in some for loops for page refreshes and you're done.
Use HtmlUnit if you want
FAST
SIMPLE
java based web interaction/crawling.
For example: here is some simple code showing a bunch of output and an example of accessing all IMG elements of the loaded page.
public class HtmlUnitTest {
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://www.google.com");
System.out.println(page.getTitleText());
for (HtmlElement node : page.getHtmlElementDescendants()) {
if (node.getTagName().toUpperCase().equals("IMG")) {
System.out.println("NAME: " + node.getTagName());
System.out.println("WIDTH:" + node.getAttribute("width"));
System.out.println("HEIGHT:" + node.getAttribute("height"));
System.out.println("TEXT: " + node.asText());
System.out.println("XMl: " + node.asXml());
}
}
}
}
Example #2 Accessing named input fields and entering data/clicking:
final HtmlPage page = webClient.getPage("http://www.google.com");
HtmlElement inputField = page.getElementByName("q");
inputField.type("Example input");
HtmlElement btnG = page.getElementByName("btnG");
Page secondPage = btnG.click();
if (secondPage instanceof HtmlPage) {
System.out.println(page.getTitleText());
System.out.println(((HtmlPage)secondPage).getTitleText());
}
NB: You can use page.refresh() on any Page object.
You could use Jakarta JMeter

Categories