Maximum length of content?

为君一笑 提交于 2019-12-20 01:33:11

问题


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?


回答1:


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



回答2:


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

It helped me a lot of times !




回答3:


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.



来源:https://stackoverflow.com/questions/15433844/maximum-length-of-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!