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

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

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

URLConnection.getHeaderFieldKey介绍

[英]Returns the name of the header field at the given position posn or null if there are fewer than posn 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.
[中]返回给定位置posn处的标头字段的名称,如果少于posn字段,则返回null。此方法的基本实现总是返回null。
一些实现(尤其是HttpURLConnection)包括空键的映射;在HTTP的情况下,这映射到HTTP状态行,并在索引到头字段时被视为处于位置0。

代码示例

代码示例来源:origin: org.eclipse.jetty.osgi/jetty-osgi-boot-warurl

@Override
public String getHeaderFieldKey(int n)
{
  return _conn.getHeaderFieldKey(n);
}

代码示例来源:origin: org.vx68k.quercus/quercus

public String getHeaderFieldKey(int i)
{
 return _conn.getHeaderFieldKey(i);
}

代码示例来源:origin: org.jboss/jboss-common-core

public String getHeaderFieldKey(int n) {
 return delegateConnection.getHeaderFieldKey(n);
}

代码示例来源:origin: freeplane/freeplane

public String getHeaderFieldKey(int n) {
  return connection.getHeaderFieldKey(n);
}

代码示例来源:origin: org.microemu/microemu-javase

private int getImplIndex(int index){
  if (cn.getHeaderFieldKey(0) == null && cn.getHeaderField(0) != null){
    index++;
  }
  return index;
}

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

public String getCookies(URLConnection connection) {
  String headerName = null;
  String cookie = "";
  for (int i=1; (headerName = connection.getHeaderFieldKey(i))!=null; i++) {
    if (headerName.equals("Set-Cookie")) {
      if (cookie.equals("")) {
        cookie = connection.getHeaderField(i);
      }
      else {
        cookie = cookie + "; " + connection.getHeaderField(i);
      }
    }
  }
  writeToCookiesFile(cookie);
  return cookie;
}

代码示例来源:origin: org.ofbiz/ofbcore-jira-share

/** Returns the key for the nth response header field. */
public String getResponseHeaderFieldKey(int n) throws HttpClientException {
  if (con == null)
    throw new HttpClientException("Connection not yet established");
  return con.getHeaderFieldKey(n);
}

代码示例来源:origin: apache/ofbiz-framework

/** Returns the key for the nth response header field. */
public String getResponseHeaderFieldKey(int n) throws HttpClientException {
  if (con == null) {
    throw new HttpClientException("Connection not yet established");
  }
  return con.getHeaderFieldKey(n);
}

代码示例来源:origin: lincanbin/Android-Carbon-Forum

public static Boolean saveCookie(Context context, URLConnection connection){
  //获取Cookie
  String headerName=null;
  for (int i=1; (headerName = connection.getHeaderFieldKey(i))!=null; i++) {
    if (headerName.equals("Set-Cookie")) {
      String cookie = connection.getHeaderField(i);
      //将Cookie保存起来
      SharedPreferences mySharedPreferences = context.getSharedPreferences("Session",
          Activity.MODE_PRIVATE);
      SharedPreferences.Editor editor = mySharedPreferences.edit();
      editor.putString("Cookie", cookie);
      editor.apply();
      return true;
    }
  }
  return false;
}

代码示例来源:origin: org.microemu/microemu-javase

public String getHeaderFieldKey(int n) throws IOException {
  if (cn == null) {
    throw new IOException();
  }
  if (!connected) {
    cn.connect();
    connected = true;
  }
  return cn.getHeaderFieldKey(getImplIndex(n));
}

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

public class URLConnectionReader {

 public static void main(String[] args) throws Exception {
 URL oracle = new URL("http://www.google.com/");
 URLConnection yc = oracle.openConnection();

 String headerName = null;
 for (int i = 1; (headerName = yc.getHeaderFieldKey(i)) != null; i++) {
  if (headerName.equals("Set-Cookie")) {
   String cookie = yc.getHeaderField(i);
   System.out.println("Cookie  ::  " + cookie);
  } else {
   System.out.println("Header: "+ yc.getHeaderField(i));
  }
 }

 BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
 String inputLine;
 while ((inputLine = in.readLine()) != null)
  System.out.println(inputLine);
 in.close();
}

代码示例来源:origin: org.encog/encog-core

/**
 * Load any cookies from the specified URLConnection object. Cookies will be
 * located by their Set-Cookie headers. Any cookies that are found can be
 * moved to a new URLConnection class by calling saveCookies.
 * 
 * @param http
 *            The URLConnection object to load the cookies from.
 */
public void loadCookies(final URLConnection http) {
  String str;
  int n = 1;
  do {
    str = http.getHeaderFieldKey(n);
    if ((str != null) && str.equalsIgnoreCase("Set-Cookie")) {
      str = http.getHeaderField(n);
      final StringTokenizer tok = new StringTokenizer(str, "=");
      final String name = tok.nextToken();
      final String value = tok.nextToken();
      this.map.put(name, value);
    }
    n++;
  } while (str != null);
}

代码示例来源:origin: com.bitplan/mediawiki-japi

/**
 *  Grabs cookies from the URL connection provided.
 *  @param u an unconnected URLConnection
 *  @param map the cookie store
 */
private void grabCookies(URLConnection u)
{
  String headerName;
  for (int i = 1; (headerName = u.getHeaderFieldKey(i)) != null; i++)
    if (headerName.equals("Set-Cookie"))
    {
      String cookie = u.getHeaderField(i);
      cookie = cookie.substring(0, cookie.indexOf(';'));
      String name = cookie.substring(0, cookie.indexOf('='));
      String value = cookie.substring(cookie.indexOf('=') + 1, cookie.length());
      // these cookies were pruned, but are still sent for some reason?
      // TODO: when these cookies are no longer sent, remove this test
      if (!value.equals("deleted"))
        cookies.put(name, value);
    }
}

代码示例来源:origin: org.opengis.cite.teamengine/teamengine-core

private static void append_headers(URLConnection uc, Element e) {
  Document doc = e.getOwnerDocument();
  Element headers = doc.createElement("headers");
  e.appendChild(headers);
  for (int i = 0;; i++) {
    String headerKey = uc.getHeaderFieldKey(i);
    String headerValue = uc.getHeaderField(i);
    if (headerKey == null) {
      if (headerValue == null)
        break;
    } else {
      Element header = doc.createElement("header");
      headers.appendChild(header);
      header.setAttribute("name", headerKey);
      header.appendChild(doc.createTextNode(headerValue));
    }
  }
  if (LOGR.isLoggable(Level.FINER)) {
    LOGR.finer(DomUtils.serializeNode(e));
  }
}

代码示例来源:origin: opengeospatial/teamengine

private static void append_headers(URLConnection uc, Element e) {
  Document doc = e.getOwnerDocument();
  Element headers = doc.createElement("headers");
  e.appendChild(headers);
  for (int i = 0;; i++) {
    String headerKey = uc.getHeaderFieldKey(i);
    String headerValue = uc.getHeaderField(i);
    if (headerKey == null) {
      if (headerValue == null)
        break;
    } else {
      Element header = doc.createElement("header");
      headers.appendChild(header);
      header.setAttribute("name", headerKey);
      header.appendChild(doc.createTextNode(headerValue));
    }
  }
  if (LOGR.isLoggable(Level.FINER)) {
    LOGR.finer(DomUtils.serializeNode(e));
  }
}

代码示例来源:origin: httpunit/httpunit

private void loadHeaders( URLConnection connection ) {
  if (HttpUnitOptions.isLoggingHttpHeaders()) {
    System.out.println( "Header:: " + connection.getHeaderField(0) );
  }
  for (int i = 1; true; i++) {
    String headerFieldKey = connection.getHeaderFieldKey( i );
    String headerField = connection.getHeaderField(i);
    if (headerFieldKey == null || headerField == null) break;
    if (HttpUnitOptions.isLoggingHttpHeaders()) {
      System.out.println( "Header:: " + headerFieldKey + ": " + headerField );
    }
    addHeader( headerFieldKey.toUpperCase(), headerField );
  }
  if (connection.getContentType() != null) {
    setContentTypeHeader( connection.getContentType() );
  }
}

代码示例来源:origin: javanettasks/httpunit

private void loadHeaders( URLConnection connection ) {
  if (HttpUnitOptions.isLoggingHttpHeaders()) {
    System.out.println( "Header:: " + connection.getHeaderField(0) );
  }
  for (int i = 1; true; i++) {
    String headerFieldKey = connection.getHeaderFieldKey( i );
    String headerField = connection.getHeaderField(i);
    if (headerFieldKey == null || headerField == null) break;
    if (HttpUnitOptions.isLoggingHttpHeaders()) {
      System.out.println( "Header:: " + headerFieldKey + ": " + headerField );
    }
    addHeader( headerFieldKey.toUpperCase(), headerField );
  }
  if (connection.getContentType() != null) {
    setContentTypeHeader( connection.getContentType() );
  }
}

代码示例来源:origin: org.kohsuke.httpunit/httpunit

private void loadHeaders( URLConnection connection ) {
  if (HttpUnitOptions.isLoggingHttpHeaders()) {
    System.out.println( "Header:: " + connection.getHeaderField(0) );
  }
  for (int i = 1; true; i++) {
    String headerFieldKey = connection.getHeaderFieldKey( i );
    String headerField = connection.getHeaderField(i);
    if (headerFieldKey == null || headerField == null) break;
    if (HttpUnitOptions.isLoggingHttpHeaders()) {
      System.out.println( "Header:: " + headerFieldKey + ": " + headerField );
    }
    addHeader( headerFieldKey.toUpperCase(), headerField );
  }
  if (connection.getContentType() != null) {
    setContentTypeHeader( connection.getContentType() );
  }
}

代码示例来源:origin: octo-online/reactive-audit

@Test(expected = NetworkReactiveAuditException.class)
public void getHeaderFieldKey()
    throws IOException
{
  Assume.assumeTrue(IOTestTools.isNetworkConnected());
  ReactiveAudit.off.commit();
  URLConnection conn = new URL("http://" + HOST + ":" + PORT).openConnection();
  TestTools.strict.commit();
  conn.getHeaderFieldKey(0);
}

相关文章

URLConnection类方法