Java Code Examples for java.net.HttpURLConnection#getRequestProperty()

The following examples show how to use java.net.HttpURLConnection#getRequestProperty() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: GPT   File: HttpURLRequest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 添加Post请求内容
 *
 * @param connection connection
 * @param params     params
 * @return 是否添加成功
 * @throws IOException IOException
 */
protected boolean addContentBody(HttpURLConnection connection, RequestParams params) throws IOException {
    if (params.getBody() == null) {
        return false;
    }
    connection.setFixedLengthStreamingMode(params.getBody().length);
    connection.setDoOutput(true);
    String requestProperty = connection.getRequestProperty(RequestParams.HEADER_CONTENT_TYPE);
    if (TextUtils.isEmpty(requestProperty)) {
        connection.addRequestProperty(RequestParams.HEADER_CONTENT_TYPE, params.getContentType());
    }
    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
    out.write(params.getBody());
    out.close();

    return true;
}
 
Example 2
/**
 * Add the appropriate Authorization header to the HttpURLConnection.
 *
 * @param connection The HttpURLConnection to which the header will be added.
 * @param method The HTTP method to use (GET, PUT, DELETE)
 * @param bucket the bucket name this request is for
 * @param key the key this request is for
 * @param pathArgs path arguments which are part of this request
 */
private void addAuthHeader(HttpURLConnection connection, String method, String bucket, String key, Map pathArgs) {
    if (connection.getRequestProperty("Date") == null) {
        connection.setRequestProperty("Date", httpDate());
    }
    if (connection.getRequestProperty("Content-Type") == null) {
        connection.setRequestProperty("Content-Type", "");
    }

    if (this.awsAccessKeyId != null && this.awsSecretAccessKey != null) {
        String canonicalString
                = Utils.makeCanonicalString(method, bucket, key, pathArgs, connection.getRequestProperties());
        String encodedCanonical = Utils.encode(this.awsSecretAccessKey, canonicalString, false);
        connection.setRequestProperty("Authorization",
                "AWS " + this.awsAccessKeyId + ":" + encodedCanonical);
    }
}
 
Example 3
/**
 * Get the content length of the request in the given HTTP connection.
 * @param connection The connection.
 * @return The content length, or zero if not found.
 */
private long getRequestContentLength(HttpURLConnection connection) {
  String lengthString = connection.getRequestProperty(
      HeaderConstants.CONTENT_LENGTH);
  if (lengthString != null){
    return Long.parseLong(lengthString);
  }
  else{
    return 0;
  }
}
 
Example 4
/**
 * Get the content length of the request in the given HTTP connection.
 * @param connection The connection.
 * @return The content length, or zero if not found.
 */
private long getRequestContentLength(HttpURLConnection connection) {
  String lengthString = connection.getRequestProperty(
      HeaderConstants.CONTENT_LENGTH);
  if (lengthString != null){
    return Long.parseLong(lengthString);
  }
  else{
    return 0;
  }
}
 
Example 5
Source Project: FxDock   File: NetTools.java    License: Apache License 2.0 5 votes vote down vote up
private static InputStream getInputStream(HttpURLConnection c) throws Exception
{
	//Object x = c.getContent();
	
	InputStream in = c.getInputStream();
	String s = c.getRequestProperty("Content-Encoding");
	if(s != null)
	{
		if(s.contains("zip"))
		{
			return new GZIPInputStream(in);
		}
	}
	return in;
}
 
Example 6
@Nullable
@Override
public String getFirstHeader(String headerName, HttpURLConnection urlConnection) {
    return urlConnection.getRequestProperty(headerName);
}
 
Example 7
Source Project: Aria   File: ConnectionHelp.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 设置头部参数
 *
 * @throws ProtocolException
 */
public static HttpURLConnection setConnectParam(HttpTaskOption delegate, HttpURLConnection conn) {
  if (delegate.getRequestEnum() == RequestEnum.POST) {
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
  }
  Set<String> keys = null;
  if (delegate.getHeaders() != null && delegate.getHeaders().size() > 0) {
    keys = delegate.getHeaders().keySet();
    for (String key : keys) {
      conn.setRequestProperty(key, delegate.getHeaders().get(key));
    }
  }
  if (conn.getRequestProperty("Accept-Language") == null) {
    conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7");
  }
  if (conn.getRequestProperty("Accept-Encoding") == null) {
    conn.setRequestProperty("Accept-Encoding", "identity");
  }
  if (conn.getRequestProperty("Accept-Charset") == null) {
    conn.setRequestProperty("Accept-Charset", "UTF-8");
  }
  if (conn.getRequestProperty("Connection") == null) {
    conn.setRequestProperty("Connection", "Keep-Alive");
  }
  if (conn.getRequestProperty("Charset") == null) {
    conn.setRequestProperty("Charset", "UTF-8");
  }
  if (conn.getRequestProperty("User-Agent") == null) {
    conn.setRequestProperty("User-Agent",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
  }
  if (conn.getRequestProperty("Accept") == null) {
    StringBuilder accept = new StringBuilder();
    accept.append("image/gif, ")
        .append("image/jpeg, ")
        .append("image/pjpeg, ")
        .append("image/webp, ")
        .append("image/apng, ")
        .append("application/xml, ")
        .append("application/xaml+xml, ")
        .append("application/xhtml+xml, ")
        .append("application/x-shockwave-flash, ")
        .append("application/x-ms-xbap, ")
        .append("application/x-ms-application, ")
        .append("application/msword, ")
        .append("application/vnd.ms-excel, ")
        .append("application/vnd.ms-xpsdocument, ")
        .append("application/vnd.ms-powerpoint, ")
        .append("application/signed-exchange, ")
        .append("text/plain, ")
        .append("text/html, ")
        .append("*/*");
    conn.setRequestProperty("Accept", accept.toString());
  }
  //302获取重定向地址
  conn.setInstanceFollowRedirects(false);

  CookieManager manager = delegate.getCookieManager();
  if (manager != null) {
    CookieStore store = manager.getCookieStore();
    if (store != null && store.getCookies().size() > 0) {
      conn.setRequestProperty("Cookie",
          TextUtils.join(";", store.getCookies()));
    }
  }

  return conn;
}
 
Example 8
Source Project: j2objc   File: URLConnectionTest.java    License: Apache License 2.0 4 votes vote down vote up
private static String setAndReturnRequestHeaderValue(String value) throws Exception {
    URL url = new URL("http://www.google.com/");
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.addRequestProperty("key", value);
    return urlConnection.getRequestProperty("key");
}
 
Example 9
Source Project: azure-storage-android   File: Utility.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Returns the standard header value from the specified connection request, or an empty string if no header value
 * has been specified for the request.
 * 
 * @param conn
 *            An <code>HttpURLConnection</code> object that represents the request.
 * @param headerName
 *            A <code>String</code> that represents the name of the header being requested.
 * 
 * @return A <code>String</code> that represents the header value, or <code>null</code> if there is no corresponding
 *         header value for <code>headerName</code>.
 */
public static String getStandardHeaderValue(final HttpURLConnection conn, final String headerName) {
    final String headerValue = conn.getRequestProperty(headerName);

    // Coalesce null value
    return headerValue == null ? Constants.EMPTY_STRING : headerValue;
}