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

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

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

URLConnection.getHeaderFields介绍

[英]Returns an unmodifiable map of the response-header fields and values. The response-header field names are the key values of the map. The map values are lists of header field values associated with a particular key name.

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.
[中]返回响应头字段和值的不可修改映射。响应头字段名是映射的键值。映射值是与特定键名关联的标题字段值列表。
一些实现(尤其是HttpURLConnection)包括空键的映射;在HTTP的情况下,这映射到HTTP状态行,并在索引到头字段时被视为处于位置0。

代码示例

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

// Gather all cookies on the first request.
URLConnection connection = new URL(url).openConnection();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
// ...

// Then use the same cookies on all subsequent requests.
connection = new URL(url).openConnection();
for (String cookie : cookies) {
  connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
// ...

代码示例来源:origin: lingochamp/FileDownloader

@Override
public Map<String, List<String>> getResponseHeaderFields() {
  return mConnection.getHeaderFields();
}

代码示例来源:origin: lingochamp/okdownload

@Override
public Map<String, List<String>> getResponseHeaderFields() {
  return connection.getHeaderFields();
}

代码示例来源:origin: nutzam/nutz

public static String dumpHeaders(URLConnection conn) {
  StringBuilder sb = new StringBuilder();
  Iterator<?> it = conn.getHeaderFields().keySet().iterator();
  sb.append('\n');
  sb.append("<HEADERS url=\"" + conn.getURL().toString() + "\">");
  while (it.hasNext()) {
    String name = (String) it.next();
    sb.append('\n');
    sb.append("[" + name + "]:");
    sb.append(conn.getHeaderField(name));
  }
  sb.append('\n');
  sb.append("</HEADERS>");
  return sb.toString();
}

代码示例来源: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

.append("\n");
Map<String, List<String>> map = httpURLConnection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet())

代码示例来源:origin: scouter-project/scouter

private static void process(String arg) throws Exception {
  long stime = System.currentTimeMillis();
  try {
    URL u = new URL(arg);
    URLConnection uc = u.openConnection();
    String uu = setUser(uc);
    uc.connect();
    InputStream o = uc.getInputStream();
    FileUtil.readAll(o);
    Map<String, List<String>> heads = uc.getHeaderFields();
    List<String> cookie = heads.get("Set-Cookie");
    keepCookie("" + cookie);
    o.close();
    long dur = System.currentTimeMillis() - stime;
    System.out.println(arg + " " + dur + " ms " + uu);
  } catch (Exception e) {
    long dur = System.currentTimeMillis() - stime;
    System.out.println(arg + " " + dur + " ms - ERROR");
  }
}

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

static final String COOKIES_HEADER = "Set-Cookie";
HttpURLConnection connection = ... ;
static java.net.CookieManager msCookieManager = new java.net.CookieManager();

Map<String, List<String>> headerFields = connection.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);

if (cookiesHeader != null) {
  for (String cookie : cookiesHeader) {
    msCookieManager.getCookieStore().add(null,HttpCookie.parse(cookie).get(0));
  }               
}

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

connection.connect();
  System.out.println("Headers of " + url + " => "
      + connection.getHeaderFields());
} catch (SSLHandshakeException e) {
  System.out.println("Untrusted: " + url);

代码示例来源:origin: lingochamp/okdownload

@Test
public void getResponseHeaderFields() throws Exception {
  when(urlConnection.getHeaderFields()).thenReturn(headerFields);
  assertThat(downloadUrlConnection.getResponseHeaderFields()).isEqualTo(headerFields);
}

代码示例来源:origin: square/okhttp

Headers responseHeaders = createHeaders(urlConnection.getHeaderFields());

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

URLConnection connection = new URL("http://google.com").openConnection();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
// ...

代码示例来源:origin: ron190/jsql-injection

/**
 * Extract HTTP headers from a connection.
 * @param connection Connection with HTTP headers
 * @return Map of HTTP headers <name, value>
 */
public static Map<String, String> getHttpHeaders(URLConnection connection) {
  Map<String, String> mapHeaders = new HashMap<>();
  
  for (Map.Entry<String, List<String>> entries : connection.getHeaderFields().entrySet()) {
    mapHeaders.put(entries.getKey() == null ? "Status code" : entries.getKey(), String.join(",", entries.getValue()));
  }
  return mapHeaders;
}

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

URLConnection connection = new URL("http://cdn3.sstatic.net/stackoverflow/img/favicon.ico").openConnection();
connection.setRequestProperty("If-Modified-Since", "Wed, 06 Oct 2010 02:53:46 GMT");
System.out.println(connection.getHeaderFields());

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

URLConnection connection = new URL("http://cdn3.sstatic.net/stackoverflow/img/favicon.ico").openConnection();
System.out.println(connection.getHeaderFields());

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

URLConnection connection = new URL("http://cdn3.sstatic.net/stackoverflow/img/favicon.ico").openConnection();
connection.setRequestProperty("If-None-Match", "9d9bd8b1165cb1:0");
System.out.println(connection.getHeaderFields());

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

con.connect();
int metaDataOffset = 0;
Map<String, List<String>> headers = con.getHeaderFields();
InputStream stream = con.getInputStream();

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

Set<String> keySet = urlConnection.getHeaderFields().keySet();
String keys = urlConnection.getHeaderFields().keySet().toString();
Log.d(TAG, String.format("Headers keys %s.", keys));
for (String key : keySet) {

代码示例来源:origin: com.braintreepayments/braintreehttp

Headers parseResponseHeaders(URLConnection connection) {
  Headers headers = new Headers();
  for (String key : connection.getHeaderFields().keySet()) {
    headers.header(key, connection.getHeaderField(key));
  }
  return headers;
}

代码示例来源:origin: igvteam/igv

static boolean isExpectedRangeMissing(URLConnection conn, Map<String, String> requestProperties) {
  final boolean rangeRequested = (requestProperties != null) && (new CI.CIHashMap<String>(requestProperties)).containsKey("Range");
  if (!rangeRequested) return false;
  Map<String, List<String>> headerFields = conn.getHeaderFields();
  boolean rangeReceived = (headerFields != null) && (new CI.CIHashMap<List<String>>(headerFields)).containsKey("Content-Range");
  return !rangeReceived;
}

相关文章

URLConnection类方法