我正在尝试使用
我需要发送一个包含1500个字符或更多的字符串,但在这种情况下,服务器会产生超时并返回500-服务器内部错误。
如果我发送少于1400个字符的字符串,我就没有问题,并且服务器返回OK。
我尝试添加以下代码行:
HttpURLConnection
连接服务器,但是PUT方法有问题。我需要发送一个包含1500个字符或更多的字符串,但在这种情况下,服务器会产生超时并返回500-服务器内部错误。
如果我发送少于1400个字符的字符串,我就没有问题,并且服务器返回OK。
我的代码如下:
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();
}
我尝试添加以下代码行:
conn.setFixedLengthStreamingMode(contentLength)
conn.setChunkedStreamingMode(8 * 1024);
但是服务器的响应是错误的。
更新:
我已经发现了问题。由于某种原因,在我尝试发送大量请求时,服务器会产生超时但不是所有网络都有,只有一些网络。我使用了安全连接 SSL,这可能会带来问题?