【问题标题】:HttpConnectionUrl always return status code 307HttpConnectionUrl 总是返回状态码 307
【发布时间】:2016-04-18 00:30:13
【问题描述】:

我正在尝试访问 Web 服务。它适用于 android 4.4 或 android 5.X。但是当我尝试使用 android 4.1.1 来点击“http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID"”时,它总是返回 307 状态码。但是这个 url 在 android 4.4 或 5.x 上运行良好。我也尝试点击其他 url 它工作正常安卓 4.1.1。 所以请告诉我是什么问题

Log.i(TAG, url);
        String response = null;
        HttpURLConnection conn = null;
        try {
            URL webServiceUrl = new URL(url);
            conn = (HttpURLConnection) webServiceUrl
                    .openConnection();
            Log.i(TAG, "Connection open");
            conn.setRequestMethod(GET);
            conn.setConnectTimeout(CONNECTION_TIME_OUT);
            conn.setRequestProperty(CONTENT_TYPE, contentType);
            conn.setRequestProperty(ACCEPT_TYPE, acceptType);
            conn.setDoInput(true);
            conn.connect();
            Log.i(TAG, "Connection Connected");

            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK && conn.getInputStream() != null) {
                response = StreamUtility.convertStreamToString(conn.getInputStream());
                conn.getInputStream().close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        return response;
    }

【问题讨论】:

  • 发生这种情况是因为标头在这里检查您的代码一次:- conn.setRequestProperty(TOKEN, header);确保您传递给在 API 客户端中确认的 Web 服务
  • 我没有在这里传递任何标题
  • 老兄,“conn”的所有设置操作都被视为标题。首先,在 REST API 客户端中检查您的网络服务,然后实现代码。

标签: android httpurlconnection httpconnection


【解决方案1】:

http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID/替换你的URL地址(注意最后/)。响应代码将为200


更新:

如果你当前的 URL 地址(http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID)最后没有/,你可以使用下面的代码:

String address = "http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID";
URL url = new URL(address);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setInstanceFollowRedirects(false);
// if print out (debug or logging), you will see secondURL has / at the end
URL secondURL = new URL(urlConnection.getHeaderField("Location"));
HttpURLConnection urlConnection1 = (HttpURLConnection) secondURL.openConnection();

然后使用urlConnection1.getResponseCode()

希望对你有帮助!

【讨论】:

  • 一个非常烦人的行为(错误?)。感谢您提供此代码。效果很好
【解决方案2】:

BNK 的回答有帮助,我这样修复它,以便它也适用于较新的设备(位置标头返回为空/空!)

String headerLocation = httpsUrlConnection.getHeaderField("Location");
logger.debug("Location header: " + headerLocation);

// if the redirect URL ends with a "/" sign, but the original URL does not, it's probably the redirect bug
String originalURL = url.toString();
if (!TextUtils.isEmpty(headerLocation) && headerLocation.endsWith("/") && !originalURL.endsWith("/"))
    {
    logger.info("Redirect Location differs from original URL, create new connection to: " + headerLocation);
    httpsUrlConnection = (HttpsURLConnection) new URL(headerLocation).openConnection();

    // optional
    httpsUrlConnection.setSSLSocketFactory(sslSocketFactory);
    }

【讨论】:

    猜你喜欢
    • 2019-02-18
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 2016-02-07
    • 2015-08-27
    相关资源
    最近更新 更多