java.net.URLConnection.getHeaderField()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(841)

本文整理了Java中java.net.URLConnection.getHeaderField()方法的一些代码示例,展示了URLConnection.getHeaderField()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLConnection.getHeaderField()方法的具体详情如下:
包路径:java.net.URLConnection
类名称:URLConnection
方法名:getHeaderField

URLConnection.getHeaderField介绍

[英]Returns the header value at the field position pos or nullif the header has fewer than pos fields. The base implementation of this method returns always null.

Some implementations (notably HttpURLConnection) include a mapping for the null key; in HTTP's case, this maps to the HTTP status line and is treated as being at position 0 when indexing into the header fields.
[中]返回字段位置pos处的标题值,如果标题字段少于pos,则返回NULL。此方法的基本实现总是返回null。
一些实现(尤其是HttpURLConnection)包括空键的映射;在HTTP的情况下,这映射到HTTP状态行,并在索引到头字段时被视为处于位置0。

代码示例

代码示例来源:origin: stackoverflow.com

HttpURLConnection con = (HttpURLConnection)(new URL( url ).openConnection());
con.setInstanceFollowRedirects( false );
con.connect();
int responseCode = con.getResponseCode();
System.out.println( responseCode );
String location = con.getHeaderField( "Location" );
System.out.println( location );

代码示例来源:origin: stackoverflow.com

URL url = new URL(url);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
URLConnection conn = secondURL.openConnection();

代码示例来源:origin: apache/incubator-druid

@JsonCreator
public HttpFirehoseFactory(
  @JsonProperty("uris") List<URI> uris,
  @JsonProperty("maxCacheCapacityBytes") Long maxCacheCapacityBytes,
  @JsonProperty("maxFetchCapacityBytes") Long maxFetchCapacityBytes,
  @JsonProperty("prefetchTriggerBytes") Long prefetchTriggerBytes,
  @JsonProperty("fetchTimeout") Long fetchTimeout,
  @JsonProperty("maxFetchRetry") Integer maxFetchRetry
) throws IOException
{
 super(maxCacheCapacityBytes, maxFetchCapacityBytes, prefetchTriggerBytes, fetchTimeout, maxFetchRetry);
 this.uris = uris;
 Preconditions.checkArgument(uris.size() > 0, "Empty URIs");
 final URLConnection connection = uris.get(0).toURL().openConnection();
 final String acceptRanges = connection.getHeaderField(HttpHeaders.ACCEPT_RANGES);
 this.supportContentRange = acceptRanges != null && "bytes".equalsIgnoreCase(acceptRanges);
}

代码示例来源:origin: stackoverflow.com

URL url = new URL("JSPURL");
URLConnection conn = url.openConnection();
for (int i = 0;; i++) {
 String headerName = conn.getHeaderFieldKey(i);
 String headerValue = conn.getHeaderField(i);
 System.out.println(headerName + "===");
 System.out.println(headerValue);
 if (headerName == null && headerValue == null) {
  break;
 }
}

代码示例来源:origin: stackoverflow.com

HttpURLConnection conn = (HttpURLConnection) feedUrl.openConnection();
int responseCode = conn.getResponseCode();
if( responseCode == 307 ){
  String location = conn.getHeaderField("location");
  feedUrl = new URL(location);
  conn = (HttpURLConnection) this.feedUrl.openConnection();
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

private static String handleRedirect(URLConnection con, String url) throws IOException {
  if (!(con instanceof HttpURLConnection)) return url;
  if (!isRedirect(((HttpURLConnection)con).getResponseCode())) return url;
  return con.getHeaderField("Location");
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * If the server advertises CLI endpoint, returns its location.
 * @deprecated Specific to {@link Mode#REMOTING}.
 */
@Deprecated
protected CliPort getCliTcpPort(URL url) throws IOException {
  if (url.getHost()==null || url.getHost().length()==0) {
    throw new IOException("Invalid URL: "+url);
  }
  URLConnection head = url.openConnection();
  try {
    head.connect();
  } catch (IOException e) {
    throw (IOException)new IOException("Failed to connect to "+url).initCause(e);
  }
  String h = head.getHeaderField("X-Jenkins-CLI-Host");
  if (h==null)    h = head.getURL().getHost();
  String p1 = head.getHeaderField("X-Jenkins-CLI-Port");
  if (p1==null)    p1 = head.getHeaderField("X-Hudson-CLI-Port");   // backward compatibility
  String p2 = head.getHeaderField("X-Jenkins-CLI2-Port");
  String identity = head.getHeaderField("X-Instance-Identity");
  flushURLConnection(head);
  if (p1==null && p2==null) {
    verifyJenkinsConnection(head);
    throw new IOException("No X-Jenkins-CLI2-Port among " + head.getHeaderFields().keySet());
  }
  if (p2!=null)   return new CliPort(new InetSocketAddress(h,Integer.parseInt(p2)),identity,2);
  else            return new CliPort(new InetSocketAddress(h,Integer.parseInt(p1)),identity,1);
}

代码示例来源:origin: stackoverflow.com

public void checkURL(URL url) throws IOException {
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("GET");
  System.out.println(String.format("Fetching %s ...", url));
  try {
    int responseCode = conn.getResponseCode();
    if (responseCode == 200) {
      System.out.println(String.format("Site is up, content length = %s", conn.getHeaderField("content-length")));
    } else {
      System.out.println(String.format("Site is up, but returns non-ok status = %d", responseCode));
    }
  } catch (java.net.UnknownHostException e) {
    System.out.println("Site is down");
  }
}

代码示例来源:origin: stackoverflow.com

URL resourceUrl, base, next;
HttpURLConnection conn;
String location;
...
while (true)
{
  resourceUrl = new URL(url);
  conn        = (HttpURLConnection) resourceUrl.openConnection();
  conn.setConnectTimeout(15000);
  conn.setReadTimeout(15000);
  conn.setInstanceFollowRedirects(false);   // Make the logic below easier to detect redirections
  conn.setRequestProperty("User-Agent", "Mozilla/5.0...");
  switch (conn.getResponseCode())
  {
   case HttpURLConnection.HTTP_MOVED_PERM:
   case HttpURLConnection.HTTP_MOVED_TEMP:
     location = conn.getHeaderField("Location");
     base     = new URL(url);               
     next     = new URL(base, location);  // Deal with relative URLs
     url      = next.toExternalForm();
     continue;
  }
  break;
}
is = conn.openStream();
...

代码示例来源:origin: stackoverflow.com

URLConnection connection = new URL("http://foo.bar/w23afv").openConnection();
String contentType = connection.getHeaderField("Content-Type");
boolean image = contentType.startsWith("image/");

代码示例来源:origin: advantageous/qbit

private static String extractResponseString(URLConnection connection) throws IOException {
  /* Handle input. */
  HttpURLConnection http = (HttpURLConnection) connection;
  int status = http.getResponseCode();
  String charset = getCharset(connection.getHeaderField("Content-Type"));
  if (status == 200) {
    return readResponseBody(http, charset);
  } else {
    return readErrorResponseBody(http, status, charset);
  }
}

代码示例来源:origin: org.unidal.framework/test-framework

public void display(URL url) {
 try {
   URLConnection urlc = url.openConnection();
   String contentType = urlc.getHeaderField("Content-Type");
   byte[] ba = Files.forIO().readFrom(urlc.getInputStream());
   display(new String(ba, determinCharset(contentType, "utf-8")));
 } catch (Exception e) {
   throw new RuntimeException("Error when accessing URL: " + url, e);
 }
}

代码示例来源:origin: stackoverflow.com

HttpURLConnection con = (HttpURLConnection) (new URL(url).openConnection());
con.setInstanceFollowRedirects(false);
con.connect();
int responseCode = con.getResponseCode();
System.out.println(responseCode);
String location = con.getHeaderField("Location");
System.out.println(location);

代码示例来源:origin: jenkinsci/jenkins

@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE", justification = "Due to whatever reason FindBugs reports it fot try-with-resources")
static int sshConnection(String jenkinsUrl, String user, List<String> args, PrivateKeyProvider provider, final boolean strictHostKey) throws IOException {
  Logger.getLogger(SecurityUtils.class.getName()).setLevel(Level.WARNING); // suppress: BouncyCastle not registered, using the default JCE provider
  URL url = new URL(jenkinsUrl + "login");
  URLConnection conn = url.openConnection();
  CLI.verifyJenkinsConnection(conn);
  String endpointDescription = conn.getHeaderField("X-SSH-Endpoint");

代码示例来源:origin: advantageous/qbit

private static Response extractResponseObject(URLConnection connection) throws IOException {
  /* Handle input. */
  HttpURLConnection http = (HttpURLConnection) connection;
  int status = http.getResponseCode();
  String charset = getCharset(connection.getHeaderField("Content-Type"));
  String body;
  if (status == 200) {
    body = readResponseBody(http, charset);
  } else {
    body = readErrorResponseBodyDoNotDie(http, status, charset);
  }
  return Response.response(status, http.getHeaderFields(), http.getResponseMessage(), body);
}

代码示例来源:origin: stackoverflow.com

URL url = new URL("https://android.clients.google.com/c2dm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
out.close();
int responseCode = conn.getResponseCode();
String updatedAuthToken = conn.getHeaderField(UPDATE_CLIENT_AUTH);
if (updatedAuthToken != null && !auth_key.equals(updatedAuthToken)) {
  Log.i("C2DM",

代码示例来源:origin: stackoverflow.com

final URL uri=new URL(...);
URLConnection ucon;
try
 {
 ucon=uri.openConnection();
 ucon.connect();
 final String contentLengthStr=ucon.getHeaderField("content-length");
 //...
 }
catch(final IOException e1)
 {
 }

代码示例来源:origin: advantageous/qbit

private static byte[] extractResponseBytes(URLConnection connection) throws IOException {
  /* Handle input. */
  HttpURLConnection http = (HttpURLConnection) connection;
  int status = http.getResponseCode();
  //System.out.println("CONTENT-TYPE" + connection.getHeaderField("Content-TypeT"));
  if (status == 200) {
    return readResponseBodyAsBytes(http);
  } else {
    String charset = getCharset(connection.getHeaderField("Content-Type"));
    readErrorResponseBody(http, status, charset);
    return null;
  }
}

代码示例来源:origin: stackoverflow.com

URL url = new URL(AUTH_URL);
  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  urlConnection.setRequestMethod("POST");
  urlConnection.setDoOutput(true);
  outStreamWriter.close();
  int responseCode = urlConnection.getResponseCode();
FileInputStream fileStream = new FileInputStream(file);
int responseCode = urlConnection.getResponseCode();
    Log.d(TAG, String.format("Headers keys %s.", keys));
    for (String key : keySet) {
      Log.d(TAG, String.format("Header key %s value %s.", key, urlConnection.getHeaderField(key)));          
    URL url = new URL(uploadUrl);
    outStreamWriter.close();
    int responseCode = connection.getResponseCode();
    return connection.getHeaderField("Location");

代码示例来源:origin: stackoverflow.com

try {
  fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
  dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
  os = new FileOutputStream(fl);
  is = dl.openStream();
  dl.openConnection().getHeaderField("Content-Length");

相关文章

URLConnection类方法