Hi ich möchte gerne das der Browser den User nach Passwort und Benutzername fragt.
Mit den Zeilen:
os.writeBytes(PROTOCOL + unautohricied + CRLF);
os.writeBytes(auth + CRLF);
sende ich:
HTTP/1.0401 UNAUTHORIZED
WWW-Authenticate: Basic realm="Geheimer Bereich"
Der Server gibt auch die Seite richtig aus. Nur diese Abfrage passiert einfach nicht. Kann mir jemand sagen was daran falsch ist: Hier nochmal der ganze Code falls was nich klar is.
Mit den Zeilen:
os.writeBytes(PROTOCOL + unautohricied + CRLF);
os.writeBytes(auth + CRLF);
sende ich:
HTTP/1.0401 UNAUTHORIZED
WWW-Authenticate: Basic realm="Geheimer Bereich"
Der Server gibt auch die Seite richtig aus. Nur diese Abfrage passiert einfach nicht. Kann mir jemand sagen was daran falsch ist: Hier nochmal der ganze Code falls was nich klar is.
Java:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.PasswordAuthentication;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
import javax.imageio.stream.FileImageInputStream;
import com.sun.java.util.*;
import com.sun.net.httpserver.Authenticator;
import com.sun.net.httpserver.HttpExchange;
public class OneShotHTTP extends Thread{
public final static String CRLF = "\r\n";
public final static String PROTOCOL ="HTTP/1.0";
public final static String SC_OK = "200 OK";
public final static String SC_BAD_REQUEST = "400 Bad Eequest";
public final static String unautohricied = "401 UNAUTHORIZED";
public final static String SC_FORBIDDEN = "403 Fobridden";
public final static String SC_NOT_FOUND = "404 not Found";
public final static String auth = "WWW-Authenticate: Basic realm=\"Geheimer Bereich\"";
protected String statutsCode = SC_OK;
protected String getSC_OK() {
return SC_OK;
}
protected void setStatusCode(String statuscode) {
this.statutsCode = statuscode;
}
protected void sendStatusLine(DataOutputStream os){
try {
System.out.println(PROTOCOL + getSC_OK() + CRLF);
os.writeBytes(PROTOCOL + getSC_OK() + CRLF);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Socket s = null;
public final static int port = 80 ;
private static String canonicalRoot;
public static void main(String[] args) throws IOException {
canonicalRoot = new File(".").getCanonicalPath();
System.out.println(canonicalRoot);
try {
System.out.println("Start:");
ServerSocket listen = new ServerSocket(port);
if(listen.isBound())
System.out.println("Port belegt");
while(true){
OneShotHTTP server = new OneShotHTTP(listen.accept());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public OneShotHTTP(Socket s) {
this.s = s;
start();
}
public void sendDocument(DataOutputStream os, File file){
System.out.println("Dokument wird gesenedet mit " + file.getAbsolutePath());
try {
BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
// sendStatusLine(os);
// os.writeBytes(CRLF);
// String auth = "HTTP/1.0200 OK" + CRLF + CRLF;
// System.out.println("Drüber");
// System.out.println(auth);
//
os.writeBytes(PROTOCOL + unautohricied + CRLF);
System.out.println(PROTOCOL + unautohricied + CRLF);
System.out.println(auth + CRLF);
os.writeBytes(auth + CRLF);
byte[] buf = new byte[1024];
int len;
while((len = is.read(buf, 0, 1024)) != -1){
os.write(buf, 0 , 1024);
}
is.close();
os.close();
s.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
try {
BufferedReader is = new BufferedReader(new InputStreamReader(s.getInputStream()));
DataOutputStream os = new DataOutputStream(s.getOutputStream());
String request;
request = is.readLine();
System.out.println("Amfrage: " + request);
StringTokenizer tokenizer = new StringTokenizer(request);
if(tokenizer.nextToken().equals("GET") ){
String fileString = tokenizer.nextToken();
System.out.println(fileString);
if(fileString.equals("/")){
fileString = "index.html";
}
File file = new File("C:\\Dokumente und Einstellungen//Administrator//workspace//HTTP_Server//" + fileString);
char slash = '\\';
// if(!file.getCanonicalPath().equals( canonicalRoot + slash + fileString)){
// System.err.println("403 Forbidden!");
// s.close();
// }
// else
sendDocument(os, file);
}} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Zuletzt bearbeitet von einem Moderator: