Last active
August 29, 2015 13:59
-
-
Save wemakeweb/10727659 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Aufgabe_4_vers1; | |
import java.net.*; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.io.*; | |
public class http_Client { | |
private URL url; | |
private static InputStream in; | |
private static OutputStream out; | |
private static BufferedReader reader; | |
// private static BufferedWriter writer; | |
private String ip; | |
private int port = 80; | |
private String s; | |
private String string; | |
private String content = ""; | |
private String header = ""; | |
private String[] splitBreakLine; | |
private String[][] array; | |
private String singleHeadParameters = "Content-Type"; //"Server" oder "Vary" oder "X-Cobbler" oder "Content-Type" eingeben!!! bei Last-Modified bekommt man eine Meldung dass es nicht existiert! | |
private String wert = ""; | |
public http_Client(String url) throws Exception { | |
// nur einmal eine URL Instance erstellen und mit dieser dann arbeiten | |
this.url = new URL(url); | |
this.ip = this.url.getHost(); | |
int port = this.url.getPort(); | |
// port wert von Anfang an auf 80 setzen und nur bei bedarf ändern | |
if ( port > 0 ) { | |
this.port = port; | |
} | |
execute(); | |
} | |
public void execute() throws Exception { | |
/* | |
wenn die Url invalid ist brauchst du gar kein Socket aufmachen, | |
deswegen ist diese if vorgezogen | |
*/ | |
if ( !isValidUrl() ) { | |
System.out.println("Die URL ist FALSCH!"); | |
return; | |
} | |
Socket socket = new Socket(this.ip, this.port); | |
in = socket.getInputStream(); | |
reader = new BufferedReader(new InputStreamReader(in)); | |
out = socket.getOutputStream(); | |
System.out.println("--------------------------------------------------------------------"); | |
System.out.println(getContentOnly()); | |
} | |
/* | |
ich nenne alle methoden namen die einen Boolean Wert zurück geben immer is… | |
*/ | |
public boolean isValidUrl(String u) throws IOException { | |
HttpURLConnection connection = (HttpURLConnection) this.url.openConnection(); | |
connection.connect(); | |
return (connection.getResponseCode() == 200); | |
} | |
public String getContentOnly() throws Exception { | |
s = "get / http/1.0\n\n"; | |
out.write(s.getBytes()); | |
out.flush(); | |
getheader(); | |
getcontent(); | |
if (checkLastModified() == true) { | |
System.out.println(getLastModified()); | |
System.out.println("--------------------------------------------------------------------"); | |
} | |
return header; // Hier hat man die Möglichkeit, entweder Head oder | |
// Content Bereich auszugeben: entweder "content" oder | |
// "header" zurückgeben | |
} | |
public String getheader() throws IOException { | |
do { | |
string = reader.readLine(); | |
header = header + string + "\n"; | |
} while (reader.readLine().isEmpty() != true); | |
return header; | |
} | |
public String getcontent() throws IOException { | |
do { | |
string = reader.readLine(); | |
content = content + string + "\n"; | |
} while (reader.readLine() != null); | |
return content; | |
} | |
public boolean checkLastModified() throws Exception { | |
boolean checkLastModified = header.contains(singleHeadParameters); | |
if (checkLastModified) { | |
System.out.println("Es gibt ein Server Parameter: "); | |
System.out.println("--------------------------------------------------------------------"); | |
} else { | |
System.out.println("Es gibt leider kein Server im Head des Requests!"); | |
System.out.println("--------------------------------------------------------------------"); | |
} | |
return checkLastModified; | |
} | |
public String getLastModified() throws Exception { | |
splitBreakLine = header.split("\n"); | |
String[] eindim = new String[splitBreakLine.length]; | |
array = new String[splitBreakLine.length][3]; | |
int counter = 0; | |
int temp = 0; | |
for (int i = 0; i <= (splitBreakLine.length - 1); i++) { | |
eindim = splitBreakLine[i].split(" "); | |
do { | |
array[i][counter] = eindim[counter]; | |
temp++; | |
counter++; | |
} while (temp != eindim.length); | |
temp = 0; | |
counter = 0; | |
} | |
for (int i = 0; i <= (splitBreakLine.length - 1); i++) { | |
if(array[i][0].contains("Content-Type:")){ | |
singleHeadParameters = array[i][0]; | |
wert = array[i][1]+" "+ array[i][2]; | |
}else if (array[i][0].contains(singleHeadParameters+":")) { | |
singleHeadParameters = array[i][0]; | |
wert = array[i][1]; | |
} | |
} | |
return singleHeadParameters + " " + wert; | |
} | |
public static void main(String[] args) throws Exception { | |
http_Client client = new http_Client("http://www.heise.de/"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment