public string GenerateSign(string method, string key, Dictionary <string, string> queryParameters, Dictionary <string, string> headers, long signDurationSecond) { try { string signTime = null; if (signDurationSecond > 0) { long currentTimeSecond = TimeUtils.GetCurrentTime(TimeUnit.SECONDS); signTime = String.Format("{0};{1}", currentTimeSecond, currentTimeSecond + signDurationSecond); } Dictionary <string, string> encodeQuery = null; if (queryParameters != null) { encodeQuery = new Dictionary <string, string>(queryParameters.Count); foreach (KeyValuePair <string, string> keyValuePair in queryParameters) { encodeQuery[keyValuePair.Key] = URLEncodeUtils.Encode(keyValuePair.Value); } } return(CosXmlSigner.GenerateSign(method, key, encodeQuery, headers, signTime, qcloudCredentailProvider.GetQCloudCredentials())); } catch (CosClientException) { throw; } catch (Exception ex) { throw new CosClientException((int)CosClientError.INVALID_ARGUMENT, ex.Message, ex); } }
/// <summary> /// get source with urlEncode /// </summary> /// <returns></returns> public string GetCopySouce() { if (!key.StartsWith("/")) { key = "/" + key; } StringBuilder copySource = new StringBuilder(); copySource.Append(bucket); if (!bucket.EndsWith("-" + appid)) { copySource.Append("-") .Append(appid); } copySource.Append(".").Append("cos").Append(".") .Append(region).Append(".") .Append("myqcloud.com") .Append(URLEncodeUtils.EncodePathOfURL(key)); if (versionId != null) { copySource.Append("?versionId=").Append(versionId); } return(copySource.ToString()); }
public string Source(Request request) { Dictionary <string, string> sourceHeaders = request.Headers; Dictionary <string, string> lowerKeySourceHeaders = new Dictionary <string, string>(sourceHeaders.Count); foreach (KeyValuePair <string, string> pair in sourceHeaders) { lowerKeySourceHeaders.Add(pair.Key.ToLower(), pair.Value); } try { lowerKeySourceHeaders.Add("host", request.Host); } catch (Exception) { } try { lowerKeySourceHeaders.Add("user-agent", request.UserAgent); } catch (Exception) { } Dictionary <string, string> sourceParameters = request.Url.GetQueryParameters(); Dictionary <string, string> lowerKeySourceParameters = new Dictionary <string, string>(sourceParameters.Count); foreach (KeyValuePair <string, string> pair in sourceParameters) { lowerKeySourceParameters.Add(pair.Key.ToLower(), pair.Value); } string path = URLEncodeUtils.Decode(request.Url.Path); return(GenerateSource(request.Method, path, lowerKeySourceParameters, lowerKeySourceHeaders)); }
private HttpUrl CreateUrl(CosRequest cosRequest) { HttpUrl httpUrl = new HttpUrl(); httpUrl.Scheme = (bool)cosRequest.IsHttps ? "https" : "http"; httpUrl.Host = cosRequest.GetHost(); httpUrl.Path = URLEncodeUtils.EncodePathOfURL(cosRequest.RequestPath); httpUrl.SetQueryParameters(cosRequest.GetRequestParamters()); return(httpUrl); }
public SensitiveContentRecognitionRequest(string bucket, string key, string type) : base(bucket, key) { if (type == null) { throw new CosClientException((int)CosClientError.InvalidArgument, "type = null"); } this.method = CosRequestMethod.GET; this.queryParameters.Add("ci-process", "sensitive-content-recognition"); this.queryParameters.Add("detect-type", URLEncodeUtils.Encode(type)); }
public string Source(Request request) { Dictionary <string, string> sourceHeaders = request.Headers; Dictionary <string, string> lowerKeySourceHeaders = new Dictionary <string, string>(sourceHeaders.Count); foreach (KeyValuePair <string, string> pair in sourceHeaders) { lowerKeySourceHeaders.Add(pair.Key.ToLower(), pair.Value); if (signAll) { headerKeys.Add(pair.Key.ToLower()); } } try { lowerKeySourceHeaders.Add("host", request.Host); headerKeys.Add("host"); } catch (Exception) { } if (signAll) { try { long contentLength = 0; if (request.Body != null) { contentLength = request.Body.ContentLength; } if (contentLength > 0) { lowerKeySourceHeaders.Add("content-length", contentLength.ToString()); headerKeys.Add("content-length"); } } catch (Exception) {} } Dictionary <string, string> sourceParameters = request.Url.GetQueryParameters(); Dictionary <string, string> lowerKeySourceParameters = new Dictionary <string, string>(sourceParameters.Count); foreach (KeyValuePair <string, string> pair in sourceParameters) { lowerKeySourceParameters.Add(pair.Key.ToLower(), pair.Value); if (signAll) { parameterKeys.Add(pair.Key.ToLower()); } } string path = URLEncodeUtils.Decode(request.Url.Path); return(GenerateSource(request.Method, path, lowerKeySourceParameters, lowerKeySourceHeaders)); }
public string[] Calculate(List <string> keys, Dictionary <string, string> dict, bool isNeedEncode) { StringBuilder resultBuilder = new StringBuilder(); StringBuilder keyResultBuilder = new StringBuilder(); foreach (string key in keys) { if (!dict.ContainsKey(key)) { // 排除一些不可能存在的key // 排除一些不可能存在的key continue; } string value = dict[key]; if (value != null) { if (isNeedEncode) { resultBuilder.Append(key).Append('=').Append(URLEncodeUtils.Encode(value)).Append('&'); } else { resultBuilder.Append(key).Append('=').Append(value).Append('&'); } keyResultBuilder.Append(key).Append(';'); } else { resultBuilder.Append(key).Append('=').Append('&'); keyResultBuilder.Append(key).Append(';'); } } string result = resultBuilder.ToString(); string keyResult = keyResultBuilder.ToString(); if (result.EndsWith("&", StringComparison.OrdinalIgnoreCase)) { result = result.Substring(0, result.Length - 1); keyResult = keyResult.Substring(0, keyResult.Length - 1); return(new string[] { result, keyResult }); } return(null); }
public string GenerateSign(string method, string key, Dictionary <string, string> queryParameters, Dictionary <string, string> headers, long signDurationSecond) { try { string signTime = null; if (signDurationSecond > 0) { long currentTimeSecond = TimeUtils.GetCurrentTime(TimeUnit.Seconds); signTime = String.Format("{0};{1}", currentTimeSecond, currentTimeSecond + signDurationSecond); } Dictionary <string, string> encodeQuery = null; if (queryParameters != null) { encodeQuery = new Dictionary <string, string>(queryParameters.Count); foreach (KeyValuePair <string, string> keyValuePair in queryParameters) { if (keyValuePair.Key == null || keyValuePair.Key == "") { continue; } else if (keyValuePair.Value == null) { encodeQuery[URLEncodeUtils.Encode(keyValuePair.Key).ToLower()] = URLEncodeUtils.Encode(""); } else { encodeQuery[URLEncodeUtils.Encode(keyValuePair.Key).ToLower()] = URLEncodeUtils.Encode(keyValuePair.Value); } } } return(CosXmlSigner.GenerateSign(method, key, encodeQuery, headers, signTime, credentialProvider.GetQCloudCredentialsCompat(null))); } catch (CosClientException) { throw; } catch (Exception ex) { throw new CosClientException((int)CosClientError.InvalidArgument, ex.Message, ex); } }
public void TestSTS(string secretId, string secretKey, string policy) { string camHost = "sts.api.qcloud.com"; string camPath = "/v2/index.php"; string camMethod = "GET"; bool isHttps = true; Dictionary <string, string> queryParameters = new Dictionary <string, string>(); queryParameters.Add("policy", policy); queryParameters.Add("name", "brady"); queryParameters.Add("Action", "GetFederationToken"); queryParameters.Add("SecretId", secretId); queryParameters.Add("Nonce", new Random().Next(1, int.MaxValue).ToString()); long time = TimeUtils.GetCurrentTime(TimeUnit.SECONDS); queryParameters.Add("Timestamp", time.ToString()); queryParameters.Add("RequestClient", "net-sdk-v5"); queryParameters.Add("durationSeconds", 7200.ToString()); string plainText = MakeSignPlainText(queryParameters, camMethod, camHost, camPath); string hamcSha1 = DigestUtils.GetHamcSha1ToBase64(plainText, Encoding.UTF8, secretKey, Encoding.UTF8); queryParameters.Add("Signature", hamcSha1); HttpUrl httpUrl = new HttpUrl(); httpUrl.Scheme = isHttps ? "https" : "http"; httpUrl.Host = camHost; httpUrl.Path = camPath; Dictionary <string, string> tmp = new Dictionary <string, string>(queryParameters.Count); foreach (KeyValuePair <string, string> pair in queryParameters) { tmp.Add(pair.Key, URLEncodeUtils.Encode(pair.Value).Replace("%20", "+")); } queryParameters.Clear(); httpUrl.SetQueryParameters(tmp); SetSTSRequest(camMethod, isHttps, httpUrl); }
/// <summary> /// header 默认不 encode /// </summary> /// <param name="key">不能为null 即不包含空格,即 位于(\u0020, \u007F),超过这个范围,urlencode</param> /// <param name="value">可以为null,为空,且位于(\u001f,\u007F) 和 '\t',超过这个范围,urlencode</param> /// <param name="isNeedUrlEncode"></param> public void SetRequestHeader(string key, string value, bool isNeedUrlEncode) { try { if (value == null) { value = ""; } if (isNeedUrlEncode) { value = URLEncodeUtils.Encode(value); } headers.Add(key, value); } catch (ArgumentException) { // cover the current value // cover the current value headers[key] = value; } }
/// <summary> /// url 部分都统一 url encode /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="isNeedUrlEncode"></param> public void SetQueryParameter(string key, string value, bool isNeedUrlEncode) { try { if (value == null) { value = ""; } if (isNeedUrlEncode) { value = URLEncodeUtils.Encode(value); } queryParameters.Add(key, value); } catch (ArgumentNullException) { QLog.D(TAG, "SetQueryParameter: key ==null"); } catch (ArgumentException) { queryParameters[key] = value; // cover the current value } }
public string GenerateSignURL(PreSignatureStruct preSignatureStruct) { try { if (preSignatureStruct.httpMethod == null) { throw new CosClientException((int)CosClientError.InvalidArgument, "httpMethod = null"); } if (preSignatureStruct.key == null) { throw new CosClientException((int)CosClientError.InvalidArgument, "key = null"); } StringBuilder urlBuilder = new StringBuilder(); if (preSignatureStruct.isHttps) { urlBuilder.Append("https://"); } else { urlBuilder.Append("http://"); } if (preSignatureStruct.host == null) { StringBuilder host = new StringBuilder(); if (preSignatureStruct.bucket == null) { throw new CosClientException((int)CosClientError.InvalidArgument, "bucket = null"); } if (preSignatureStruct.bucket.EndsWith("-" + preSignatureStruct.appid)) { host.Append(preSignatureStruct.bucket); } else { host.Append(preSignatureStruct.bucket).Append("-") .Append(preSignatureStruct.appid); } host.Append(".cos.") .Append(preSignatureStruct.region) .Append(".myqcloud.com"); urlBuilder.Append(host.ToString()); // host 入签 if (preSignatureStruct.signHost) { if (preSignatureStruct.headers == null) { preSignatureStruct.headers = new Dictionary <string, string>(); } if (!preSignatureStruct.headers.ContainsKey("host")) { preSignatureStruct.headers.Add("host", host.ToString()); } } } else { urlBuilder.Append(preSignatureStruct.host); // host 入签 if (preSignatureStruct.signHost) { if (preSignatureStruct.headers == null) { preSignatureStruct.headers = new Dictionary <string, string>(); } preSignatureStruct.headers.Add("host", preSignatureStruct.host); } } if (!preSignatureStruct.key.StartsWith("/")) { preSignatureStruct.key = "/" + preSignatureStruct.key; } urlBuilder.Append(preSignatureStruct.key); string sign = GenerateSign(preSignatureStruct.httpMethod, preSignatureStruct.key, preSignatureStruct.queryParameters, preSignatureStruct.headers, preSignatureStruct.signDurationSecond); StringBuilder queryBuilder = new StringBuilder(); if (preSignatureStruct.queryParameters != null && preSignatureStruct.queryParameters.Count > 0) { foreach (KeyValuePair <string, string> keyValuePair in preSignatureStruct.queryParameters) { if (keyValuePair.Key == null || keyValuePair.Key == "") { continue; } queryBuilder.Append(URLEncodeUtils.Encode(keyValuePair.Key)).Append('=').Append(URLEncodeUtils.Encode(keyValuePair.Value)); queryBuilder.Append('&'); } } // 针对需要二次 Encode 的 request Param 特殊处理 Regex rgx = new Regex("q-url-param-list=.*&q-signature"); string paramlist = rgx.Match(sign).ToString().Split('=')[1].ToString().Split('&')[0].ToString(); paramlist = paramlist.Trim('&'); paramlist = URLEncodeUtils.Encode(paramlist).ToLower(); string encodedStr = "q-url-param-list=" + paramlist + "&q-signature"; sign = rgx.Replace(sign, encodedStr); queryBuilder.Append(sign); urlBuilder.Append("?").Append(queryBuilder.ToString()); return(urlBuilder.ToString()); } catch (CosClientException) { throw; } catch (Exception ex) { throw new CosClientException((int)CosClientError.InvalidArgument, ex.Message, ex); } }
public string GenerateSignURL(PreSignatureStruct preSignatureStruct) { try { if (preSignatureStruct.httpMethod == null) { throw new CosClientException((int)CosClientError.InvalidArgument, "httpMethod = null"); } if (preSignatureStruct.key == null) { throw new CosClientException((int)CosClientError.InvalidArgument, "key = null"); } StringBuilder urlBuilder = new StringBuilder(); if (preSignatureStruct.isHttps) { urlBuilder.Append("https://"); } else { urlBuilder.Append("http://"); } if (preSignatureStruct.host == null) { if (preSignatureStruct.bucket == null) { throw new CosClientException((int)CosClientError.InvalidArgument, "bucket = null"); } if (preSignatureStruct.bucket.EndsWith("-" + preSignatureStruct.appid)) { urlBuilder.Append(preSignatureStruct.bucket); } else { urlBuilder.Append(preSignatureStruct.bucket).Append("-") .Append(preSignatureStruct.appid); } urlBuilder.Append(".cos.") .Append(preSignatureStruct.region) .Append(".myqcloud.com"); } else { urlBuilder.Append(preSignatureStruct.host); } if (!preSignatureStruct.key.StartsWith("/")) { preSignatureStruct.key = "/" + preSignatureStruct.key; } urlBuilder.Append(preSignatureStruct.key); string sign = GenerateSign(preSignatureStruct.httpMethod, preSignatureStruct.key, preSignatureStruct.queryParameters, preSignatureStruct.headers, preSignatureStruct.signDurationSecond); StringBuilder queryBuilder = new StringBuilder(); if (preSignatureStruct.queryParameters != null && preSignatureStruct.queryParameters.Count > 0) { foreach (KeyValuePair <string, string> keyValuePair in preSignatureStruct.queryParameters) { queryBuilder.Append(keyValuePair.Key).Append('=').Append(URLEncodeUtils.Encode(keyValuePair.Value)); queryBuilder.Append('&'); } } queryBuilder.Append(sign); urlBuilder.Append("?").Append(queryBuilder.ToString()); return(urlBuilder.ToString()); } catch (CosClientException) { throw; } catch (Exception ex) { throw new CosClientException((int)CosClientError.InvalidArgument, ex.Message, ex); } }