[XoL]
2013-05-02 22:24:57
Solved!!
Thanks Brandon and Le Jingle!
Thanks Brandon and Le Jingle!
2013-05-02 22:24:57
2013-05-03 01:03:44
public static void downloadFile(URL Address, String Path) throws IOException {
URLConnection Connection = Address.openConnection();
try (FileOutputStream OutFile = new FileOutputStream(Path); InputStream IStream = Connection.getInputStream()) {
byte[] Buffer = new byte[1024];
for (int Read = 0; (Read = IStream.read(Buffer)) != -1;) {
OutFile.write(Buffer, 0, Read);
}
}
}
//Use Connection.addRequestProperty to add a field to the protocol.
public static String getPage(URL Address) throws IOException {
try {
URLConnection Connection = Address.openConnection();
Connection.addRequestProperty("Protocol", "HTTP/1.1");
Connection.addRequestProperty("Connection", "keep-alive");
Connection.addRequestProperty("Keep-Alive", "200");
Connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
byte[] Buffer = new byte[Connection.getContentLength()];
try (DataInputStream Stream = new DataInputStream(Connection.getInputStream())) {
Stream.readFully(Buffer);
}
return new String(Buffer);
} catch (Exception Ex) {
return null;
}
}
//OR
public static String getPage(URL Address) {
try {
URLConnection Connection = Address.openConnection();
Connection.addRequestProperty("Protocol", "HTTP/1.1");
Connection.addRequestProperty("Connection", "keep-alive");
Connection.addRequestProperty("Keep-Alive", "200");
Connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
try (BufferedReader Stream = new BufferedReader(new InputStreamReader(Connection.getInputStream()))) {
String Line, Result = new String();
while ((Line = Stream.readLine()) != null) {
Result += Line;
}
return Result;
}
} catch (Exception Ex) {
System.out.println(Ex.getMessage());
}
return null;
}
package browser;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Browser {
private JFrame frame = null;
private JTextField AddressBar = null;
private JButton GoButton = null;
private JTextArea ViewArea = null;
public static void main(String[] args) {
Browser B = new Browser();
B.initialize();
B.frame.setVisible(true);
}
private void initialize() {
frame = new JFrame("Some Browser..");
frame.setBounds(100, 100, 550, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel AddressPanel = new JPanel();
JPanel ViewPanel = new JPanel();
AddressPanel.add(AddressBar = new JTextField());
AddressPanel.add(GoButton = new JButton("Go"));
ViewPanel.add(ViewArea = new JTextArea(25, 25));
frame.add(AddressPanel, BorderLayout.NORTH);
frame.add(ViewPanel, BorderLayout.SOUTH);
AddressBar.setColumns(45);
ViewArea.setLineWrap(true);
ViewArea.setEditable(false);
ViewArea.setAutoscrolls(true);
JScrollPane scroll = new JScrollPane(ViewArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.add(scroll);
frame.pack();
this.addListeners();
}
private void addListeners() {
GoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String Link = AddressBar.getText();
if (Link == null) {
System.out.println("Cannot search for blank URLs!");
} else {
try {
String Source = getPage(new URL(Link));
if (Source != null && !Source.isEmpty()) {
ViewArea.setText(Source);
} else {
System.out.println("Failed to retrieve Source..");
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
});
AddressBar.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
GoButton.doClick();
}
}
});
}
public static void downloadFile(URL Address, String Path) throws IOException {
URLConnection Connection = Address.openConnection();
try (FileOutputStream OutFile = new FileOutputStream(Path); InputStream IStream = Connection.getInputStream()) {
byte[] Buffer = new byte[1024];
for (int Read = 0; (Read = IStream.read(Buffer)) != -1;) {
OutFile.write(Buffer, 0, Read);
}
}
}
public static String getPage(URL Address) {
try {
URLConnection Connection = Address.openConnection();
Connection.addRequestProperty("Protocol", "HTTP/1.1");
Connection.addRequestProperty("Connection", "keep-alive");
Connection.addRequestProperty("Keep-Alive", "200");
Connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
try (BufferedReader Stream = new BufferedReader(new InputStreamReader(Connection.getInputStream()))) {
String Line, Result = new String();
while ((Line = Stream.readLine()) != null) {
Result += Line;
}
return Result;
}
} catch (Exception Ex) {
System.out.println(Ex.getMessage());
}
return null;
}
}
2013-05-03 01:45:26
2013-05-04 14:06:07
2013-05-04 15:42:39