Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am developing a java web application and I want to know how to take a certain field (table and/or output-text) value from a certain website. Assuming that this component has always the same ID does anyone know how can I retrieve this information?
I don't know if anyone has ever faced this issue but if anyone has any idea please share.
Thank you.
In general:
1.) Retrieve the pages markup by reading it through an HTTPConnection to the URL in your application
2.) Parse the Markup using a framework like jsoup and retrieve the value you need.
More specifically, here is some example code for jsoup:
HttpClient http = new DefaultHttpClient();
String htmlcode = "";
HttpGet request = new HttpGet("http://www.example.com");
HttpResponse response = null;
try {
response = http.execute(request);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(response != null){
BufferedReader read = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while((line = read.readLine()) != null){
htmlcode += line;
}
}
// at this point we have the pages markup
Document doc = Jsoup.parse(htmlcode);
Elements lis = doc.getElementsByTag("li"); // get all entries in lists
for(Element el : lis){
String val = el.text().trim();
// do something for each list entry
}
You are talking about web scraping, check this library for php:
http://simplehtmldom.sourceforge.net/
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 days ago.
Improve this question
I tried to do "image = (PNMImage) ois.read(getBytes(fileName);" in order to fix the issue of converting string to byte but I got an error that said, cannot resolve getBytes in PNMimage
// TODO Implement this method
// return null;
PNMImage image = null;
try {
DataInputStream ois = new DataInputStream(new FileInputStream(fileName));
image = (PNMImage) ois.read(getBytes(fileName));
}catch(ClassNotFoundException e ) {
e.printStackTrace();
}
return image;
}```
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
i built by using java
a telegram bot
which basically get users's name
it can be 1 or 10 or even 40 (i limited it to 50) depends on how much you want to put
now the purpose of this bot is later on when you finish with your specific users
you write a message to send them
now everything is work perfect the id names getting save as well the message
although
how do i send it to all the users i just insert as a private message
if it even possible... by command
i hope i was understandable enough
thats my code:
class Bot extends TelegramLongPollingBot {
public int counter = 0;
public ArrayList names = new ArrayList(50);
public SendMessage mainMessage = new SendMessage();
public String sgMsg = "";
public StringBuilder stringBuilder = new StringBuilder();
public String msg;
public void onUpdateReceived(Update update) {
String command = update.getMessage().getText();
SendMessage sysMsg = new SendMessage();
sysMsg.setChatId(update.getMessage().getChatId());
String firstCdletter;
firstCdletter = Character.toString(command.charAt(0));
if (command.equals("/start")) {
sysMsg.setText("Enter the user's id, to finish send: Ok");
try {
execute(sysMsg);
} catch (TelegramApiException e) {
e.printStackTrace();
}
counter = 0;
names.clear();
sgMsg = "";
}else if (firstCdletter.equals("#")) {
String user = command;
names.add(counter);
counter++;
}else if(command.equals("/ok")){
sysMsg.setText("Good, now write your message you want to deliver");
try {
execute(sysMsg);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}else if(command.equals("/done")){
msg = stringBuilder.toString();
}else{
sgMsg = update.getMessage().getText();
stringBuilder.append(sgMsg + " ");
}
}
thank you all for your time and help
Unfortunately, you cannot send message via #username, the only identify you can use is UID (looks like 109780439).
And by the way, bot have to chat with that user before, or you will got an 400 Error.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am facing issue for validate the URL pattern like ER.RTR.RT12345,its return me true .But it is works fine for https://www.sophos.com/cs-cz/support/knowledgebase/117316.aspx this.
public static boolean validateURL(String url) {
String urlPattern = "(#)?(href=')?(HREF=')?(HREF=\")?(href=\")?(http://)?(https://)?[a-zA-Z_0-9\\-]+(\\.\\w[a-zA-Z_0-9\\-]+)+(/[#&\\n\\-=?\\+\\%/\\.\\w]+)?";
if (url.matches(urlPattern))
return true;
else
return false;
}
How to resolves this issue ?
Java's URL class automatically "validates" a URL string. The validation is according to
The syntax of URL is defined by RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax, amended by RFC 2732: Format for Literal IPv6 Addresses in URLs.
You can just use the constructor:
public static void main(String[] args) {
try {
URL url = new URL("ER.RTR.RT12345");
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
and manage true or false with the catch block. The above example throws the exception.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So I have this rather weird problem with my program. What I try to do is to check the Content-Length of a URL and then use this number in a non-static method with an if-else statement connecting other classes. But my problem with this is that while I was researching the internet to somehow solve this problem, there where these static-methods that I could use to get the contentLength and it wouldn't work with non-static. So I tried it but I never really got any results. What I basically tried to do is to call the class with the static-method via non-static-method in another class which, I guess, shouldn't be the problem (or should? I don't really know since I have never worked with static methods before). Then I used this code to determine the contentLength of a random webpage with changing date:
public static void main() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 1); // Adding 1 day
String output = sdf.format(c.getTime());
try {
String urlStr = "http://www.sasintern.de/show_pdf.php?fn=" + output + ".pdf";
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int contentLength = httpConn.getContentLength();
if (contentLength < 2000) {
new NotWorkingActivity();
System.out.println("unknown content length");
} else {
new download2Activity();
System.out.println("content length: " + contentLength + " bytes");
}
InputStream inStream = httpConn.getInputStream();
// now read data
// ...
// close connection
httpConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Afterwards, when the size of the page was determined to be smaller than 2000 bytes, it should show a Toast in another class that the page is not available, but when it was bigger, then it should open up a new class, as you may see in the else-statement.
But it doesn't work and I think I just messed up with static methods. I'm just not that experienced with them. Sorry for any other mistakes that I might have made. If there is an different solution to my problem which, hopefully, is easier to understand and better to use, I would be very grateful.
Please, I need help on this problem and I hope you understand what I am up to.
Thanks in advance :)
Read the manual
http://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#getContentLength()
public int getContentLength()
Returns the value of the content-length header field.
Not all resources set the Content-Length header. So, you need to obtain input stream and read all the bytes until the end to count them.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a code that is running on NodeJs. We would like to change the technology (to java).
The problem is we have some existing passwords, and I am not sure how to I copy the encryption logic to java.
So, one of the possible solutions is to run the encryption logic in javascript (e.g. command line, embbeded in the java, etc) and get the result back.
The question is - how do I do that?
The nodejs code goes like this:
crypto = require('crypto');
this.salt = this.makeSalt();
encryptPassword: function(password) {
var salt = new Buffer(this.salt, 'base64');
return crypto.pbkdf2Sync(password, salt, iterations, keylen).toString('base64');
crypto.randomBytes(..)
}
makeSalt: function() {
return crypto.randomBytes(numOfBytes).toString('base64');
},
UPDATE:
Following the suggestions here, I added the full code. If the right way of doing it is by transforming the javascript code to java code, can you please help me translated the above code?
You should not do this, if you want random bytes in Java do this. You should be able to replicate the encryption logic in Java.
byte[] b = new byte[20];
new Random().nextBytes(b);
Almost all of the Node.js crypto functions are generic, and should have their own Java counterparts or 3rd party libraries.
Update
If you must run your node code via java you can add this method
public static String runCommand(String command) {
String output = "";
try {
String line;
Process process = Runtime.getRuntime().exec( command );
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()) );
while ((line = reader.readLine()) != null) {
output += line;
}
reader.close();
} catch (Exception exception) {
// ...
}
return output;
}
and run it like this
String encryptedPassword = runCommand("node myEncryption.js --password=1234");