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

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

本文整理了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

  1. @Override
  2. public String getRequestProperty(String key) {
  3. return connection.getRequestProperty(key);
  4. }

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

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

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

  1. /**
  2. * This procedure sets the URLConnection request properties
  3. * from the PROTOCOL_HEADERS in the message.
  4. */
  5. private void transferProtocolHeadersToURLConnection(URLConnection connection) {
  6. boolean addHeaders = MessageUtils.getContextualBoolean(message, ADD_HEADERS_PROPERTY, false);
  7. for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
  8. String header = entry.getKey();
  9. if (HttpHeaderHelper.CONTENT_TYPE.equalsIgnoreCase(header)) {
  10. continue;
  11. }
  12. List<String> headerList = entry.getValue();
  13. if (addHeaders || HttpHeaderHelper.COOKIE.equalsIgnoreCase(header)) {
  14. headerList.forEach(s -> connection.addRequestProperty(header, s));
  15. } else {
  16. connection.setRequestProperty(header, String.join(",", headerList));
  17. }
  18. }
  19. // make sure we don't add more than one User-Agent header
  20. if (connection.getRequestProperty("User-Agent") == null) {
  21. connection.addRequestProperty("User-Agent", USER_AGENT);
  22. }
  23. }

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

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

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

  1. public String getRequestProperty(String key) {
  2. if (cn == null) {
  3. return null;
  4. }
  5. return cn.getRequestProperty(key);
  6. }

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

  1. public class MyClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
  2. private Cookie cookie;
  3. //setters and getters.
  4. @Override
  5. protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
  6. this.setCookie(connection.getRequestProperty("Cookie"));
  7. }
  8. }

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

  1. @Override
  2. public String getRequestProperty(String key)
  3. {
  4. return _conn.getRequestProperty(key);
  5. }

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

  1. public String getRequestProperty(String key)
  2. {
  3. return _conn.getRequestProperty(key);
  4. }

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

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

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

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

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

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

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

  1. public String getRequestProperty(String key) {
  2. super.getRequestProperty(key);
  3. List<String> list = requestHeaders.get(key);
  4. if (list != null && !list.isEmpty()) {
  5. if (list.size() == 1) {
  6. return list.get(0);
  7. } else {
  8. StringBuffer buf = new StringBuffer(list.get(0));
  9. for (int x = 1; x < list.size(); x++) {
  10. buf.append(";");
  11. buf.append(list.get(x));
  12. }
  13. return buf.toString();
  14. }
  15. }
  16. return null;
  17. }
  18. public Map<String, List<String>> getRequestProperties() {

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

  1. URL url = new URL("http://youhost:8080/your-kerberised-resource");
  2. AuthenticatedURL.Token token = new AuthenticatedURL.Token();
  3. HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token);
  4. String authorizationTokenString = conn.getRequestProperty("Authorization");
  5. String delegationToken = conn.getRequestProperty("X-Hadoop-Delegation-Token");
  6. ...
  7. // do what you have to to get your basic client connection
  8. ...
  9. myBasicClientConnection.setRequestProperty("Authorization", authorizationTokenString);
  10. myBasicClientConnection.setRequestProperty("Cookie", "hadoop.auth=" + token.toString());
  11. myBasicClientConnection.setRequestProperty("X-Hadoop-Delegation-Token", delegationToken);

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

  1. /**
  2. * This procedure sets the URLConnection request properties
  3. * from the PROTOCOL_HEADERS in the message.
  4. */
  5. private void transferProtocolHeadersToURLConnection(URLConnection connection) {
  6. boolean addHeaders = MessageUtils.getContextualBoolean(message, ADD_HEADERS_PROPERTY, false);
  7. for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
  8. String header = entry.getKey();
  9. if (HttpHeaderHelper.CONTENT_TYPE.equalsIgnoreCase(header)) {
  10. continue;
  11. }
  12. List<String> headerList = entry.getValue();
  13. if (addHeaders || HttpHeaderHelper.COOKIE.equalsIgnoreCase(header)) {
  14. headerList.forEach(s -> connection.addRequestProperty(header, s));
  15. } else {
  16. connection.setRequestProperty(header, String.join(",", headerList));
  17. }
  18. }
  19. // make sure we don't add more than one User-Agent header
  20. if (connection.getRequestProperty("User-Agent") == null) {
  21. connection.addRequestProperty("User-Agent", USER_AGENT);
  22. }
  23. }

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

  1. @Override
  2. public void handle(URLConnection connection) throws Exception {
  3. // TODO switch to https since this signing is only secure with https
  4. // since we only sign the date.
  5. if (!(connection instanceof HttpURLConnection) || !matches(connection))
  6. return;
  7. if (!(connection instanceof HttpsURLConnection))
  8. logger.debug("bnd authentication should only be used with https: {}", connection.getURL());
  9. init();
  10. // Build up Authorization header
  11. StringBuilder sb = new StringBuilder(identity);
  12. // Get the date header, set it if not set
  13. String dateHeader = connection.getRequestProperty("Date");
  14. if (dateHeader == null) {
  15. synchronized (httpFormat) {
  16. dateHeader = httpFormat.format(new Date());
  17. }
  18. connection.setRequestProperty("Date", dateHeader);
  19. }
  20. // Ok, calculate the signature
  21. Signature hmac = Signature.getInstance("SHA1withRSA");
  22. hmac.initSign(privateKey);
  23. hmac.update(dateHeader.getBytes()); // never non-ascii
  24. // Finish the header
  25. sb.append(Base64.encodeBase64(hmac.sign()));
  26. connection.setRequestProperty(X_A_QUTE_AUTHORIZATION, sb.toString());
  27. }

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

  1. /**
  2. * send the headers for the given connection based on the given Dictionary of headers
  3. * @param connection
  4. * @param headers
  5. */
  6. private void sendHeaders( URLConnection connection, Dictionary headers ) {
  7. boolean sendReferer = getClientProperties().isSendReferer();
  8. for (Enumeration e = headers.keys(); e.hasMoreElements();) {
  9. String key = (String) e.nextElement();
  10. if ( sendReferer || !"referer".equalsIgnoreCase( key ) ) {
  11. connection.setRequestProperty( key, (String) headers.get( key ) );
  12. if (HttpUnitOptions.isLoggingHttpHeaders()) {
  13. if (key.equalsIgnoreCase( "authorization" ) || key.equalsIgnoreCase( "proxy-authorization") ) {
  14. System.out.println( "Sending:: " + key + ": " + headers.get( key ) );
  15. } else {
  16. System.out.println( "Sending:: " + key + ": " + connection.getRequestProperty( key ) );
  17. }
  18. }
  19. } else if (HttpUnitOptions.isLoggingHttpHeaders()) {
  20. System.out.println( "Blocked sending referer:: "+ connection.getRequestProperty( key ) );
  21. }
  22. } // for
  23. }
  24. }

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

  1. /**
  2. * send the headers for the given connection based on the given Dictionary of headers
  3. * @param connection
  4. * @param headers
  5. */
  6. private void sendHeaders( URLConnection connection, Dictionary headers ) {
  7. boolean sendReferer = getClientProperties().isSendReferer();
  8. for (Enumeration e = headers.keys(); e.hasMoreElements();) {
  9. String key = (String) e.nextElement();
  10. if ( sendReferer || !"referer".equalsIgnoreCase( key ) ) {
  11. connection.setRequestProperty( key, (String) headers.get( key ) );
  12. if (HttpUnitOptions.isLoggingHttpHeaders()) {
  13. if (key.equalsIgnoreCase( "authorization" ) || key.equalsIgnoreCase( "proxy-authorization") ) {
  14. System.out.println( "Sending:: " + key + ": " + headers.get( key ) );
  15. } else {
  16. System.out.println( "Sending:: " + key + ": " + connection.getRequestProperty( key ) );
  17. }
  18. }
  19. } else if (HttpUnitOptions.isLoggingHttpHeaders()) {
  20. System.out.println( "Blocked sending referer:: "+ connection.getRequestProperty( key ) );
  21. }
  22. } // for
  23. }
  24. }

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

  1. /**
  2. * send the headers for the given connection based on the given Dictionary of headers
  3. * @param connection
  4. * @param headers
  5. */
  6. private void sendHeaders( URLConnection connection, Dictionary headers ) {
  7. boolean sendReferer = getClientProperties().isSendReferer();
  8. for (Enumeration e = headers.keys(); e.hasMoreElements();) {
  9. String key = (String) e.nextElement();
  10. if ( sendReferer || !"referer".equalsIgnoreCase( key ) ) {
  11. connection.setRequestProperty( key, (String) headers.get( key ) );
  12. if (HttpUnitOptions.isLoggingHttpHeaders()) {
  13. if (key.equalsIgnoreCase( "authorization" ) || key.equalsIgnoreCase( "proxy-authorization") ) {
  14. System.out.println( "Sending:: " + key + ": " + headers.get( key ) );
  15. } else {
  16. System.out.println( "Sending:: " + key + ": " + connection.getRequestProperty( key ) );
  17. }
  18. }
  19. } else if (HttpUnitOptions.isLoggingHttpHeaders()) {
  20. System.out.println( "Blocked sending referer:: "+ connection.getRequestProperty( key ) );
  21. }
  22. } // for
  23. }
  24. }

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

  1. /**
  2. * Подготовка обязательных заголовков
  3. * @param connection Открытое соединение
  4. * @see #prepareUserAgent(java.net.URLConnection)
  5. */
  6. protected void prepareRequiredHeaders(URLConnection connection){
  7. logFine("prepareRequiredHeaders");
  8. if( connection!=null ){
  9. if( httpHeaders==null ){
  10. // создать headers из connection request header
  11. httpHeaders = new HttpHeaders();
  12. }
  13. // копирование существующей cookie
  14. String cookie = connection.getRequestProperty("Cookie");
  15. if( cookie!=null ){
  16. String existsCookie = httpHeaders.getCookie();
  17. if( existsCookie==null )httpHeaders.setCookie(cookie);
  18. }
  19. }
  20. prepareUserAgent(connection);
  21. }

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

  1. /**
  2. * Подготовка обязательных заголовков
  3. * @param connection Открытое соединение
  4. * @see #prepareUserAgent(java.net.URLConnection)
  5. */
  6. protected void prepareRequiredHeaders(URLConnection connection){
  7. logFine("prepareRequiredHeaders");
  8. if( connection!=null ){
  9. if( httpHeaders==null ){
  10. // создать headers из connection request header
  11. httpHeaders = new HttpHeaders();
  12. }
  13. // копирование существующей cookie
  14. String cookie = connection.getRequestProperty("Cookie");
  15. if( cookie!=null ){
  16. String existsCookie = httpHeaders.getCookie();
  17. if( existsCookie==null )httpHeaders.setCookie(cookie);
  18. }
  19. }
  20. prepareUserAgent(connection);
  21. }

相关文章

URLConnection类方法