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...
Related
We are getting url from JSON Response and which we open in in Chrome.The page loads , there is submit button which we click then it redirect to url as :-
https://www.google.com/AB1234
We need the need to retrieve only "AB1234" value from url.
tried following code to get value ="AB1234"
String url = driver.getCurrentUrl();
int index=url.lastIndexOf("/");
String result = url.substring(0,index);
but here getting initial part of url:https://www.google.com/
You need to call substring function with index +1 .
Try below code :
String url = driver.getCurrentUrl();
int index = url.lastIndexOf("/");
String result = url.substring(index + 1);
To parse a URI, it's likely a good idea to use a URI parser.
Given http://example.com/bar
String path = URI.create(driver.getCurrentUrl()).getPath();
will get you '/bar'.
Given http://example.com/bar/mumble the same code gets '/bar/mumble'. It's unclear from your question whether this is what you want. Nevertheless, you should at least start the parse as above.
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
I'm trying to send some data to a servlet and then to get back a .xls file from it. In order to do this, I'm using jquery, but I'm facing some strange issues. Let me explain.
Here is how I'm sending the data to the servlet and how I'm supposed to get the generated file back:
jQuery.download = function(url, data, method){
//url and data options required
if( url && data ){
//data can be string of parameters or array/object
data = typeof data == 'string' ? data : jQuery.param(data);
//split params into form inputs
var inputs = '';
jQuery.each(data.split('&'), function(){
var pair = this.split('=');
inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />';
});
//send request
jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
.appendTo('body').submit().remove();
};
};
download = function () {
var a = this.mainData();
var b = JSON.stringify(a);
console.log(b);
what = "test",
obj = $.extend({WrJOB: "xlsExport", mainData: b}, tJS.getCommonPostData());
var data = $.param(obj); //.replace(/\+/g, '%20'); its just a test
$.download('/myapp/AppProxy', data);
},
A button in my html is calling the download function wich is sending some JSON data to the servlet. In my case it is var b.
I'm pretty sure that there is an encoding issue, but I have no idea how to fix it.
Please, help me with this strange problem, I'm already working many hours on it and I can not find a solution.
You should unescape your output at some point. I would advise to do it on servlet side.
It looks like the servlet is receiving it encoded for a URL. You might be able to decode it on the servlet side if you have control over the code on the servlet.
For instance, in PHP, using urldecode()
Hope this helps.
like this
$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;while(x<c.length){var m=r.exec(c.substr(x)); if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length; }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16); o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}});
jQuery.each(data.split('&'), function(){
var pair = this.split('=');
inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ jQuery.URLDecode(pair[1]) +'" />';
});
The problem is that you urlencode your data twice. First explicitly in your javascript, then implicitly when creating the form. The browser will be "nice" to you and urlencode the input parameters before doing the request.
Either decode the parameters before adding them as input values or change the way you build your data to avoid the explicit encoding.
I am creating an app in Java that will take all the information from a public website and load it in the app for people to read using jsoup. I was trying the same kind of function with Facebook but it wasn't working the same way. Does anyone have a good idea about how I should go about this?
Thanks,
Calland
public String[] scrapeEvents(String... args) throws Exception {
Document doc = Jsoup.connect("http://www.facebook.com/cedarstreettimes?fref=ts").get();
Elements elements = doc.select("div._wk");
String s = elements.toString();
return s;
}
edit: I found this link of information,but I'm a little confused on how to manipulate it to get me only the content of what the specific user posts on their wall. http://developers.facebook.com/docs/getting-started/graphapi/
I had a look at the source of that page -- the thing that is tripping up the parse is that all the real content is wrapped in comments, like this:
<code class="hidden_elem" id="u_0_42"><!-- <div class="fbTimelineSection ...> --></code>
There is JS on the page that lifts that data into the real DOM, but as jsoup doesn't execute JS it stays as comments. So before extracting the content, we need to emulate that JS and "un-hide" those elements. Here's an example to get you started:
String url = "https://www.facebook.com/cedarstreettimes?fref=ts";
String ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.33 (KHTML, like Gecko) Chrome/27.0.1438.7 Safari/537.33";
Document doc = Jsoup.connect(url).userAgent(ua).timeout(10*1000).get();
// move the hidden commented out html into the DOM proper:
Elements hiddenElements = doc.select("code.hidden_elem");
for (Element hidden: hiddenElements) {
for (Node child: hidden.childNodesCopy()) {
if (child instanceof Comment) {
hidden.append(((Comment) child).getData()); // comment data parsed as html
}
}
}
Elements articles = doc.select("div[role=article]");
for (Element article: articles) {
if (article.select("span.userContent").size() > 0) {
String text = article.select("span.userContent").text();
String imgUrl = article.select("div.photo img").attr("abs:src");
System.out.println(String.format("%s\n%s\n\n", text,imgUrl));
}
}
That example pulls out the article text and any photo that is associated with it.
(It's possibly better to use the FB API that this method; I wanted to show how you can emulate little bits of JS to make a scrape work properly.)
I am retrieving all the contacts of a user from gmail and yahoo, I have added the checkbox, the user needs to select the desired email id to which he need to send email. I need to collect all user selected email id's and save them in a string send that string to another servlet where I am sending emails.
I was able to add the check box dynamically but I am not able to collect the emails and save them in a string. This is the code I have written to add check box before all the emails,
kindly help me to put those selected email id's in a string
I used the following code, but still I am not able to do it.You can have a look at the demo of this app http://ec2-50-16-183-101.compute-1.amazonaws.com/SocialAuthNew/
To get the contacts from Gmail type google in the text box and for yahoo type yahoo and click on submit button
List<Contact> contactsList = provider.getContactList();
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><script type='text/javascript'>");
out.println("function getAllContacts(size){ var selected_list='';");
out.println("for(var c=0;c<size;c++){if(document.getElementById('mailCheckbox'+c).checked==true){selected_list=selected_list+document.getElementById('lblmail'+c).innerHTML+':';}}");
out.println("document.getElementById('final_mailing_list').innerHTML=selected_list;}</script>");
out.println("<title></title>");
out.println("</head>");
out.println("<body>");
for(int i=0;i<contactsList.size();i++){
System.out.println(contactsList.get(i).getFirstName()+" : "+contactsList.get(i).getLastName()+":"+contactsList.get(i).getEmail());
out.println("<h1> Imported conatcts from your mail are:-</h1>");
out.println("<input type='checkbox' id='mailCheckBox"+i+"' name='mailCheckbox'></input>");
/* out.println(contactsList.get(i).getFirstName());
out.println(contactsList.get(i).getLastName());*/
out.println("<label id='lblmail"+i+"'>"+contactsList.get(i).getEmail()+"</label>");
}
int size=contactsList.size();
out.println("<input type='button' value='GetContact' onclick='getAllContacts("+size+");'/> ");
out.println("<div id='final_mailing_list'></div></body>");
out.println("</html>");
}
Try this:
1) Wrap your email in a DOM element to make it easier to access
out.println("<span>" + contactsList.get(i).getEmail() + "</span>");
2) Using something like e.g. JQuery for normalizing access to the DOM on the client, do
function getSelectedEmails() {
var emails = [];
$('body').find('input[name="mailCheckbox"]:checked').each(function() {
emails.push($(this).closest('span').text());
});
return emails;
}
This returns the emails in an array - which you can easily concatenate into a string if you want with e.g.
var emailString = emails.join(", ");
...although I think using an array is usually better (perhaps JSON encoded if you need to serialize it).
s using array is much easier. i have used array and gave the check box name as check1 and on the click of submit button i have called the following function. this function alerts the value of the selected check boxes and passes the action to servlet
<script>
function onCallForInterview()
{
var selectedIds;
var count=0;
for (i=0; i<document.frm.check1.length; i++)
{
if (document.frm.check1[i].checked==true)
{
if(count==0){
selectedIds=document.frm.check1[i].value;
count=count+1;
}
else
selectedIds=selectedIds+","+document.frm.check1[i].value;
}
}
alert(selectedIds);
document.frm.action="<%=contextPath%>/SearchCandInfo? action=selectcanforinterview&ids="+selectedIds;
document.frm.submit();
}
</script>
A slightly more primitive way to achieve this,
Create a bunch of Check-Box as you are doing now but with the difference that all of htem should have the same name i.e. do the following correction in your code
out.println("<input type='checkbox' id='mailCheckBox' name='mailCheckbox'></input>");
Now retrieve all the values of such text boxes on your server side using following call on request object,
String[] emailIds = request.getParameterValues("mailCheckBox");
Hope this helps.