创建对Microsoft Azure服务器的请求
-(void)createRequest{
NSString *strAccountName = @"assetmanagerwebmapfiles";
NSString *strContainerName = @"fieldclientgdbmapfiles";
NSString *strBlob = @"gsiscout.geodatabase";
NSString *strSecurityKey =@"gvXSY7kFx/juHRXFus7Hc2DyRViCkUBvAn9NwmvMU86cnemucqTxRX8FasdfTOmPHASYHav0Dasd89H4wGRRrfjat/pw==";
dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *now = [NSDate date];
NSString *iso8601StringDt1 = [dateFormatter stringFromDate:now];
NSDateComponents *components= [[NSDateComponents alloc] init];
[components setDay:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *strDate1 = [calendar dateByAddingComponents:components toDate:[NSDate date] options:0];
NSString *iso8601StringDt2 = [dateFormatter stringFromDate:strDate1];
NSString *strSignature = [self createSignatureSharedkey:strSecurityKey];
NSString *strURL = [NSString stringWithFormat:@"https://%@.blob.core.windows.net/%@/%@?sv=2012-02-12&st=%@&se=%@&sr=b&sp=r&sig=%@",strAccountName,strContainerName,strBlob, iso8601StringDt1,iso8601StringDt2,strSignature];
NSURL *blobURL = [NSURL URLWithString:strURL];
NSMutableURLRequest *blobRequest = [NSMutableURLRequest requestWithURL:blobURL];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:blobRequest delegate:self];
if (theConnection) {
receivedData = [NSMutableData data];
} else {
// We should inform the user that the connection failed.
}}
Genrating Shared Key“用于验证对blob的访问权限。签名是使用SHA256算法在字符串到符号和密钥上计算的HMAC,然后使用Base64编码进行编码。 “根据MS Azure Documents的建议。
-(NSString*)createSignatureSharedkey:(NSString *)key{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *now = [NSDate date];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *iso8601StringDt1 = [dateFormatter stringFromDate:now];
NSLog(@"Start time:%@",iso8601StringDt1);
NSDateComponents *components= [[NSDateComponents alloc] init];
[components setDay:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *strDate1 = [calendar dateByAddingComponents:components toDate:[NSDate date] options:0];
NSString *iso8601StringDt2 = [dateFormatter stringFromDate:strDate1];
NSLog(@"Expiry time:%@",iso8601StringDt2);
NSString *messageToSign = [NSString stringWithFormat:@"r\n%@\n%@\nassetmanagerwebmapfiles/fieldclientgdbmapfiles/gsiscout.geodatabase\n2012-02-12",iso8601StringDt1,iso8601StringDt2];
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [messageToSign cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData * HMACData =[[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
NSString *base64Hash = [HMACData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSLog(@"Encrypted hash: %@", base64Hash);
return base64Hash;}
现在这是NSURLConnection委托方法
-(void)connection:(NSConnection*)conn didReceiveResponse:(NSURLResponse *)response {
if (receivedData == NULL) {
receivedData = [[NSMutableData alloc] init];
}
[receivedData setLength:0];
NSLog(@"didReceiveResponse: responseData length:(%lu)", (unsigned long)receivedData.length);}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"---------- Finished Loading-------------");
NSString *jsonString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@",jsonString);}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"Failed: %@",[error description]);}
我从服务器获取的错误是
<Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:0d684b92-0001-0076-268e-435181000000
Time:2014-12-10T08:29:12.5587707Z</Message><AuthenticationErrorDetail>Signature did not match. String to sign used was r
2014-12-10
2014-12-11
/assetmanagerwebmapfiles/fieldclientgdbmapfiles/gsiscout.geodatabase
2012-02-12</AuthenticationErrorDetail></Error>
任何帮助将不胜感激。谢谢