2

我正在使用“https”从服务器调用 web 服务,我使用了简单的 HttpURLConnection 代码而不是 HttpsURLConnection,代码在平板电脑(OS 4.0.4)上运行 f9,但在我的设备(2.3.5)上却不行。

代码很简单:

  URLConnection urlConn = null;
  URL url = new URL("https://myurl");
   urlConn = null;
   urlConn = url.openConnection();


  if (!(urlConn instanceof HttpURLConnection)) {
        try {
            throw new IOException("URL is not an Http URL");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    HttpURLConnection httpConn = (HttpURLConnection) urlConn;
    httpConn.setAllowUserInteraction(false);
    httpConn.setInstanceFollowRedirects(true);
    try {
        httpConn.setRequestMethod("GET");
        httpConn.connect();
    } catch (ProtocolException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
        httpResponsecode = httpConn.getResponseCode();

如果我在设备上调用,httpresponsecode 为 400,但从平板电脑调用时为 200。

有什么建议么?

4

1 回答 1

1

我不认为 HttpURLConnection 支持在旧操作系统版本上进行以下重定向。我建议改用 apache HTTP 客户端,它的错误要少得多。

于 2012-09-22T13:46:34.870 回答