加密请求在iOS中返回415状态代码

时间:2014-01-07 09:25:13

标签: ios encryption

我已使用Apple文档和此站点的建议成功加密了soap请求。

我正背面的(0)< 69e85088 38687d1a bd4e78a2 1d8c391c 0bcfb73f fa8a35d6 b25225c1 58ba14ac 92c47a5a 169da482 0f5af8b3 5522559c fda0e182 23634968 2d93eaba 13a24739 8860a0a7 8f827ef2 103a019f 7c8250fa c1f7333f cc40e6d9 03078308 e7c46281 f032680f bd6fbf8d 4077af79 9bb5c364 975a8414 1d56581a 3e3182d9 0fb31965 0fad495c b1773dfd 652e43d2 860d5c73 3a328253 12ec03e0 e75f1745 7ea6b254 6def8b2f 39d2533a dd97cd8a 23f55ed4 6a61986b 528436fc 45d283d2 486c0d6d 452284ab b47ce74e bab36833 03ea3453 afbb50b6 d13e1073 2673b10a 9f4b771e a3119f05 eee3508d 8cd7c803 898abada 3ef58e06 9f7dde79 98548f8d 71a87912>

当我从我的encryptWithPublicKey方法打印出状态代码时,表明它已成功运行。

当我做出要求时

NSMutableURLRequest *faURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:svcURL]];
    [faURLRequest addValue:@"soapMethod" forHTTPHeaderField:@"SOAPAction"];
    [faURLRequest addValue:@"application/soap; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    NSData *dta =[self encryptWithPublicKey:soapMessage];

    NSLog(@"%@",[dta description]);

    [faURLRequest setValue:[NSString stringWithFormat:@"%i",dta.length] forHTTPHeaderField:@"Content-Length"];

    [faURLRequest setHTTPMethod:@"POST"];

     [faURLRequest setHTTPBody:dta];

我的状态代码为415。

有人能指出我的代码有什么问题吗?

encryptWithPublicKey方法是这样的:

- (NSData *)encryptWithPublicKey:(NSString *)requestString
{

OSStatus status = noErr;

size_t cipherBufferSize;
uint8_t *cipherBuffer;                     // 1

uint8_t *dataToEncrypt= (uint8_t *)[requestString UTF8String];



size_t dataLength = sizeof(dataToEncrypt)/sizeof(dataToEncrypt[0]);

SecKeyRef publicKey = NULL;                                 // 3

NSString *certPath = [[NSBundle mainBundle] pathForResource:@"devservercertificate" ofType:@"pfx"];
NSData *certData = [[NSData alloc] initWithContentsOfFile:certPath];
CFDataRef certDataRef = (__bridge_retained CFDataRef)certData;
//SecCertificateRef cert = SecCertificateCreateWithData(NULL, certDataRef);

// Establish a chain of trust anchored on our bundled certificate.
SecIdentityRef myIdentity;
SecTrustRef myTrust;
extractIdentityAndTrust(certDataRef, &myIdentity, &myTrust);

SecCertificateRef myCertificate;
SecIdentityCopyCertificate(myIdentity, &myCertificate);
publicKey = SecTrustCopyPublicKey(myTrust);

// NSLog(@"%@",publicKey);

//  Allocate a buffer

cipherBufferSize = SecKeyGetBlockSize(publicKey);
cipherBuffer = malloc(cipherBufferSize);

//  Error handling

if (cipherBufferSize < sizeof(dataToEncrypt)) {
    // Ordinarily, you would split the data up into blocks
    // equal to cipherBufferSize, with the last block being
    // shorter. For simplicity, this example assumes that
    // the data is short enough to fit.
    printf("Could not decrypt.  Packet too large.\n");
    return NULL;
}

// Encrypt using the public.
status = SecKeyEncrypt(    publicKey,
                       kSecPaddingPKCS1,
                       dataToEncrypt,
                       (size_t) dataLength,
                       cipherBuffer,
                       &cipherBufferSize
                       );                              // 8

//  Error handling
//  Store or transmit the encrypted text

if (publicKey) CFRelease(publicKey);

NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];
free(cipherBuffer);
NSLog(@"(%d) %@", (int)status, encryptedData);
return encryptedData;
}

由于

1 个答案:

答案 0 :(得分:0)

我正在回答我自己的问题!

似乎SOAP服务的头信息不正确,请求正文也有错误...

这样做会浪费几个小时:D