android - HttpConnectionUrl 始终返回状态代码 307

标签 android httpurlconnection httpconnection

我正在尝试访问网络服务。它在 android 4.4 或 android 5.X 上运行良好。但是当我尝试使用 android 4.1.1 点击“http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID” 时,它总是返回 307 状态代码。但这个网址在 android 4.4 或 5.x 上工作正常。我还尝试点击其他网址,它在 android 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;
    }

最佳答案

将您的 URL 地址替换为 http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID/(注意 /结束)。响应代码将为 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()

希望对你有帮助!

关于android - HttpConnectionUrl 始终返回状态代码 307,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34759805/

相关文章:

java - java 中的 httpurlConnection 与 httpParams

java - 从谷歌获取输入流时连接被拒绝

java - 如何从 GAE Java 通过代理发送基本的 HTTP GET?

java - Windows 与 Linux 的 http 身份验证。 javax.net.ssl.SSLPeerUnverifiedException : peer not authenticated

android - 谷歌地图当前位置标记和精度半径叠加

java - Android AsyncTask fragment 加载

java - 将图像从应用程序发送到网络服务器

安卓布局 : Game Board

android - 调用api时只得到一半的json日期

blackberry - 从黑莓模拟器打开 http 连接的问题