forum.boolean.name

forum.boolean.name (http://forum.boolean.name/index.php)
-   Программирование (http://forum.boolean.name/forumdisplay.php?f=54)
-   -   HttpConnection, OutputStream IOException (http://forum.boolean.name/showthread.php?t=14434)

Doctor Drive 17.03.2011 16:49

HttpConnection, OutputStream IOException
 
При отсылании больше ~1024 байт выскакивает IOException - Couldn't write to socket

Код:

        submission = new Thread() {
            public void run() {
 
                StringBuffer request = new StringBuffer("api_sig=").append(api_sig);
 
                Enumeration keys = lib.keys();
                String key;
                while (keys.hasMoreElements()) {
                    request.append("&").append(URICoder.encodeSequence(key = (String)keys.nextElement()));
                    request.append("=").append(URICoder.encodeSequence((String)lib.get(key)));
                }
                keys = null;
                lib = null;
 
 
                byte[] data = null;
                try {
                    data = (request.toString()).getBytes("UTF-8");
                } catch (UnsupportedEncodingException uee) {
                    data = (request.toString()).getBytes();
                }
 
                HttpConnection net = null;
                ByteArrayOutputStream out = null;
                InputStream is = null;
                OutputStream output = null;
                try {
 
                    net = (HttpConnection)Connector.open("http://ws.audioscrobbler.com/2.0/", Connector.READ_WRITE, false);
 
                    net.setRequestMethod(HttpConnection.POST);
                    net.setRequestProperty("Host", "ws.audioscrobbler.com");
                    net.setRequestProperty("User-Agent", "LastFM log Scrobbler");
                    net.setRequestProperty("Referer", "ws.audioscrobbler.com");
                    net.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    net.setRequestProperty("Content-Length", Long.toString(data.length));
 
 
                    output = net.openOutputStream();
                    output.write(data);
 
 
                    int responsecode = net.getResponseCode();
                    if (responsecode != 200) Main.main.log.write("HTTP " + responsecode + " " + net.getResponseMessage());
                    if (responsecode == 200) {
                        is = net.openInputStream();
 
                        out = new ByteArrayOutputStream();
                        byte[] buffer = new byte[512];
                        for(;;) {
                            int copy = is.read(buffer);
                            if(copy < 0) break;
                            else out.write(buffer, 0, copy);
                        }
 
                        try {
                            submission_response = new String(out.toByteArray(), "UTF-8");
                        } catch (UnsupportedEncodingException uee) {
                            submission_response = new String(out.toByteArray());
                        }
 
                    } else {
                        Main.main.log.write("Bad Response");
                    }
                } catch (IOException ioe) {
                    done = true;
                    Main.main.log.write("Cannot post data");
                    Main.main.log.write(ioe.getMessage());
                } finally {
                    if(is != null) { try { is.close(); } catch(IOException e) { } }
                    if(out != null) { try { out.close(); } catch(IOException e) { } }
                    if(output != null) { try { output.close(); } catch(IOException e) { } }
                    if(net != null) { try { net.close(); } catch(IOException e) { } }
                }
                done = true;
            }
        };


ПЕРЕБРОБОВАЛ:

DataOutputStream вместо OutputStream
Убрать хедер Content-Length
Писать по 512 байт

Так
Код:

                    output.write(data, 0, data.length)
И так
Код:

                for (int i = 0; i < data.length; i++)
                    output.write((int)data[i]);

Так
Код:

                    for (int i = 0; i < data.length; i++) {
                        try { output.write(data[i]); }
                        catch (IOException ioe) {
                            i--;
                        }

Т даже так
Код:

                    for (int i = 0; i < data.length; i++) {
                        try { output.write(data[i]); }
                        catch (IOException ioe) {
                            output.close();
                            output = net.openOutputStream();
                            i--;
                        }
                    }

Что безрезультатно.

odd 19.03.2011 16:27

Ответ: HttpConnection, OutputStream IOException
 
byte[] buffer = new byte[512];

Ты буфер для отправки информации делаешь размером 512 байт. В этом и проблема. Увеличь размер буфера.

Doctor Drive 19.03.2011 21:27

Ответ: HttpConnection, OutputStream IOException
 
Цитата:

Сообщение от odd (Сообщение 183264)
byte[] buffer = new byte[512];

Ты буфер для отправки информации делаешь размером 512 байт. В этом и проблема. Увеличь размер буфера.

Это буфер для считывания ведь о_О

Ну хорошо, сделал и буфер для отправки

Код:

                    output = net.openOutputStream();

                    bais = new ByteArrayInputStream(data);
                    byte[] buf = new byte[1024];
                    int off = 0;
                    int read;
                    while (true) {
                        read = bais.read(buf, 0, buf.length);
                        if (read < 0) break;
                        else {
                            off += read;
                            output.write(buf, 0, read);
                        }
                    }
                    bais.close();

Но это ничего не изменило.


Часовой пояс GMT +4, время: 21:56.

vBulletin® Version 3.6.5.
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Перевод: zCarot