Problem częsty w sumie, choć jakąś nie potrafię znaleźć rozwiązania. readLine() blokuje program.
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Klasa
{
public static String strona;
public static String sciezkaPlikuDoZapisania;
private Socket socket;
private PrintWriter pw;
private BufferedReader br;
private InputStream is;
private OutputStream os;
public Klasa()
{
strona = "www.google.pl";
sciezkaPlikuDoZapisania = "strona.html";
}
public Klasa(String s1, String s2)
{
strona = s1;
sciezkaPlikuDoZapisania = s2;
}
public void wyslijZadanie() throws IOException
{
os = socket.getOutputStream();
pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
pw.println("GET / HTTP/1.1");
pw.println("Host: " + strona);
pw.println("Connection: close");
pw.println("");
pw.flush();
pw.flush();
}
public String pobierzStrone() throws IOException
{
StringBuilder ret = new StringBuilder();
is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line;
System.out.println("Wypis");
while(!(line = br.readLine()).isEmpty())
System.out.println(line);
System.out.println("Naglowek przeczytany");
while((line = br.readLine()) != null)
ret.append(line);
System.out.println("Wypisuje");
return ret.toString();
}
public void zapiszDoPliku(String s) throws IOException
{
FileWriter fw = new FileWriter(sciezkaPlikuDoZapisania);
fw.write(s);
fw.close();
}
public void utworzSocket() throws IOException
{
InetAddress ia = InetAddress.getByName(strona);
socket = new Socket(ia, 80);
}
public void zamknijSocket() throws IOException
{
is.close();
os.close();
pw.close();
br.close();
socket.close();
}
public static void main(String args[]) throws UnknownHostException, IOException
{
Klasa k = null;
if(args.length == 2)
k = new Klasa(args[0], args[1]);
else
k = new Klasa();
k.utworzSocket();
String t = k.pobierzStrone();
k.zapiszDoPliku(t);
k.zamknijSocket();
}
}
Dla mnie dziwny jest fakt, że to poniżej działa poprawnie:
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Yyy
{
public static String strona = "www.google.pl";
public static String sciezkaPlikuDoZapisania = "strona.html";
public static void main(String args[]) throws UnknownHostException, IOException
{
if(args.length == 2)
{
strona = args[0];
sciezkaPlikuDoZapisania = args[1];
}
InetAddress ia = InetAddress.getByName(strona);
@SuppressWarnings("resource")
Socket s = new Socket(ia, 80);
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
pw.println("GET / HTTP/1.1");
pw.println("Host: " + strona);
pw.println("Connection: close");
pw.println("");
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = br.readLine();
StringBuilder sb = new StringBuilder();
while(!line.isEmpty())
{
line = br.readLine();
}
while((line = br.readLine()) != null)
sb.append(line);
String ret = sb.toString();
FileWriter fw = new FileWriter(sciezkaPlikuDoZapisania);
fw.write(ret);
fw.close();
}
}
Siedzę nad tym już trochę, trochę czytałem o tym, jednak po kilku próbach się poddałem i piszę tutaj. W jaki sposób odczytać odpowiedź w pierwszym kodzie, by to działało poprawnie?