Java - Logging into a website in order to retrieve data - java

What I am attempting to do is:
Login to a website in order to retrieve data that can only be accessed while logged on.
The website I need to login to is https://www.indemed.com.
I think that this is a two part program, part 1 being logging in, while part 2 is getting the information. When I run the login part of my program and then attempt to manually log in it says my account is in use, which I take to mean it is correctly logging in.
However when I try to get the price it is not there (if not logged in prices will not show up, but everything else will be there).
My questions are: Is there a problem with how I am combining my logging method and my retrieving method? Is the problem just with my logging method? Is the problem with just my retrieving method? Why doesn't this work? Most importantly, how can I fix this?
Here is what I have attempted so far:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class IndependenceMedical {
public IndependenceMedical(){
login();
}
private void login() {
URL URLObj;
URLConnection connect;
try {
// Establish a URL and open a connection to it. Set it to output mode.
URLObj = new URL("https://www.indemed.com/Action/Login/LoginAction.cfm?Refer=/index.cfm");
connect = URLObj.openConnection();
System.out.println(connect.toString());
connect.setDoOutput(true);
// Create a buffered writer to the URLConnection's output stream and write our forms parameters.
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
writer.write("AccountNumber=12345&UserName=myUserName&Password=myPassword&Login=Login");
writer.close();
// Now establish a buffered reader to read the URLConnection's input stream.
BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
String lineRead = "";
// Read all available lines of data from the URL and print them to screen.
while ((lineRead = reader.readLine()) != null) {
System.out.println(lineRead);
}
reader.close();
}
catch (MalformedURLException ex) {
System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
System.exit(1);
}
catch (Exception ex) {
System.out.println(ex.getMessage() + "\nAn exception occurred.");
System.exit(1);
}
}
public Document getDoc(String itemNumber){
try {
return Jsoup.connect("https://www.indemed.com/Catalog/SearchResults.cfm?source=advancedSearch&psku=" + itemNumber + "&keyword=&PHCPCS=&PClassID=&ManufacturerID=&Search.x=41&Search.y=9").get();
}
catch (IOException e) {}
return null;
}
public String getPrice(Document doc){
try{
Elements stuff = doc.select("#tr_51187955");
stuff = stuff.select("div.product-price");
String newStuff = stuff.toString();
newStuff = newStuff.substring(newStuff.indexOf("$")); // throws exception because "$" is not in the String.
newStuff = newStuff.substring(0, newStuff.indexOf(" "));
return newStuff;
}
catch (Exception arg0){
return "";
}
}
public static void main(String[] args){
IndependenceMedical test = new IndependenceMedical();
Document doc = test.getDoc("187955");
System.out.println("\n\n\n\n\n\n\n\n\n\n"); //to separate the return lines
System.out.println(test.getPrice(doc));
}
}
Due to character restrictions and the fact that I don't know which parts are important, I can't show the output. However if requested I will try to provide all the requested output.
Sorry for being so wordy I'm just trying to make sure the question is clear.
Lastly I have thoroughly looked through other login questions and although there are examples of how to login, I can't seem to find how to do anything after logging in (i'm sure someone has talked about it, but I haven't been able to find it).
Thanks in advance to anyone that can help me with this.
EDIT:
Although this question is similar to Parse HTML source after login with Java
I'm not parsing the redirected page, I need access to all pages this grants access to.

Jsoup provides the methods for login mechanisms.
Try the below, after you've filled the username, password and account number.
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Map;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class IndependenceMedical {
private Map<String, String> loginCookies;
public IndependenceMedical() {
login();
}
private void login() {
try {
Connection.Response res = Jsoup.connect("https://www.indemed.com/Action/Login/LoginAction.cfm?refer=MyAccount&qs=")
.data("UserName", "myUserName")
.data("Password", "myPassword")
.data("AccountNumber", "myAccountNumber")
.method(Method.POST)
.execute();
loginCookies = res.cookies();
} catch (MalformedURLException ex) {
System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
System.exit(1);
} catch (Exception ex) {
System.out.println(ex.getMessage() + "\nAn exception occurred.");
System.exit(1);
}
}
public Document getDoc(String itemNumber){
try {
return Jsoup.connect("https://www.indemed.com/Catalog/SearchResults.cfm?source=advancedSearch&psku=" + itemNumber + "&keyword=&PHCPCS=&PClassID=&ManufacturerID=&Search.x=41&Search.y=9")
.cookies(loginCookies)
.get();
} catch (IOException e) {}
return null;
}
public String getPrice(Document doc){
try {
Elements stuff = doc.select("#tr_51187955");
stuff = stuff.select("div.product-price");
String newStuff = stuff.toString();
newStuff = newStuff.substring(newStuff.indexOf("$")); // throws exception because "$" is not in the String.
newStuff = newStuff.substring(0, newStuff.indexOf(" "));
return newStuff;
} catch (Exception arg0) {
return "";
}
}
public static void main(String[] args){
IndependenceMedical test = new IndependenceMedical();
Document doc = test.getDoc("187955");
System.out.println("\n\n\n\n\n\n\n\n\n\n"); //to separate the return lines
System.out.println(test.getPrice(doc));
}
}

Related

If i parse an encrypted html file to a string can i somehow obtain the text from it?

import java.net.*;
import java.io.*;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class UrlReaderTest {
public static void main(String[] args) throws Exception {
URL url = new URL("https://www.amazon.com/");
String s = null;
StringBuilder contentBuilder = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new
InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
contentBuilder.append(str);
}
in.close();
} catch (IOException e) {
System.err.println("Error");
}
s = contentBuilder.toString();
Document document = Jsoup.parse(s);
System.out.println(document.text());
}
}
What i am getting has mainly symbols like these: Η1?0 Π??0ή=tθ Jr?/β#Q? l?r{ΪεI/ ΉΟ~νJ?j?Ά-??ΙiLs?YdHλ²ύ?α?η?ογV"ηw[:?0??νSQψyθ?*²?γpI? ??²ρνl???2JμΚ?ΣS?Αl4ςRΛ\KR545υ?SK
Is there anything i can do to transform that in a form that i can use?
I can't find something specific online.
Edit: What i want specificly is to decrypt that information. What i want for example is to be able to take the text from an event page from facebook search it to find the keywords i want and use those somewhere else.
As #t.m.adam noted in his comment, the problem is that the response from stream is gzipped (compressed). So, if you want to read it from the URL stream, you need to pass it through a GZIPInputStream before InputStreamReader (see this answer). Alternatively, as #t.m.adam suggests, you can use Jsoup's built-in connect() method:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class UrlReaderTest {
public static void main(String[] args) {
System.out.println(System.getProperty("java.classpath"));
try {
Document doc = Jsoup.connect("https://www.amazon.com").get();
System.out.print(doc.text());
}
catch (IOException e) {
System.err.println("Error");
}
}
}

Optimize search in sourcecode using BufferedReader

I've put some lines Java together and get a "programm" that read the sourcecode of a website and search line by line via BufferedReader the String Suche, copy the URL's in clipboard and give a message via JOptionPane if there is a match. All in all it work's great, but there is a problem: the perfomance.
It's mostly because of my network latency I think.
At first, here's my code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import javax.swing.JOptionPane;
public class Verbindung {
static String links ="";
public static void main(String[] args) {
String[] urls = {"http://google.de"};
for(int i=0; i < urls.length; i++ ){
try {
URL url = new URL(urls[i]);
show(url);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
new StringSelection(links), null);
if (links != ""){
JOptionPane.showMessageDialog(null, "Es wurden Links gefunden!");
}
}
private static void show(URL url) throws IOException{
InputStream in = url.openStream();
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
String s;
while ((s = buff.readLine()) != null) {
if(s.indexOf(Suche) != -1){
links += url;
}
}
}
}
Edit:
Theres is the nice idea from RealSkeptic to insert a break and close the reading after having a match, but there isn't a big different of perfomance.
There are some more ideas to improve the perfomance of getting the Sourcecode (not including get a higher downloadspeed :D) or do it a better way?
Best regards from Germany.

Read file, store content to an object, storing object in ArrayList causes java.lang.NullPointerException

I am having some trouble to find out whats wrong with my code. I've been busy reading other topics with NullPointerExceptions and tried to find it out by using the debugger, I couldn't find a good video to show me how to work with a debugger so I kinda failed to figure it out.
I am using Netbeans and this is a take out from the error output:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tibianic.spy.system.FileHandler.addVipToList(FileHandler.java:81)
at tibianic.spy.system.FileHandler.loadVipList(FileHandler.java:65)
at tibianic.spy.system.FileHandler.<init>(FileHandler.java:27)
Im assuming the problem is located within the FileHandler Class - loadVipList/addVipToList methods.
The class:
package spy.system;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* #author unknown
*/
public class FileHandler {
ArrayList<Vip> vip;
public FileHandler() {
this.loadVipList();
}
public void setVipList(ArrayList<Vip> newList) {
this.vip = newList;
}
public ArrayList<Vip> getVipList() {
return this.vip;
}
public void saveVipList() {
String line;
try {
FileWriter fWriter = new FileWriter("vip.txt");
BufferedWriter bWriter = new BufferedWriter(fWriter);
for (Vip v : vip) {
line = v.getName() + ":" + v.getNote();
bWriter.write(line);
}
bWriter.close();
fWriter.close();
} catch (IOException ex) {
Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void loadVipList() {
String line;
String[] person;
try {
FileReader fReader = new FileReader("vip.txt");
BufferedReader bReader = new BufferedReader(fReader);
while ((line = bReader.readLine()) != null) {
person = line.split(":");
addVipToList(person[0],person[1]);
}
bReader.close();
fReader.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void addVipToList(String name, String note) {
Vip guy = new Vip(name,note);
vip.add(guy);
}
}
To my understanding the NullPointerException comes when im giving a object with value null to a method, is that right? I think there is a very small misstake I just cant find because I dont really know what the error message is trying to tell me -
I did cut out a header comment so the lines might not be accurate.
Thanks in advance
Your exception says that your error is located at tibianic.spy.system.FileHandler.addVipToList(FileHandler.java:81)
and based on your code it is thrown in the following method.
private void addVipToList(String name, String note) {
Vip guy = new Vip(name,note);
vip.add(guy);
}
Note that you have
public void setVipList(ArrayList<Vip> newList) {
this.vip = newList;
}
you need to call setVipList() first before you call addVipToList(), since without that the list vip is still null.
I think the problem is because you never initialized the arraylist.
ArrayList<Vip> vip = new ArrayList<Vip>
I didn't call setVipList() and I didn't want to call it - yet changing
public class FileHandler {
ArrayList<Vip> vip;
to
public class FileHandler {
ArrayList<Vip> vip = new ArrayList<>();
solved this, I knew this was easy - so embarrassing.
Well, thanks everyone for your fast responses.
the class seems have problem:
when you new a instance ,it will be invoke method this.loadVipList();
public FileHandler() {
this.loadVipList();
}
but vip is null;
ArrayList<Vip> vip;
so you are better write like this:
ArrayList<Vip> vip = new ArrayList<Vip>();
or
List<Vip> vip = new ArrayList<Vip>();
it will be ok!

jsoup unexpectedly fetching shopwiki image

I am using the following code to fetch images from the web:
import java.io.FileOutputStream;
import java.io.IOException;
import org.jsoup.Jsoup;
public class fetchImageTest {
public static void main(String[] args) throws Exception {
saveImage(args[0], args[1]);
}
private static boolean saveImage(String string, String destination) throws IOException {
string = string.replaceAll(" ", "%20");
try {
byte[] image = Jsoup.connect(string).ignoreContentType(true).timeout(10000).execute().bodyAsBytes();
FileOutputStream os = new FileOutputStream(destination);
os.write(image);
os.close();
return true;
}
catch (IOException e) {
System.out.println("couldn't open " + string);
return false;
}
catch (Exception e) {
System.out.println("couldn't open - general exception" + string);
return false;
}
}
}
Due to a bug in some of my other code, I tried to fetch an image from a broken URL, of the form:
http://shop.foo.comhttp://shop.foo.com/1.jpg
My code ended up fetching a shopwiki image, like
I am using jsoup-1.7.1.jar. Is there a virus on my server? Is there a virus with my jsoup jar file?
I really have no idea ...
Several sites set up a system to protect the recovery of their image.
I guess you try to retrieve images shopwiki.com
I watched their URL to retrieve a picture is it is well established that security.
http://si4.shopwiki.com/i/data/120x120/18/4/2/aHR0cDovL2VjeC5pbWFnZXMtYW1hem9uLmNvbS9pbWFnZXMvSS81MVMwWTBuZHBjTC5qcGc=.jpg

Run an interactive Dos program from java

I would like to run a Dos program from a web server. The Dos program has to be run interactively as the user interface is via a series of questions and answers. The answer to one question will determine the next question. I will have to use ajax on the web server, but I think I can do that.
I found one java program on Stackoverflow which seems to do something similar to what I want. However when I compile the program I get an error ie.
javac PipeRedirection.java
PipeRedirection.java:43: package InputProcess does not exist
InputProcess.Gobbler outGobbler = new InputProcess.Gobbler(p.getInputStream());
The stack overflow question url was
How can I write large output to Process getOutputStream?
The Java file was
/*
####### PipeRedirection.java
*/
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class PipeRedirection {
public static void main(String[] args) throws FileNotFoundException {
if(args.length < 2) {
System.err.println("Need at least two arguments");
System.exit(1);
}
try {
String input = null;
for(int i = 0; i < args.length; i++) {
String[] commandList = args[i].split(" ");
ProcessBuilder pb = new ProcessBuilder(commandList);
//pb.redirectErrorStream(true);
Process p = pb.start();
if(input != null) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream())), true);
writer.println(input);
writer.flush();
writer.close();
}
InputProcess.Gobbler outGobbler = new InputProcess.Gobbler(p.getInputStream());
InputProcess.Gobbler errGobbler = new InputProcess.Gobbler(p.getErrorStream());
Thread outThread = new Thread(outGobbler);
Thread errThread = new Thread(errGobbler);
outThread.start();
errThread.start();
outThread.join();
errThread.join();
int exitVal = p.waitFor();
System.out.println("\n****************************");
System.out.println("Command: " + args[i]);
System.out.println("Exit Value = " + exitVal);
List<String> output = outGobbler.getOuput();
input = "";
for(String o: output) {
input += o;
}
}
System.out.println("Final Output:");
System.out.println(input);
} catch (IOException ioe) {
// TODO Auto-generated catch block
System.err.println(ioe.getLocalizedMessage());
ioe.printStackTrace();
} catch (InterruptedException ie) {
// TODO Auto-generated catch block
System.err.println(ie.getLocalizedMessage());
ie.printStackTrace();
}
}
public static class Gobbler implements Runnable {
private BufferedReader reader;
private List<String> output;
public Gobbler(InputStream inputStream) {
this.reader = new BufferedReader(new InputStreamReader(inputStream));
}
public void run() {
String line;
this.output = new ArrayList<String>();
try {
while((line = this.reader.readLine()) != null) {
this.output.add(line + "\n");
}
this.reader.close();
}
catch (IOException e) {
// TODO
System.err.println("ERROR: " + e.getMessage());
}
}
public List<String> getOuput() {
return this.output;
}
}
}
Does anyone know why I get the compile error? Can I substitute some other code for InputProcess?
Thanks for any help
Peter
I think it's pretty obvious that you're missing parts to this code. A package named InputProcess which has a class called Gobbler was not included in the OP's post. Probably because it was not relevant to their question.
The error message essentially says that it can not find this package/code that it is looking for.
What this class does exactly, only the OP can tell you. At its most basic, though, it appears to read from an InputStream and convert it to a List<String>. I would read up on Java IO and try to replicate similar functionality.
Edit:
Looks like the Gobbler class is indeed included in the example above. Remove the InputProcess package name from your code (or put the Gobbler class in an InputProcess package) and you should be good to go.

Categories

Resources