InputStream - seltsamer Output

Wie oft warst Du auf Rex schon sauer?

  • Doch, schon ein wenig.

    Stimmen: 0 0,0%
  • Ja, doch.

    Stimmen: 0 0,0%
  • Ziemlich!

    Stimmen: 0 0,0%
  • Sehr!!

    Stimmen: 0 0,0%

  • Anzahl der Umfrageteilnehmer
    174
Status
Nicht offen für weitere Antworten.

Rex

Bekanntes Mitglied
Liebe Freunde,

mit der Classe ClientHTTPRequest sende ich ja einen POST_Request an den Server.
Dieser Antwortet ganz normal darauf.

Allerdings kommt beim Applet leider nix an.

Hier zunächst der Code:
Code:
 InputStream serverInput = ClientHttpRequest.post(
                    new URL("http://www.xxx.de/test/uptest.php"),
                    new Object[] {
                "orderno", orderno,
                "SID", SID,
                "data", f
            }

In serverInput wird als Stream die Ausgabe des Servers geschrieben.


Dann wird der Stream in einem String gespeichert:
Code:
String serverresponse = serverInput.toString();

Wenn ich mir den String ausgeben lasse,
kommt
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@f3d6a5


Das ist aber nicht die Antwort des Servers.

Der Vollständigkeit halber - hier die Klasse ClientHTTPRequest:
Code:
import java.net.URLConnection;
import java.net.URL;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.io.InputStream;
import java.util.Random;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.util.Iterator;
import javax.swing.JProgressBar;

/**
 * 

Title: Client HTTP Request class</p>
 * 

Description: this class helps to send POST HTTP requests with various form data,
 * including files. Cookies can be added to be included in the request.</p>
 *
 * @author Vlad Patryshev
 * @version 1.0
 */
public class ClientHttpRequest {
    URLConnection connection;
    OutputStream os = null;
    Map cookies = new HashMap();
    static long filesize;
    
    protected void connect() throws IOException {
        if (os == null) os = connection.getOutputStream();
    }
    
    protected void write(char c) throws IOException {
        connect();
        os.write(c);
    }
    
    protected void write(String s) throws IOException {
        connect();
        os.write(s.getBytes());
    }
    
    protected void newline() throws IOException {
        connect();
        write("\r\n");
    }
    
    protected void writeln(String s) throws IOException {
        connect();
        write(s);
        newline();
    }
    
    private static Random random = new Random();
    
    protected static String randomString() {
        return Long.toString(random.nextLong(), 36);
    }
    
    String boundary = "---------------------------" + randomString() + randomString() + randomString();
    
    private void boundary() throws IOException {
        write("--");
        write(boundary);
    }
    
    /**
     * Creates a new multipart POST HTTP request on a freshly opened URLConnection
     *
     * @param connection an already open URL connection
     * @throws IOException
     */
    public ClientHttpRequest(URLConnection connection) throws IOException {
        this.connection = connection;
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
        connection.addRequestProperty("Accept-Encoding", "gzip,deflate"); //Bugfix by AS: needed for PHP.
    }
    
    /**
     * Creates a new multipart POST HTTP request for a specified URL
     *
     * @param url the URL to send request to
     * @throws IOException
     */
    public ClientHttpRequest(URL url) throws IOException {
        this(url.openConnection());
    }
    
    /**
     * Creates a new multipart POST HTTP request for a specified URL string
     *
     * @param urlString the string representation of the URL to send request to
     * @throws IOException
     */
    public ClientHttpRequest(String urlString) throws IOException {
        this(new URL(urlString));
    }
    
    
    private void postCookies() {
        StringBuffer cookieList = new StringBuffer();
        
        for (Iterator i = cookies.entrySet().iterator(); i.hasNext();) {
            Map.Entry entry = (Map.Entry)(i.next());
            cookieList.append(entry.getKey().toString() + "=" + entry.getValue());
            
            if (i.hasNext()) {
                cookieList.append("; ");
            }
        }
        if (cookieList.length() > 0) {
            connection.setRequestProperty("Cookie", cookieList.toString());
        }
    }
    
    /**
     * adds a cookie to the requst
     * @param name cookie name
     * @param value cookie value
     * @throws IOException
     */
    public void setCookie(String name, String value) throws IOException {
        cookies.put(name, value);
    }
    
    /**
     * adds cookies to the request
     * @param cookies the cookie "name-to-value" map
     * @throws IOException
     */
    public void setCookies(Map cookies) throws IOException {
        if (cookies == null) return;
        this.cookies.putAll(cookies);
    }
    
    /**
     * adds cookies to the request
     * @param cookies array of cookie names and values (cookies[2*i] is a name, cookies[2*i + 1] is a value)
     * @throws IOException
     */
    public void setCookies(String[] cookies) throws IOException {
        if (cookies == null) return;
        for (int i = 0; i < cookies.length - 1; i+=2) {
            setCookie(cookies[i], cookies[i+1]);
        }
    }
    
    private void writeName(String name) throws IOException {
        newline();
        write("Content-Disposition: form-data; name=\"");
        write(name);
        write('"');
    }
    
    /**
     * adds a string parameter to the request
     * @param name parameter name
     * @param value parameter value
     * @throws IOException
     */
    public void setParameter(String name, String value) throws IOException {
        boundary();
        writeName(name);
        newline(); newline();
        writeln(value);
    }
    
    private static void pipe(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[500000];
        int nread;
        int navailable;
        int total = 0; //Menge an Bytes bisher gesendet
        long percentage; //Percent done...

        synchronized (in) {
            while((nread = in.read(buf, 0, buf.length)) >= 0) {
                out.write(buf, 0, nread);
                total += nread; //Wieviel bereits gesendet?
                percentage = (filesize/total)*100;          
            }
        }
        out.flush();
        buf = null;
    }
    
    /**
     * adds a file parameter to the request
     * @param name parameter name
     * @param filename the name of the file
     * @param is input stream to read the contents of the file from
     * @throws IOException
     */
    public void setParameter(String name, String filename, InputStream is) throws IOException {
        boundary();
        writeName(name);
        write("; filename=\"");
        write(filename);
        write('"');
        newline();
        write("Content-Type: ");
        String type = connection.guessContentTypeFromName(filename);
        if (type == null) type = "application/octet-stream";
        writeln(type);
        newline();
        pipe(is, os);
        newline();
    }
    
    /**
     * adds a file parameter to the request
     * @param name parameter name
     * @param file the file to upload
     * @throws IOException
     */
    public void setParameter(String name, File file) throws IOException {
        filesize = file.length();
        setParameter(name, file.getPath(), new FileInputStream(file));
    }
    
    /**
     * adds a parameter to the request; if the parameter is a File, the file is uploaded, otherwise the string value of the parameter is passed in the request
     * @param name parameter name
     * @param object parameter value, a File or anything else that can be stringified
     * @throws IOException
     */
    public void setParameter(String name, Object object) throws IOException {
        if (object instanceof File) {
            setParameter(name, (File) object);
        } else {
            setParameter(name, object.toString());
        }
    }
    
    /**
     * adds parameters to the request
     * @param parameters "name-to-value" map of parameters; if a value is a file, the file is uploaded, otherwise it is stringified and sent in the request
     * @throws IOException
     */
    public void setParameters(Map parameters) throws IOException {
        if (parameters == null) return;
        for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) {
            Map.Entry entry = (Map.Entry)i.next();
            setParameter(entry.getKey().toString(), entry.getValue());
        }
    }
    
    /**
     * adds parameters to the request
     * @param parameters array of parameter names and values (parameters[2*i] is a name, parameters[2*i + 1] is a value); if a value is a file, the file is uploaded, otherwise it is stringified and sent in the request
     * @throws IOException
     */
    public void setParameters(Object[] parameters) throws IOException {
        if (parameters == null) return;
        for (int i = 0; i < parameters.length - 1; i+=2) {
            setParameter(parameters[i].toString(), parameters[i+1]);
        }
    }
    
    /**
     * posts the requests to the server, with all the cookies and parameters that were added
     * @return input stream with the server response
     * @throws IOException
     */
    public InputStream post() throws IOException {
        boundary();
        writeln("--");
        os.close();
        return connection.getInputStream();
    }
    
    /**
     * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument
     * @param parameters request parameters
     * @return input stream with the server response
     * @throws IOException
     * @see setParameters
     */
    public InputStream post(Map parameters) throws IOException {
        setParameters(parameters);
        return post();
    }
    
    /**
     * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument
     * @param parameters request parameters
     * @return input stream with the server response
     * @throws IOException
     * @see setParameters
     */
    public InputStream post(Object[] parameters) throws IOException {
        setParameters(parameters);
        return post();
    }
    
    /**
     * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments
     * @param cookies request cookies
     * @param parameters request parameters
     * @return input stream with the server response
     * @throws IOException
     * @see setParameters
     * @see setCookies
     */
    public InputStream post(Map cookies, Map parameters) throws IOException {
        setCookies(cookies);
        setParameters(parameters);
        return post();
    }
    
    /**
     * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments
     * @param cookies request cookies
     * @param parameters request parameters
     * @return input stream with the server response
     * @throws IOException
     * @see setParameters
     * @see setCookies
     */
    public InputStream post(String[] cookies, Object[] parameters) throws IOException {
        setCookies(cookies);
        setParameters(parameters);
        return post();
    }
    
    /**
     * post the POST request to the server, with the specified parameter
     * @param name parameter name
     * @param value parameter value
     * @return input stream with the server response
     * @throws IOException
     * @see setParameter
     */
    public InputStream post(String name, Object value) throws IOException {
        setParameter(name, value);
        return post();
    }
    
    /**
     * post the POST request to the server, with the specified parameters
     * @param name1 first parameter name
     * @param value1 first parameter value
     * @param name2 second parameter name
     * @param value2 second parameter value
     * @return input stream with the server response
     * @throws IOException
     * @see setParameter
     */
    public InputStream post(String name1, Object value1, String name2, Object value2) throws IOException {
        setParameter(name1, value1);
        return post(name2, value2);
    }
    
    /**
     * post the POST request to the server, with the specified parameters
     * @param name1 first parameter name
     * @param value1 first parameter value
     * @param name2 second parameter name
     * @param value2 second parameter value
     * @param name3 third parameter name
     * @param value3 third parameter value
     * @return input stream with the server response
     * @throws IOException
     * @see setParameter
     */
    public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException {
        setParameter(name1, value1);
        return post(name2, value2, name3, value3);
    }
    
    /**
     * post the POST request to the server, with the specified parameters
     * @param name1 first parameter name
     * @param value1 first parameter value
     * @param name2 second parameter name
     * @param value2 second parameter value
     * @param name3 third parameter name
     * @param value3 third parameter value
     * @param name4 fourth parameter name
     * @param value4 fourth parameter value
     * @return input stream with the server response
     * @throws IOException
     * @see setParameter
     */
    public InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException {
        setParameter(name1, value1);
        return post(name2, value2, name3, value3, name4, value4);
    }
    
    /**
     * posts a new request to specified URL, with parameters that are passed in the argument
     * @param parameters request parameters
     * @return input stream with the server response
     * @throws IOException
     * @see setParameters
     */
    public static InputStream post(URL url, Map parameters) throws IOException {
        return new ClientHttpRequest(url).post(parameters);
    }
    
    /**
     * posts a new request to specified URL, with parameters that are passed in the argument
     * @param parameters request parameters
     * @return input stream with the server response
     * @throws IOException
     * @see setParameters
     */
    public static InputStream post(URL url, Object[] parameters) throws IOException {
        return new ClientHttpRequest(url).post(parameters);
    }
    
    /**
     * posts a new request to specified URL, with cookies and parameters that are passed in the argument
     * @param cookies request cookies
     * @param parameters request parameters
     * @return input stream with the server response
     * @throws IOException
     * @see setCookies
     * @see setParameters
     */
    public static InputStream post(URL url, Map cookies, Map parameters) throws IOException {
        return new ClientHttpRequest(url).post(cookies, parameters);
    }
    
    /**
     * posts a new request to specified URL, with cookies and parameters that are passed in the argument
     * @param cookies request cookies
     * @param parameters request parameters
     * @return input stream with the server response
     * @throws IOException
     * @see setCookies
     * @see setParameters
     */
    public static InputStream post(URL url, String[] cookies, Object[] parameters) throws IOException {
        return new ClientHttpRequest(url).post(cookies, parameters);
    }
    
    /**
     * post the POST request specified URL, with the specified parameter
     * @param name parameter name
     * @param value parameter value
     * @return input stream with the server response
     * @throws IOException
     * @see setParameter
     */
    public static InputStream post(URL url, String name1, Object value1) throws IOException {
        return new ClientHttpRequest(url).post(name1, value1);
    }
    
    /**
     * post the POST request to specified URL, with the specified parameters
     * @param name1 first parameter name
     * @param value1 first parameter value
     * @param name2 second parameter name
     * @param value2 second parameter value
     * @return input stream with the server response
     * @throws IOException
     * @see setParameter
     */
    public static InputStream post(URL url, String name1, Object value1, String name2, Object value2) throws IOException {
        return new ClientHttpRequest(url).post(name1, value1, name2, value2);
    }
    
    /**
     * post the POST request to specified URL, with the specified parameters
     * @param name1 first parameter name
     * @param value1 first parameter value
     * @param name2 second parameter name
     * @param value2 second parameter value
     * @param name3 third parameter name
     * @param value3 third parameter value
     * @return input stream with the server response
     * @throws IOException
     * @see setParameter
     */
    public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3) throws IOException {
        return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3);
    }
    
    /**
     * post the POST request to specified URL, with the specified parameters
     * @param name1 first parameter name
     * @param value1 first parameter value
     * @param name2 second parameter name
     * @param value2 second parameter value
     * @param name3 third parameter name
     * @param value3 third parameter value
     * @param name4 fourth parameter name
     * @param value4 fourth parameter value
     * @return input stream with the server response
     * @throws IOException
     * @see setParameter
     */
    public static InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) throws IOException {
        return new ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3, name4, value4);
    }
}


Also - wie komme ich an den String ran?

Vielen Dank!
MFG
Rex
 

Leroy42

Top Contributor
Code:
String serverresponse = serverInput.toString();

Die toString-Methode liefert nur eine interne Repräsentation des Stringobjekts.

Du mußt den String schon selbst auslesen.

Und bevor du mal wieder einen neuen Thread aufmachst, hier eine Routine:

Code:
public String liesAus(InputStream is) throws IOException {
		StringBuffer sb = new StringBuffer();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String line;
		while ((line = br.readLine()) != null)
			sb.append(line);
		return sb.toString();
	}
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
X Input/Output InputStream/Scanner(System.in) read()/hasNextLine() block unterbrechen Allgemeine Java-Themen 7
krgewb Problem mit Umlauten und Eszett bei InputStream Allgemeine Java-Themen 3
W Mal ein paar generelle Fragen zu InputStream und OutputStream Allgemeine Java-Themen 4
L Input/Output InputStream öffnen? Allgemeine Java-Themen 12
P Input/Output Inputstream verhält sich verschieden Allgemeine Java-Themen 9
R Inputstream lesen mit Pointer Allgemeine Java-Themen 0
M InputStream auslesen Allgemeine Java-Themen 1
M String aus InputStream Allgemeine Java-Themen 3
C Threading mit BufferedReader/InputStream & sockets Allgemeine Java-Themen 0
B BufferedWriter in InputStream oder Zeichen-Stream in Byte-Stream Allgemeine Java-Themen 5
P Scanner als Inputstream für Java Prozess Allgemeine Java-Themen 4
E POI-InputStream selber wählen Allgemeine Java-Themen 2
S RandomAcessFile das einen InputStream wrappt..? Allgemeine Java-Themen 2
M Umwandlung ByteArrayOutputStream in InputStream Allgemeine Java-Themen 5
1 InputStream hängt Allgemeine Java-Themen 4
H Input/Output InputStream: 32-Bit-Ganzzahl (DWORD) binär lesen Allgemeine Java-Themen 5
D Datentypen Reader/InputStream wiederverwenden? Allgemeine Java-Themen 11
K Inputstream -> Bilder Allgemeine Java-Themen 23
1 InputStream liest die letzte Zeile nicht ein Allgemeine Java-Themen 2
J Java Datei aus FTP in ein INputstream Allgemeine Java-Themen 2
J InputStream Zip-File Allgemeine Java-Themen 3
F ImageIO cant create InputStream Allgemeine Java-Themen 9
S InputStream "weiterleiten" Allgemeine Java-Themen 12
T Integer aus InputStream lesen Allgemeine Java-Themen 3
T Undwandlung eines String in einen InputStream? Allgemeine Java-Themen 3
M Selbst geschriebener InputStreamReader über einen beliebigen InputStream Allgemeine Java-Themen 4
T OutputStream to InputStream Allgemeine Java-Themen 8
S von OutputStream in InputStream schreiben ohne das es blockiert..? Allgemeine Java-Themen 2
reibi BufferedImage nach InputStream Allgemeine Java-Themen 10
R InputStream zweimal lesen Allgemeine Java-Themen 2
G InputStream und File Allgemeine Java-Themen 11
B InputStream zu URL umwandeln Allgemeine Java-Themen 2
F Teile vom InputStream schreiben Allgemeine Java-Themen 9
J InputStream aus URL funktioniert nicht Allgemeine Java-Themen 7
H InputStream Problem mit externem Kommandozeilenprogramm Allgemeine Java-Themen 2
W InputStream öfters nutzen Allgemeine Java-Themen 9
R The mark method of InputStream does nothing. Allgemeine Java-Themen 4
V String oder doch InputStream? Allgemeine Java-Themen 9
A Inputstream in Outputstream umleiten Allgemeine Java-Themen 7
Nils_Langner InputStream soll sich bei neuen Daten melden Allgemeine Java-Themen 2
S java.io.InputStream.read(byte[] b) überschreiben Allgemeine Java-Themen 33
N InputStream erst ab gegebener Position Allgemeine Java-Themen 4
R InputStream Allgemeine Java-Themen 5
G InputStream kopieren? Allgemeine Java-Themen 6
D Inputstream aus StringBuffer Allgemeine Java-Themen 3
G inputstream() to imageicon() Allgemeine Java-Themen 7
D Durch OutpustStream und InputStream dateien in eine jar sch Allgemeine Java-Themen 3
G aus String einen InputStream erstellen Allgemeine Java-Themen 2
C Wie kann man im InputStream nach einer Zeichenkette suchen? Allgemeine Java-Themen 4
R Aus InputStream wieder eine Datei erzeugen? Allgemeine Java-Themen 2
N String als Inputstream Allgemeine Java-Themen 4
N Suchen in InputStream/ByteArrayOutputStream Allgemeine Java-Themen 11
M InputStream.read abbrechen Allgemeine Java-Themen 2
HerrBolte Seltsamer Fehler nur in der Windows- und nicht in der Java-Console O_O Allgemeine Java-Themen 16
T Seltsamer CaseSensitive Effekt bei getResource Allgemeine Java-Themen 1
I Seltsamer Ausgabefehler Allgemeine Java-Themen 5
E Seltsamer aufruf von java.util.Date.toString() Allgemeine Java-Themen 3
M Seltsamer Fehler bei GregorianCalendar Allgemeine Java-Themen 2
R Seltsamer Effekt beim initialisieren. Allgemeine Java-Themen 3
E Output Fehler (Java-Programm Kuchen) Allgemeine Java-Themen 11
harrytut Java Input/Output Tests Junit Allgemeine Java-Themen 3
Tobero Batch Vorgang zum output Allgemeine Java-Themen 3
B In Java Methode mit generic input und output basteln? Allgemeine Java-Themen 4
X Output von ArrayList Allgemeine Java-Themen 3
S Gradle "schluckt" output und error Allgemeine Java-Themen 1
S Unerwarteter Output (kleines Raetsel) Allgemeine Java-Themen 6
TheWhiteShadow 2D-Grafik GIF Library mit byte output Allgemeine Java-Themen 10
J Java Sound Api Sound input und Output auswählen Allgemeine Java-Themen 4
T ProcessBuilder Output Allgemeine Java-Themen 12
T Threads Input/Output im Thread - Datei ohne Inhalt Allgemeine Java-Themen 1
M Output einer anderen/externen .jar in eigene JTextArea Allgemeine Java-Themen 4
B Input/Output Server Startet, Jedoch Kein Output. Allgemeine Java-Themen 1
N Kalender Normen input/output Allgemeine Java-Themen 2
B Best Practice HTML Output Optimal implementieren Allgemeine Java-Themen 3
H Input/Output Binäre Daten konvertieren, Output hat Zeilenumbrüche?? Allgemeine Java-Themen 9
G LayoutManager Launch4j: Launcher startet nur in Output Directory Allgemeine Java-Themen 4
G Swing JEditorPaneAppender (Output im HTML-Format) Allgemeine Java-Themen 12
H Binary File Output Allgemeine Java-Themen 15
G Output eines über Runtime.getRuntime.exec gestarteten Jars Allgemeine Java-Themen 6
W In Runtime Output schreiben Allgemeine Java-Themen 5
O Output From Java Creator? Allgemeine Java-Themen 3
J Threads + Dualcore = Probleme mit dem Output auf der Konsole Allgemeine Java-Themen 12
N IOException: "Reading from an output buffer" ? Allgemeine Java-Themen 2
K Output eines Runtimes Allgemeine Java-Themen 2
C Process output Allgemeine Java-Themen 7
M Output Stream / Protokoll does not support output Allgemeine Java-Themen 2
J Output Stream Allgemeine Java-Themen 4

Ähnliche Java Themen


Oben