/* * Created by zhangwei on 2019/04/17. * Copyright 2015-2020 Sensors Data Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sensorsdata.analytics.android.sdk; import android.text.TextUtils; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; class SensorsDataHttpURLConnectionHelper { /** * HTTP 状态码 307 */ private static final int HTTP_307 = 307; static boolean needRedirects(int responseCode) { return responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HTTP_307; } static String getLocation(HttpURLConnection connection, String path) throws MalformedURLException { if (connection == null || TextUtils.isEmpty(path)) { return null; } String location = connection.getHeaderField("Location"); if (TextUtils.isEmpty(location)) { location = connection.getHeaderField("location"); } if (TextUtils.isEmpty(location)) { return null; } if (!(location.startsWith("http://") || location .startsWith("https://"))) { //某些时候会省略host,只返回后面的path,所以需要补全url URL originUrl = new URL(path); location = originUrl.getProtocol() + "://" + originUrl.getHost() + location; } return location; } }