Http request incomplete [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to make an HTTP request for a personal project and for some reason my request did not return the full HTML.
I don't know if I'm forgetting anything, I tried googling it but I can't find anything that helped.
URL link = new Url("https:\\www (...)");
HttpURLConnection con = (HttpURLConnection)link.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine = "";
while (in.readLine() != null){
inputLine += in.readLine();
}
in.close();
con.disconnect();
I was expecting the full HTML page, but I got part of it, like:
HTML Page:
<div>
<span>product</span>
<span>price</span>
</div>
received:
<div>
<span>product</span>
</div>

Think about what this does:
String inputLine = "";
while (in.readLine() != null){
inputLine += in.readLine();
}
You're checking if a line does not equal null, then appending the next line to the string. This means you skip every other line.
To fix this, you can assign a temporary variable the value of the nextLine() call and use that instead:
String inputLine = "";
String line = in.readLine();
while (line != null){
inputLine += line;
line = in.readLine();
}
Inline:
String inputLine = "";
String line;
while ((line = in.readLine()) != null){
inputLine += line;
}

Related

Out of bounds error, while reading file then splitting it [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I've been getting an Array Index Out of Bounds Error.
Basically I have a file where a user name and password is saved lke this: user:password
I'm trying to read the file to check if a new user is already signed in or not. This is in a thread and also using a socket.
private void authentication(String user, String password) {
List<String> nomes = new ArrayList<>();
List<String> pass = new ArrayList<>();
BufferedReader reader;
try {
FileWriter fw = new FileWriter("users.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw);
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
//String line;
String line;
while((line = br.readLine())!=null){
String[] pair = line.split(";");
nomes.add(pair[0]);
pass.add(pair[1]);
}
/*while ((line = br.readLine()) != null) {
String[] info = line.split(":");
System.out.println(line);
System.out.println(info[0]);
System.out.println(info[1]);
}*/
if(nomes.isEmpty()) {
out.println(user + ":" + password);
System.out.println("Novo Utilizador Autenticado.");
System.out.println("Bem Vindo!!");
}else if(nomes.contains(user)) {
bw.newLine();
if(password.equals(pass.get(nomes.indexOf(user)))) {
System.out.println("Utilizador autenticado com sucesso!");
System.out.println("Bem Vindo de Volta!!");
}
}else {
terminate();
}
bw.close();
br.close();
out.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
The error is happening in the last line on the pair[1]. It has a value but for some reason isn't seeing it.
Your text in the file is like "user:password" and you are trying to split it with ";". So pair[1] won't have any value. You should try split it with ":"
Try to access the values dynamically using index instead of hardcoded 0,1 to avoid index out of bond exception :
while ((line = br.readLine()) != null) {
String[] info = line.split(":");
int index = 0;
System.out.println(line);
while(index < info.length){
System.out.println(info[index++]);
}
}

Download from an URL in Spring [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want that , by pasting the URL of a file (the file can be an image, an Xhtml, or a Css) into a form of a JSP, this can be downloaded form internet and saved locally. Please can you help me ?
you can use this to open URL in the browser and save into the file location.
<%
String site= contain the string(URL);
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site);
File file = new File("/Users/asdf.xml");
FileWriter fr = null;
BufferedWriter br = null;
URL url = new URL(site);
BufferedReader reader = new BufferedReader
(new InputStreamReader(url.openStream()));
fr = new FileWriter(file);
br = new BufferedWriter(fr);
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
br.write(line);
br.newLine();
}
reader.close();
br.close();
%>

Unexpected Buffered Reader Behavior: skipping odd-numbered lines [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am experiencing very strange behavior with BufferedReader. I want to read an entire file however it only reads every other line.
E.g the file below
1 //ignore the left most space - shouldn't exist
2
3
4
5
6
Will output
2
4
6
Here is some of my code...
fileRead = new BufferedReader(new InputStreamReader( new FileInputStream(file)));
public void scan(){
if (fileRead != null){
try{
while ((fileRead.readLine()) != null){
String line = fileRead.readLine();
String abcLine = line;
System.out.println(line);
}
}catch(IOException e) {
System.out.println("Line can not be read");
}
}else{ System.out.println("Can not Read - File Not Found"); }
}
My best bet is the bug lies within the while statement. Is this the correct way to ensure
you read the file until you reach EOF "end of file" ?
Any insight is truly appreciated
Thank you!
You're reading two lines each time through the loop. Your current code is:
while ((fileRead.readLine()) != null){ // reads a line, ignores it
String line = fileRead.readLine(); // reads another line, stores in 'line'
... // do stuff with 'line'
}
Every call to readLine() reads a line. You probably want something more like:
String line;
while ((line = fileRead.readLine()) != null) {
... // do stuff with 'line'
}

Read Formated Text for Java Servlets [closed]

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 minor problem that sets me back nearly all day and I would like some input on how to solve it.
I am making a servlet for my site and I have a formatted .txt in the following form:
Name,Surname,Age
for example:
mary,jane,23
mark,thomson,25
.
.
.
etc
I want somehow to read this txt in order to pass these strings from my .txt to my database, in the corresponding fields.
Thanks in advance for any input!
Maybe this will get you started:
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line = br.readLine()) != null) {
String [] split = line.split(",");
String name= split[0];
String surname= split[1];
Integer age= Integer.parseInt(split[2]);
//save it to the database
}
//close the reader somewhere
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
Arraylist<String> string_array = new Arraylist<String>();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
string_array.add(sb.soString());
}
} finally {
br.close();
}
Subsequently you will separate by comma every element/line in your String Arraylist
string_array[i].split(',');
and you will proceed to any action you want with the data.

How can i find the City name using IP address in Java [closed]

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 2 years ago.
Improve this question
I want city name from IP address using Java
is there any idea to do this ?
From Andrey link, here is how to construct the inquiry, this code will return an HTML file with all details of current IP, including city;
String IP= "123.123.123.123";
URL link = new URL("http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress="+IP);
BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
}
in.close();
UPDATE, 23 May 2013
The previous answer is ok, but it's not an API call, it reads an HTML page, that I provided previously because I didn't find any free APIs. Next is a REST API call that can be used easily and will return all the info required, it's recommended to use this one:
String ip = "2.51.255.200";
URL url = new URL("http://freegeoip.net/csv/" + ip);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
int status = connection.getResponseCode();
if (status != 200) {
return null;
}
reader = new BufferedReader(new InputStreamReader(is));
for (String line; (line = reader.readLine()) != null;) {
//this API call will return something like:
"2.51.255.200","AE","United Arab Emirates","03","Dubai","Dubai","","x-coord","y-coord","",""
// you can extract whatever you want from it
}
If your Application is deployed behind the firewall. So instead of calling a API, we can use GeoLite below is the sample code.
Download City.dat file from http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
File datapath = new File("GeoLiteCity.dat");
LookupService cl = new LookupService(datapath,
LookupService.GEOIP_MEMORY_CACHE
| LookupService.GEOIP_CHECK_CACHE);
String cityName = cl.getLocation(ipAddress).city;

Categories

Resources