SolArabehety
SolArabehety

Reputation: 8606

Maximum length of content?

I'm trying to connect with a server using HttpURLConnection, but I have a problem with the PUT method.
I need to send a String with 1500 characters (or more), but in this case the server produces a timeout and returns 500 - server internal error.
If I send a String lower than 1400 characters, I have not problem and the server returns OK.

My code is the following:

public String connectToServer(String prototype) {
    String responseString = "";

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(openURLForInput(new URL(URL), USERNAME, PASSWORD, prototype)));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
            responseString += line;
        }

    } catch (IOException e) {
        e.printStackTrace();
        responseString = e.toString();
    }

    return responseString;
}

//-----------------------

public InputStream openURLForInput(URL url, String uname, String pword, String content) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestProperty("Authorization", userNamePasswordBase64(uname, pword)); // I know this is OK
    conn.addRequestProperty("Content-type", "application/xml; charset=utf-8");

    //conn.setChunkedStreamingMode(8 * 1024);

    conn.setRequestMethod("PUT");
    conn.connect();

    OutputStream output = conn.getOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(output, "UTF-8");
    BufferedWriter writer = new BufferedWriter(osw);
    writer.write(content); // content length  > 1400 characters
    writer.close();
    output.close();

    int status = conn.getResponseCode();
    Log.i("STATUS", status + "");
    Log.i("STATUS_ERROR", conn.getResponseMessage());

    return conn.getInputStream();
}

I tryed adding the lines

conn.setFixedLengthStreamingMode(contentLength)
conn.setChunkedStreamingMode(8 * 1024);

But the server's answer is wrong anyway.

UPDATE:

I could detect the problem. For some reason, when I try to send large bodys in the request, the server produces a timeout but not with all networks, only with some networks. I use a secure conection SSL, maybe this can bring problems?

Upvotes: 4

Views: 3091

Answers (3)

Carlos Castillo
Carlos Castillo

Reputation: 907

Maybe it's a network MTU problem, I'd say you investigate on that front.

Here's a table relevant to windows:

Network             MTU (bytes)
-------------------------------
16 Mbps Token Ring        17914
4 Mbps Token Ring          4464
FDDI                       4352
Ethernet                   1500
IEEE 802.3/802.2           1492
PPPoE (WAN Miniport)       1480
X.25                        576

Upvotes: 2

DiogoSantana
DiogoSantana

Reputation: 2434

The server is responding 500 - server internal error. So you should check the server implementation. It should have restrictions above that content length.
I have a app that do a lot of server http requests and for me your problem really is the server. The main evidence is that your code works with content lower or equals then 1415 chars.

Upvotes: 0

StephaneT
StephaneT

Reputation: 454

I recommand you to use the very helpful HTTP-REQUEST library by Kevin Sawicki...

It helped me a lot of times !

Upvotes: 0

Related Questions