godot
godot

Reputation: 3545

Android to WCF connection reset by peer

I have android application which uses WCF services, here is my code snippet from AsyncTask:

private HttpEntity<HashMap> request(){
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Cookie", LocalData.Web.getCookie(this.context));

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

        HttpEntity<HashMap> requestDataEntity = new HttpEntity<>(request, requestHeaders);

        HttpEntity<HashMap> response = restTemplate.exchange(this.url, this.httpMethod, requestDataEntity, HashMap.class);


//        HttpHeaders headers = response.getHeaders();
//        LocalData.Web.storeCookie(headers, context);

        return response;
    }

    @Override
    protected HashMap doInBackground(Void... voids) {
        try{
            return request().getBody();
        }
        catch (Exception e){
            Log.e("error", e.getMessage());
            return null;
        }

    }

mainly it connects properly to this service, but sometimes here occurs some exception which gets this message:

I/O error: recvfrom failed: ECONNRESET (Connection reset by peer); nested exception is java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

what may cause this error?

Note:

It was worked fine long time, but now I often have this problem. Is it a android problem or I would search problem in WCF side?

Upvotes: 0

Views: 427

Answers (1)

Abdul Kawee
Abdul Kawee

Reputation: 2727

try setting for HttpURLConnection before connecting:

conn.setRequestProperty("connection", "close");

Also you can have a look at this link. Android maintains the pool of connections and uses the old one first, so by setting this property you will disable keep-alive property which is on by default.

Todo this you have to create RestTemplateHttpComponentsClientHttpRequestFactor

@Bean
RestTemplate restTemplate(SimpleClientHttpRequestFactory factory) {
   return new RestTemplate(factory);
}

Check this link

and this one

Upvotes: 1

Related Questions