【问题标题】:Android to WCF connection reset by peer对等方重置 Android 到 WCF 的连接
【发布时间】:2018-02-13 13:30:20
【问题描述】:

我有一个使用 WCF 服务的 android 应用程序,这是我来自AsyncTask 的代码 sn-p:

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;
        }

    }

主要是它正确连接到此服务,但有时会出现一些异常,收到此消息:

I/O 错误:recvfrom 失败:ECONNRESET(对等方重置连接); 嵌套异常是 java.net.SocketException: recvfrom failed: ECONNRESET(对等方重置连接)

什么可能导致这个错误?

注意:

很长一段时间都运行良好,但现在我经常遇到这个问题。是 android 问题还是我会在 WCF 端搜索问题?

【问题讨论】:

    标签: java android wcf spring-rest


    【解决方案1】:

    在连接前尝试设置 HttpURLConnection:

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

    您也可以查看this link。 Android 会维护连接池并首先使用旧的连接池,因此通过设置此属性,您将禁用默认启用的 keep-alive 属性。

    为此,您必须创建RestTemplateHttpComponentsClientHttpRequestFactor

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

    Check this link

    and this one

    【讨论】:

    • 我在哪里写这个?能解释清楚点吗?
    • 我不明白在我的 sn-p 中将这段代码插入到哪里:/
    • 你必须写RestTamplate类。检查参考链接的答案
    猜你喜欢
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多