当前位置: 首页>>代码示例>>Java>>正文


Java HttpURLConnection.HTTP_MOVED_TEMP属性代码示例

本文整理汇总了Java中java.net.HttpURLConnection.HTTP_MOVED_TEMP属性的典型用法代码示例。如果您正苦于以下问题:Java HttpURLConnection.HTTP_MOVED_TEMP属性的具体用法?Java HttpURLConnection.HTTP_MOVED_TEMP怎么用?Java HttpURLConnection.HTTP_MOVED_TEMP使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在java.net.HttpURLConnection的用法示例。


在下文中一共展示了HttpURLConnection.HTTP_MOVED_TEMP属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: checkRedirect

private static URLConnection checkRedirect(URLConnection conn, int timeout) throws IOException {
    if (conn instanceof HttpURLConnection) {
        conn.connect();
        int code = ((HttpURLConnection) conn).getResponseCode();
        if (code == HttpURLConnection.HTTP_MOVED_TEMP
                || code == HttpURLConnection.HTTP_MOVED_PERM) {
            // in case of redirection, try to obtain new URL
            String redirUrl = conn.getHeaderField("Location"); //NOI18N
            if (null != redirUrl && !redirUrl.isEmpty()) {
                //create connection to redirected url and substitute original conn
                URL redirectedUrl = new URL(redirUrl);
                URLConnection connRedir = redirectedUrl.openConnection();
                // XXX is this neede
                connRedir.setRequestProperty("User-Agent", "NetBeans"); // NOI18N
                connRedir.setConnectTimeout(timeout);
                connRedir.setReadTimeout(timeout);
                if (connRedir instanceof HttpsURLConnection) {
                    NetworkAccess.initSSL((HttpsURLConnection) connRedir);
                }
                return connRedir;
            }
        }
    }
    return conn;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:25,代码来源:NetworkAccess.java

示例2: handleRedirect

private static URLConnection handleRedirect(URLConnection urlConnection, int retriesLeft) throws IOException
{
    if (retriesLeft == 0)
    {
        throw new IOException("too many redirects connecting to "+urlConnection.getURL().toString());
    }
    if (urlConnection instanceof HttpURLConnection)
    {
        HttpURLConnection conn = (HttpURLConnection) urlConnection;

        int status = conn.getResponseCode();
        if (status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_SEE_OTHER)
        {
            String newUrl = conn.getHeaderField("Location");
            return handleRedirect(new URL(newUrl).openConnection(), retriesLeft - 1);
        }
    }
    return urlConnection;
}
 
开发者ID:goldmansachs,项目名称:jrpip,代码行数:19,代码来源:Libboot.java

示例3: linkIsReview

static boolean linkIsReview(String link)
{
    try
    {
        HttpURLConnection con = (HttpURLConnection) new URL(link).openConnection();
        HttpsURLConnection sslCon = (HttpsURLConnection) con;
        sslCon.setInstanceFollowRedirects(false);
        sslCon.connect();
        if(con.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP)
        {
            String newUrl = con.getHeaderField("Location");
            newUrl = "https://nl.trustpilot.com" + newUrl;
            con.disconnect();
            return true;
        }

    }
    catch (Exception ex)
    {
        System.out.println(ex);
    }
    return false;
}
 
开发者ID:Pverweij,项目名称:TrustPilotFinder,代码行数:23,代码来源:Main.java

示例4: testHttpConnection

private static boolean testHttpConnection(URL url, Proxy proxy) throws IOException{
    boolean result = false;
    
    HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(proxy);
    // Timeout shorten to 5s
    httpConnection.setConnectTimeout(5000);
    httpConnection.connect();

    if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK || 
            httpConnection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
        result = true;
    }

    httpConnection.disconnect();
    
    return result;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:17,代码来源:GeneralOptionsModel.java

示例5: downloadViaHttp

private static CharSequence downloadViaHttp(String uri, String contentTypes, int maxChars) throws IOException {
    int redirects = 0;
    while (redirects < 5) {
        URL url = new URL(uri);
        HttpURLConnection connection = safelyOpenConnection(url);
        connection.setInstanceFollowRedirects(true); // Won't work HTTP -> HTTPS or vice versa
        connection.setRequestProperty("Accept", contentTypes);
        connection.setRequestProperty("Accept-Charset", "utf-8,*");
        connection.setRequestProperty("User-Agent", "ZXing (Android)");
        try {
            int responseCode = safelyConnect(connection);
            switch (responseCode) {
                case HttpURLConnection.HTTP_OK:
                    return consume(connection, maxChars);
                case HttpURLConnection.HTTP_MOVED_TEMP:
                    String location = connection.getHeaderField("Location");
                    if (location != null) {
                        uri = location;
                        redirects++;
                        continue;
                    }
                    throw new IOException("No Location");
                default:
                    throw new IOException("Bad HTTP response: " + responseCode);
            }
        } finally {
            connection.disconnect();
        }
    }
    throw new IOException("Too many redirects");
}
 
开发者ID:xiong-it,项目名称:ZXingAndroidExt,代码行数:31,代码来源:HttpHelper.java

示例6: isRedirect

private static boolean isRedirect(int code) {
    return code == HttpURLConnection.HTTP_MOVED_PERM
            || code == HttpURLConnection.HTTP_MOVED_TEMP
            || code == HttpURLConnection.HTTP_SEE_OTHER
            || code == HttpURLConnection.HTTP_MULT_CHOICE
            || code == HTTP_TEMPORARY_REDIRECT
            || code == HTTP_PERMANENT_REDIRECT;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:8,代码来源:RedirectHandler.java

示例7: hasMoved

/**
 * Returns true if the status code implies the resource has moved
 * @param statusCode    the HTTP status code
 * @return              true if resource has moved
 */
private boolean hasMoved(int statusCode) {
    switch (statusCode) {
        case HttpURLConnection.HTTP_MOVED_TEMP: return true;
        case HttpURLConnection.HTTP_MOVED_PERM: return true;
        case HttpURLConnection.HTTP_SEE_OTHER:  return true;
        default:                                return false;
    }
}
 
开发者ID:zavtech,项目名称:morpheus-core,代码行数:13,代码来源:HttpClient.java

示例8: downloadViaHttp

private static CharSequence downloadViaHttp(String uri, String contentTypes, int maxChars) throws IOException {
  int redirects = 0;
  while (redirects < 5) {
    URL url = new URL(uri);
    HttpURLConnection connection = safelyOpenConnection(url);
    connection.setInstanceFollowRedirects(true); // Won't work HTTP -> HTTPS or vice versa
    connection.setRequestProperty("Accept", contentTypes);
    connection.setRequestProperty("Accept-Charset", "utf-8,*");
    connection.setRequestProperty("User-Agent", "ZXing (Android)");
    try {
      int responseCode = safelyConnect(uri, connection);
      switch (responseCode) {
        case HttpURLConnection.HTTP_OK:
          return consume(connection, maxChars);
        case HttpURLConnection.HTTP_MOVED_TEMP:
          String location = connection.getHeaderField("Location");
          if (location != null) {
            uri = location;
            redirects++;
            continue;
          }
          throw new IOException("No Location");
        default:
          throw new IOException("Bad HTTP response: " + responseCode);
      }
    } finally {
      connection.disconnect();
    }
  }
  throw new IOException("Too many redirects");
}
 
开发者ID:PhilippC,项目名称:keepass2android,代码行数:31,代码来源:HttpHelper.java

示例9: unredirect

public static URI unredirect(URI uri) throws IOException {
  if (!REDIRECTOR_DOMAINS.contains(uri.getHost())) {
    return uri;
  }
  URL url = uri.toURL();
  HttpURLConnection connection = safelyOpenConnection(url);
  connection.setInstanceFollowRedirects(false);
  connection.setDoInput(false);
  connection.setRequestMethod("HEAD");
  connection.setRequestProperty("User-Agent", "ZXing (Android)");
  try {
    int responseCode = safelyConnect(uri.toString(), connection);
    switch (responseCode) {
      case HttpURLConnection.HTTP_MULT_CHOICE:
      case HttpURLConnection.HTTP_MOVED_PERM:
      case HttpURLConnection.HTTP_MOVED_TEMP:
      case HttpURLConnection.HTTP_SEE_OTHER:
      case 307: // No constant for 307 Temporary Redirect ?
        String location = connection.getHeaderField("Location");
        if (location != null) {
          try {
            return new URI(location);
          } catch (URISyntaxException e) {
            // nevermind
          }
        }
    }
    return uri;
  } finally {
    connection.disconnect();
  }
}
 
开发者ID:PhilippC,项目名称:keepass2android,代码行数:32,代码来源:HttpHelper.java

示例10: isHttpRedirect

private static boolean isHttpRedirect(int responseCode) {
    switch (responseCode) {
        case HttpURLConnection.HTTP_MULT_CHOICE:
        case HttpURLConnection.HTTP_MOVED_PERM:
        case HttpURLConnection.HTTP_MOVED_TEMP:
        case HttpURLConnection.HTTP_SEE_OTHER:
        case HTTP_TEMPORARY_REDIRECT:
        case HTTP_PERMANENT_REDIRECT:
            return true;
        default:
            return false;
    }
}
 
开发者ID:Sunzxyong,项目名称:Tiny,代码行数:13,代码来源:HttpUrlConnectionFetcher.java

示例11: getFinalURL

public static String getFinalURL(String url) throws IOException {
    HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
    con.setInstanceFollowRedirects(false);
    con.connect();
    con.getInputStream();

    if (con.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
        String redirectUrl = con.getHeaderField("Location");
        return getFinalURL(redirectUrl);
    }
    return url;
}
 
开发者ID:89luca89,项目名称:ThunderMusic,代码行数:12,代码来源:BandcampLinkRetriever.java

示例12: unredirect

public static URI unredirect(URI uri) throws IOException {
  if (!REDIRECTOR_DOMAINS.contains(uri.getHost())) {
    return uri;
  }
  URL url = uri.toURL();
  HttpURLConnection connection = safelyOpenConnection(url);
  connection.setInstanceFollowRedirects(false);
  connection.setDoInput(false);
  connection.setRequestMethod("HEAD");
  connection.setRequestProperty("User-Agent", "ZXing (Android)");
  try {
    int responseCode = safelyConnect(connection);
    switch (responseCode) {
      case HttpURLConnection.HTTP_MULT_CHOICE:
      case HttpURLConnection.HTTP_MOVED_PERM:
      case HttpURLConnection.HTTP_MOVED_TEMP:
      case HttpURLConnection.HTTP_SEE_OTHER:
      case 307: // No constant for 307 Temporary Redirect ?
        String location = connection.getHeaderField("Location");
        if (location != null) {
          try {
            return new URI(location);
          } catch (URISyntaxException e) {
            // nevermind
          }
        }
    }
    return uri;
  } finally {
    connection.disconnect();
  }
}
 
开发者ID:amap-demo,项目名称:weex-3d-map,代码行数:32,代码来源:HttpHelper.java

示例13: isRedirection

private static boolean isRedirection(int code) {
    return code == HttpURLConnection.HTTP_MOVED_PERM
            || code == HttpURLConnection.HTTP_MOVED_TEMP
            || code == HttpURLConnection.HTTP_SEE_OTHER
            || code == HttpURLConnection.HTTP_MULT_CHOICE
            || code == Constants.HTTP_TEMPORARY_REDIRECT
            || code == Constants.HTTP_PERMANENT_REDIRECT;
}
 
开发者ID:MindorksOpenSource,项目名称:PRDownloader,代码行数:8,代码来源:Utils.java

示例14: handle

@Override
public void handle(HttpExchange exchange) throws IOException {
  Map<String, String> queryParameters = getQueryParameters(exchange);
  String statusParam = queryParameters.get("status");
  int statusCode =
      (statusParam != null) ? Integer.parseInt(statusParam) : HttpURLConnection.HTTP_OK;

  String requestBody = streamToString(exchange.getRequestBody());

  String body = exchange.getRequestURI().toString();
  body += "\n";
  body += "\n";
  body += requestBody;

  byte[] response = body.getBytes();
  if (statusCode == HttpURLConnection.HTTP_MOVED_TEMP) {
    String redirectLocation = queryParameters.get("rd");
    if (redirectLocation == null) {
      statusCode = HttpURLConnection.HTTP_BAD_REQUEST;
    } else {
      exchange.getResponseHeaders().add("Location", redirectLocation);
    }
  }
  exchange.sendResponseHeaders(statusCode, response.length);
  OutputStream outputStream = exchange.getResponseBody();
  outputStream.write(response);
  outputStream.close();
  exchange.close();
}
 
开发者ID:ApptuitAI,项目名称:JInsight,代码行数:29,代码来源:TestWebServer.java

示例15: unredirect

public static URI unredirect(URI uri) throws IOException {
    if (!REDIRECTOR_DOMAINS.contains(uri.getHost())) {
        return uri;
    }
    URL url = uri.toURL();
    HttpURLConnection connection = safelyOpenConnection(url);
    connection.setInstanceFollowRedirects(false);
    connection.setDoInput(false);
    connection.setRequestMethod("HEAD");
    connection.setRequestProperty("User-Agent", "ZXing (Android)");
    try {
        int responseCode = safelyConnect(connection);
        switch (responseCode) {
            case HttpURLConnection.HTTP_MULT_CHOICE:
            case HttpURLConnection.HTTP_MOVED_PERM:
            case HttpURLConnection.HTTP_MOVED_TEMP:
            case HttpURLConnection.HTTP_SEE_OTHER:
            case 307: // No constant for 307 Temporary Redirect ?
                String location = connection.getHeaderField("Location");
                if (location != null) {
                    try {
                        return new URI(location);
                    } catch (URISyntaxException e) {
                        // nevermind
                    }
                }
        }
        return uri;
    } finally {
        connection.disconnect();
    }
}
 
开发者ID:xiong-it,项目名称:ZXingAndroidExt,代码行数:32,代码来源:HttpHelper.java


注:本文中的java.net.HttpURLConnection.HTTP_MOVED_TEMP属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。