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

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

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

URLConnection.getRequestProperty介绍

[英]Returns the value of the request header property specified by {code field} or null if there is no field with this name. The base implementation of this method returns always null.
[中]返回由{code field}指定的请求头属性的值,如果没有具有此名称的字段,则返回null。此方法的基本实现总是返回null。

代码示例

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

@Override
public String getRequestProperty(String key) {
  return connection.getRequestProperty(key);
}

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

@Test
public void getRequestProperty() throws Exception {
  when(urlConnection.getRequestProperty("key1")).thenReturn("value1");
  assertThat(downloadUrlConnection.getRequestProperty("key1")).isEqualTo("value1");
}

代码示例来源:origin: org.apache.cxf/cxf-rt-transports-http

/**
 * This procedure sets the URLConnection request properties
 * from the PROTOCOL_HEADERS in the message.
 */
private void transferProtocolHeadersToURLConnection(URLConnection connection) {
  boolean addHeaders = MessageUtils.getContextualBoolean(message, ADD_HEADERS_PROPERTY, false);
  for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
    String header = entry.getKey();
    if (HttpHeaderHelper.CONTENT_TYPE.equalsIgnoreCase(header)) {
      continue;
    }
    List<String> headerList = entry.getValue();
    if (addHeaders || HttpHeaderHelper.COOKIE.equalsIgnoreCase(header)) {
      headerList.forEach(s -> connection.addRequestProperty(header, s));
    } else {
      connection.setRequestProperty(header, String.join(",", headerList));
    }
  }
  // make sure we don't add more than one User-Agent header
  if (connection.getRequestProperty("User-Agent") == null) {
    connection.addRequestProperty("User-Agent", USER_AGENT);
  }
}

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

file.length()));
Log.d(TAG, urlConnection.getRequestProperty("Content-Range"));

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

public String getRequestProperty(String key) {
  if (cn == null) {
    return null;
  }
  return cn.getRequestProperty(key);
}

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

public class MyClientHttpRequestFactory extends SimpleClientHttpRequestFactory {

private Cookie cookie;
//setters and getters.

@Override
protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
 this.setCookie(connection.getRequestProperty("Cookie"));

}
}

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

@Override
public String getRequestProperty(String key)
{
  return _conn.getRequestProperty(key);
}

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

public String getRequestProperty(String key)
{
 return _conn.getRequestProperty(key);
}

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

public String getRequestProperty(String key) {
  return connection.getRequestProperty(key);
}

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

public String getRequestProperty(String key) {
 return delegateConnection.getRequestProperty(key);
}

代码示例来源:origin: org.samba.jcifs/jcifs

public String getRequestProperty(String key) {
  return connection.getRequestProperty(key);
}

代码示例来源:origin: org.objectweb.celtix/celtix-rt

public String getRequestProperty(String key) {
  super.getRequestProperty(key);
  List<String> list = requestHeaders.get(key);
  if (list != null && !list.isEmpty()) {
    if (list.size() == 1) {
      return list.get(0);
    } else {
      StringBuffer buf = new StringBuffer(list.get(0));
      for (int x = 1; x < list.size(); x++) {
        buf.append(";");
        buf.append(list.get(x));
      }
      return buf.toString();
    }
  }
  return null;
}
public Map<String, List<String>> getRequestProperties() {

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

URL url = new URL("http://youhost:8080/your-kerberised-resource");
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token);

String authorizationTokenString = conn.getRequestProperty("Authorization");
String delegationToken = conn.getRequestProperty("X-Hadoop-Delegation-Token");
...
// do what you have to to get your basic client connection
...
myBasicClientConnection.setRequestProperty("Authorization", authorizationTokenString);
myBasicClientConnection.setRequestProperty("Cookie", "hadoop.auth=" + token.toString());
myBasicClientConnection.setRequestProperty("X-Hadoop-Delegation-Token", delegationToken);

代码示例来源:origin: apache/cxf

/**
 * This procedure sets the URLConnection request properties
 * from the PROTOCOL_HEADERS in the message.
 */
private void transferProtocolHeadersToURLConnection(URLConnection connection) {
  boolean addHeaders = MessageUtils.getContextualBoolean(message, ADD_HEADERS_PROPERTY, false);
  for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
    String header = entry.getKey();
    if (HttpHeaderHelper.CONTENT_TYPE.equalsIgnoreCase(header)) {
      continue;
    }
    List<String> headerList = entry.getValue();
    if (addHeaders || HttpHeaderHelper.COOKIE.equalsIgnoreCase(header)) {
      headerList.forEach(s -> connection.addRequestProperty(header, s));
    } else {
      connection.setRequestProperty(header, String.join(",", headerList));
    }
  }
  // make sure we don't add more than one User-Agent header
  if (connection.getRequestProperty("User-Agent") == null) {
    connection.addRequestProperty("User-Agent", USER_AGENT);
  }
}

代码示例来源:origin: biz.aQute.bnd/biz.aQute.bndlib

@Override
public void handle(URLConnection connection) throws Exception {
  // TODO switch to https since this signing is only secure with https
  // since we only sign the date.
  if (!(connection instanceof HttpURLConnection) || !matches(connection))
    return;
  if (!(connection instanceof HttpsURLConnection))
    logger.debug("bnd authentication should only be used with https: {}", connection.getURL());
  init();
  // Build up Authorization header
  StringBuilder sb = new StringBuilder(identity);
  // Get the date header, set it if not set
  String dateHeader = connection.getRequestProperty("Date");
  if (dateHeader == null) {
    synchronized (httpFormat) {
      dateHeader = httpFormat.format(new Date());
    }
    connection.setRequestProperty("Date", dateHeader);
  }
  // Ok, calculate the signature
  Signature hmac = Signature.getInstance("SHA1withRSA");
  hmac.initSign(privateKey);
  hmac.update(dateHeader.getBytes()); // never non-ascii
  // Finish the header
  sb.append(Base64.encodeBase64(hmac.sign()));
  connection.setRequestProperty(X_A_QUTE_AUTHORIZATION, sb.toString());
}

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

/**
   * send the headers for the given connection based on the given Dictionary of headers
   * @param connection
   * @param headers
   */
  private void sendHeaders( URLConnection connection, Dictionary headers ) {
      boolean sendReferer = getClientProperties().isSendReferer();
    for (Enumeration e = headers.keys(); e.hasMoreElements();) {
      String key = (String) e.nextElement();
      if ( sendReferer || !"referer".equalsIgnoreCase( key ) ) {
        connection.setRequestProperty( key, (String) headers.get( key ) );
        if (HttpUnitOptions.isLoggingHttpHeaders()) {
          if (key.equalsIgnoreCase( "authorization" ) || key.equalsIgnoreCase( "proxy-authorization") ) {
            System.out.println( "Sending:: " + key + ": " + headers.get( key ) );
          } else {
            System.out.println( "Sending:: " + key + ": " + connection.getRequestProperty( key ) );
          }
        }
        } else if (HttpUnitOptions.isLoggingHttpHeaders()) {
            System.out.println( "Blocked sending referer:: "+ connection.getRequestProperty( key ) );
        }        
    } // for
  }
}

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

/**
   * send the headers for the given connection based on the given Dictionary of headers
   * @param connection
   * @param headers
   */
  private void sendHeaders( URLConnection connection, Dictionary headers ) {
      boolean sendReferer = getClientProperties().isSendReferer();
    for (Enumeration e = headers.keys(); e.hasMoreElements();) {
      String key = (String) e.nextElement();
      if ( sendReferer || !"referer".equalsIgnoreCase( key ) ) {
        connection.setRequestProperty( key, (String) headers.get( key ) );
        if (HttpUnitOptions.isLoggingHttpHeaders()) {
          if (key.equalsIgnoreCase( "authorization" ) || key.equalsIgnoreCase( "proxy-authorization") ) {
            System.out.println( "Sending:: " + key + ": " + headers.get( key ) );
          } else {
            System.out.println( "Sending:: " + key + ": " + connection.getRequestProperty( key ) );
          }
        }
        } else if (HttpUnitOptions.isLoggingHttpHeaders()) {
            System.out.println( "Blocked sending referer:: "+ connection.getRequestProperty( key ) );
        }        
    } // for
  }
}

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

/**
   * send the headers for the given connection based on the given Dictionary of headers
   * @param connection
   * @param headers
   */
  private void sendHeaders( URLConnection connection, Dictionary headers ) {
      boolean sendReferer = getClientProperties().isSendReferer();
    for (Enumeration e = headers.keys(); e.hasMoreElements();) {
      String key = (String) e.nextElement();
      if ( sendReferer || !"referer".equalsIgnoreCase( key ) ) {
        connection.setRequestProperty( key, (String) headers.get( key ) );
        if (HttpUnitOptions.isLoggingHttpHeaders()) {
          if (key.equalsIgnoreCase( "authorization" ) || key.equalsIgnoreCase( "proxy-authorization") ) {
            System.out.println( "Sending:: " + key + ": " + headers.get( key ) );
          } else {
            System.out.println( "Sending:: " + key + ": " + connection.getRequestProperty( key ) );
          }
        }
        } else if (HttpUnitOptions.isLoggingHttpHeaders()) {
            System.out.println( "Blocked sending referer:: "+ connection.getRequestProperty( key ) );
        }        
    } // for
  }
}

代码示例来源:origin: xyz.cofe/common

/**
 * Подготовка обязательных заголовков
 * @param connection Открытое соединение
 * @see #prepareUserAgent(java.net.URLConnection) 
 */
protected void prepareRequiredHeaders(URLConnection connection){
  logFine("prepareRequiredHeaders");
  if( connection!=null ){
    if( httpHeaders==null ){
      // создать headers из connection request header
      httpHeaders = new HttpHeaders();
    }
    // копирование существующей cookie
    String cookie = connection.getRequestProperty("Cookie");
    if( cookie!=null ){
      String existsCookie = httpHeaders.getCookie();
      if( existsCookie==null )httpHeaders.setCookie(cookie);
    }
  }
  prepareUserAgent(connection);
}

代码示例来源:origin: xyz.cofe/http-base

/**
 * Подготовка обязательных заголовков
 * @param connection Открытое соединение
 * @see #prepareUserAgent(java.net.URLConnection) 
 */
protected void prepareRequiredHeaders(URLConnection connection){
  logFine("prepareRequiredHeaders");
  if( connection!=null ){
    if( httpHeaders==null ){
      // создать headers из connection request header
      httpHeaders = new HttpHeaders();
    }
    // копирование существующей cookie
    String cookie = connection.getRequestProperty("Cookie");
    if( cookie!=null ){
      String existsCookie = httpHeaders.getCookie();
      if( existsCookie==null )httpHeaders.setCookie(cookie);
    }
  }
  prepareUserAgent(connection);
}

相关文章

URLConnection类方法