Probleme bei Proxy-Server!

  • Themenstarter Themenstarter Guest
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
G

Guest

Gast
Hi @ all!

Schreib schon den ganzen Tag an diesem Proxy-Server rum aber es will einfach nicht funktionieren.

Das Ziel: Mit meinem Browser schicke ich GET-Anfragen an den Proxy-Server. Der macht eine Url auf und öffnet einen Stream zur Url und schreibt den Inhalt an den Client zurück.
Der Proxy-Server soll natürlich mehrere Clients gleichzeitig parallel bedienen können, deswegen wird bei jedem neuen Client ein neuer Thread der unteren Klasse erzeugt mit einem eigenen Socket. Die Klasse, die das macht hab ich hier weggelassen.

Problem: In der Zeile DataInputStream inFromUrl = new DataInputStream(url.openStream()); bekomme ich manchmal folgenden Exception:

java.io.FileNotFoundException: http://www.orf.at/orfon/transparentpixen_a.jpg ALT=
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:787)
at aufg41.ProxyThread.run(ProxyThread.java:58)


Warum das? Hab in den docs gelesen, dass man immer den absoluten Pfad (Url + File) übergeben kann.
-----
Ganze Teile der Homepage und vor allem Bilder werden ganz oder gar nicht dargestellt.
z.B.: www.orf.at

orf.jpg




----

Sorry das der Code so lang ist, aber ich hab bei den kritischen Bereichen, wo ich glaub das der Fehler liegt, Kommentare in Großschrift daneben. So sinds nur ein paar Zeilen Code. Bitte schaut mal kurz drüber.



Code:
package aufg41;

/**
 * 

Überschrift: Proxy-Thread</p>
 * 

Beschreibung:
 * for each inquiring client one Proxy-Thread will be started
 * to handle requests of this client</p>
 * @version 1.0
 */

import java.net.*;
import java.io.*;
import java.util.StringTokenizer;

public class ProxyThread extends Thread {

  private int id;
  private Socket socket;


  /**
   * @param threadId int
   * @param socketForClient Socket: the socket for this and only this client!
   */
  public ProxyThread(int threadId, Socket socketForClient) {
    id = threadId;
    socket = socketForClient;
  } // constructor()

  //******************************************

  public void run() {
    System.err.println(this + " is running!");

    String request;
    String[] tokens;
    boolean rightCommand;
    BufferedReader inFromClient;
    DataOutputStream outToClient;

    try {
      inFromClient = new BufferedReader(new InputStreamReader(socket.
          getInputStream()));
      outToClient = new DataOutputStream(socket.getOutputStream());

      request = inFromClient.readLine(); //read 1.line from client
      System.out.println(this + " read line from client: " + request);
      tokens = splitCommand(request); //split the 1.line into its parts

      rightCommand = checkCommand(tokens);

      if (rightCommand) {

        //create and open stream to Url: token[1] = Url
        URL url = new URL(tokens[1]);
        DataInputStream inFromUrl = new DataInputStream(url.openStream());       //HIER BEKOMM ICH MANCHMAL DIE EXCEPTION

        forward( inFromUrl , outToClient );                              //FORWARD-METHODE WEITER UNTEN
      } // if(rightCommand)
      else {
        System.err.println("WRONG COMMAND");
      }

      System.out.println(this + " send content to client!");

      inFromClient.close();                                                       //HIER CLOSE ICH ALLES; 
      outToClient.close();                                                        //GLAUBE NICHT DAS HIER DATEN VERLOREN GEHEN
      socket.close();                                                               //KÖNNEN ODER?
    }
    catch (IOException ex) {
      System.err.println(ex);
      ex.printStackTrace();
    }

    System.err.println(this + " is shutdown!");
  } // run()


 /**
  * splits the command up in part-strings by delimiter " "
  * @param command String: the command to split
  * @return String[]: the part-strings
  * tokens[0]: Command
  * tokens[1]: Url
  * tokens[2]: HTTP-Version
  */
 private String[] splitCommand(String command) {
   StringTokenizer tokenizedString = new StringTokenizer(command);
   String[] tokens = new String[tokenizedString.countTokens()];

   for (int i = 0; i < tokens.length; i++) {
     tokens[i] = tokenizedString.nextToken();
   }

   return tokens;
 } // splitCommand()


  /**
   * checks if the Command equals "GET"
   * @param tokens String[]
   * @return boolean
   */
  private boolean checkCommand(String[] tokens) {
    if(tokens[0].equals("GET"))
      return true;
    else
      return false;
  } // checkCommand()


    //IST DAS OK???? ICH ÜBERGEBE DEN INPUTSTREAM VON DER URL UND OUTPUTSTREAM ZUM SOCKET VOM CLIENT
  private void forward(DataInputStream in, DataOutputStream out) {
    byte[] bytes = new byte[1024];

    try {
      while ( in.read(bytes) > 0 ) {          //HIER LES ICH JEWEILS 1024 BYTES UND SCHREIB SIE IN DEN      
        out.write(bytes);                        //OUTPUTSTREAM; DAMIT BEI BILDÜBERTRAGUNG AUCH OK IST
      }
    }
    catch (IOException ex) {
      System.err.println(ex);
      ex.printStackTrace();
    }
  } // forward()


  /**
   * overwritten toString-Method
   * @return String: ProxyThread + id
   */
  public String toString() {
    return "ProxyThread-" + id;
  } // toString()

} // class ProxyThread
 
Nein, glaub nicht das es dast ist.


Der Browser schick folgende HTTP-Anfrage

ProxyThread-19 read line from client: GET http://www.orf.at/orfon/transparentpixen_a.jpg ALT= HTTP/1.0


java.io.FileNotFoundException: http://www.orf.at/orfon/transparentpixen_a.jpg ALT=

java.io.FileNotFoundException: http://www.orf.at/orfon/transparentpixen_a.jpg ALT=

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:787)

at aufg41.ProxyThread.run(ProxyThread.java:56)


----

Die übergebene Url und die Exception-Url sind gleich
 
becstift hat gesagt.:
ruf doch mal die url auf, die gibts nicht. ist doch klar, das da ne FileNotFoundException kommt!?!

Hab jetzt einmal Strings anstelle von bytes gschrieben. Die Exceptions kommen nicht mehr, dafür findet er manche Seiten überhaupt nicht oder wiederum nur fehlerhaft.

Code:
   Printwriter outToClient = new PrintWriter(socket.getOutputStream());

    ...
    ...
    ...

   URL url = new URL(urlString);
   URLConnection urlCon = url.openConnection();
   BufferedReader inFromUrl = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));  

   forward( inFromUrl , outToClient );

    ...
    ...
    ...

  private void forward(BufferedReader in, PrintWriter out) {
    String line;

    try {
      while ( (line = in.readLine()) != null ) {
        out.println(line);
      }
    }
    catch (IOException ex) {
      System.err.println(ex);
      ex.printStackTrace();
    }
  } // forward()
 
Ich vermute das das System klappt, aber der Link einfach nur fehlerhaft ist, da eine tag-angabe aus dem html in den link zur datei gerutscht ist.
wohl falsches " gesetzt, oder falsch geparst?
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben