Показать сообщение отдельно
Старый 17.03.2011, 16:49   #1
Doctor Drive
Нуждающийся
 
Аватар для Doctor Drive
 
Регистрация: 28.01.2009
Сообщений: 65
Написано одно полезное сообщение
(для 2 участников)
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--;
                        }
                    }
Что безрезультатно.

Последний раз редактировалось Doctor Drive, 19.03.2011 в 22:12.
(Offline)
 
Ответить с цитированием