【问题标题】:http post request get SocketTimeoutExceptionhttp post 请求获取 SocketTimeoutException
【发布时间】:2013-05-03 08:18:01
【问题描述】:

我正在尝试配置 HTTP POST 请求,但我一直收到SocketTimeoutException。 有人知道我的代码有什么问题吗?

int TIMEOUT_MILLISEC = 60000;  // = 60 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

client = new DefaultHttpClient(httpParams);

StringBuilder builder = new StringBuilder(URL_STRING); 

HttpPost request = new HttpPost(builder.toString());

request.setEntity(new ByteArrayEntity(jobj.toString().getBytes("UTF8")));

HttpResponse response = client.execute(request);

【问题讨论】:

  • 可能只是您的服务器没有响应?
  • 您的服务器是否监听默认端口?如果不。你指定端口了吗?您的服务器是否在 Web 浏览器上正确回答了请愿书?您期待什么样的回应?

标签: android


【解决方案1】:

参考此代码。它将帮助您了解处理几乎所有基本的 HttpClient 异常。 试试 {

    // check for request method
    if (method == "POST") {
        // request method is POST
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        // new
        HttpParams httpParameters = httpPost.getParams();
        // Set the timeout in milliseconds until a connection is
        // established.
        int timeoutConnection = 10000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 10000;
        HttpConnectionParams
                .setSoTimeout(httpParameters, timeoutSocket);
        // new
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } else if (method == "GET") {
        // request method is GET
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String paramString = URLEncodedUtils.format(params, "utf-8");
        url += "?" + paramString;
        HttpGet httpGet = new HttpGet(url);
        // new
        HttpParams httpParameters = httpGet.getParams();
        // Set the timeout in milliseconds until a connection is
        // established.
        int timeoutConnection = 10000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 10000;
        HttpConnectionParams
                .setSoTimeout(httpParameters, timeoutSocket);
        // new
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    }

} catch (UnsupportedEncodingException e) {
    throw new Exception("Unsupported encoding error.");
} catch (ClientProtocolException e) {
    throw new Exception("Client protocol error.");
} catch (SocketTimeoutException e) {
    throw new Exception("Sorry, socket timeout.");
} catch (ConnectTimeoutException e) {
    throw new Exception("Sorry, connection timeout.");
} catch (IOException e) {
    throw new Exception("I/O error(May be server down).");
}

【讨论】:

    【解决方案2】:

    无需设置套接字超时。 删除这一行:
    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

    【讨论】:

      猜你喜欢
      • 2017-10-07
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多