I'm using a HttpsURLConnection to connect to our server in order to get data using post method. The problem I'm facing is that from time to time I receive an EOFException.
I've done some research and I've tried using for example connection.setRequestProperty("Connection", "close"); but when I set this property among the others I am not able to connect to the server, receiving always this error java.net.SocketException: Socket is closed.
And now I'm trying to use solve this problem using System.setProperty("http.keepAlive", "false"); but I'm not completely sure where I have to set this property. I'm setting it at the beginning of the method which connects to the server.
Here you have the most relevant part of my code:
HttpsURLConnection conn = null;
RESTResponse response = null;
int status = -1;
try {
List<NameValuePair> params = request.getParameters();
String uri = request.getRequestUri().toString();
if (request.getMethod() == RESTMethods.GET) {
if (params != null) {
uri += "?";
boolean first_param = true;
for (NameValuePair p : params) {
if (first_param)
first_param = false;
else
uri += "&";
uri += p.getName() + "="
+ URLEncoder.encode(p.getValue(), "UTF-8");
}
}
}
Security.addProvider(new BouncyCastleProvider());
char[] passphrase = "mypass".toCharArray();
try {
KeyStore ksTrust = KeyStore.getInstance("BKS");
ksTrust.load(context.getResources().openRawResource(R.raw.mystore),passphrase);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
tmf.init(ksTrust);
// Create a SSLContext with the certificate
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), new SecureRandom());
Log.v(TAG,"URI = " + uri);
URL url = new URL(uri);
conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(sslContext.getSocketFactory());
conn.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
if (request.getHeaders() != null) {
for (String header : request.getHeaders().keySet()) {
for (String value : request.getHeaders().get(header)) {
conn.addRequestProperty(header, value);
}
}
}
switch (request.getMethod()) {
case GET:
conn.setDoOutput(false);
break;
case POST:
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Connection", "close"); // disables connection reuse but getting ERROR
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(getQuery(params));
wr.flush();
wr.close();
default:
break;
}
try {
conn.connect();
status = conn.getResponseCode(); //Receiving EOFException when Connection close not set
} catch (IOException ex1) {
//check if it's eof, if yes retrieve code again
ex1.printStackTrace();
// handle exception
}
Thanks in advance!
conn.setDoOutput(false)is the default for GET, andconn.setDoOutput(true)is the default for PUT.conn.setDoInput(true)is the default for both.application/x-www-form-urlencoded"is the default for PUT. 2. Forcing the server to close the connection isn't going to cure anEOFException. You're barking up the wrong tree. 3. Writing output before you do the connect doesn't make sense. 4. 'retrieve code again' in the case of anEOFExceptiondoesn't make sense. In short none of your code makes sense, so it isn't surprising that it doesn't work.