I make POST requests in my application and somtimes (if i have a huge amount of Post data), the following error occured:
avax.net.ssl.SSLException: Wr
I wasn't able to fix the problem with DefaultHttpClient, AndroidHttpClient or Abstract, but finally found a solution with HttpsUrlRequest ant Authentication via header instead of CredentielsService:
public static boolean upload_image5(String urls,File file, String encoding){
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String myfilename = file.getName();
String urlServer = urls;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
boolean erg = false;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try
{
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL(urlServer);
connection = (HttpsURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
connection.addRequestProperty("Authorization","Basic [YOUR MD5 LOGIN VALUE]");
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"DestFileName\"");
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(myfilename);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Target\"" );
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes("DOC");
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"filename\"");
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(myfilename);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"File\"; filename=\"" + myfilename + "\"");
outputStream.writeBytes(lineEnd);
outputStream.writeBytes("Content-Type: application/*");
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(lineEnd);
//hier File schreiben
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
try {
inputStream = new DataInputStream(connection.getInputStream());
StringBuilder response = new StringBuilder();
String line;
while ((line = inputStream.readLine()) != null) {
response.append(line).append('\n');
}
LastError =response.toString();
erg = true;
} catch (IOException e) {
LastError = e.getMessage();
erg = false;
} finally {
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
LastError = ex.getMessage();
erg = false;
}
return erg;
}
I've had the same problem using the DefaultHttpClient, well, using my own implementation of the httpclient to support https. Small files it was working fine and big files were failing time after time.
After reading this response I've changed to the HttpsURLConnection like the accepted answer suggests and also because it is recommended by android (http://android-developers.blogspot.pt/2011/09/androids-http-clients.html).
The problem stood. Turns out the issue was on the server, I've changed the configs from the PHP server before to accept bigger sizes, but I totally forgot to change the client_max_body_size
of the nginx. After making that small change sending big files was working. Through both HttpsUrlConnections and DefaultHttpClient.
I had got the same error. The problem is in the Android library where is use DefaultHttpClient has been around since Android API level 1 and AndroidHttpClient has been available since Android API level 8. This is bug in android https://code.google.com/p/android/issues/detail?id=8625
My problem was: The default timeout is 60 seconds. When I ran the connection in the Wireshark. It was created handshake and he has been waiting on the ApplicationData, but he didn't get it so after timeout send FIN and I have got :javax.net.ssl.SSLException: Write error: ssl=0x2f0610: I/O error during system call, Broken pipe.
I resolved my problem problem setting timeout the http connection on the 5 minutes or something value is longer than 60second. If I can recommended how resolve your problem run on the server Wireshark and listening the comunication with mobile device.