iOS errorcode为-1011 问题及解答

这篇博客主要介绍了在遇到iOS应用中出现errorcode为-1011的问题时,如何定位并解决。问题根源是服务器返回了内部错误500。通过检查Tomcat控制台输出和日志,可以发现数据库主键冲突导致的错误。解决方案未在文中详述。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在程序执行中发现错误,请求时请求数据并没有被服务器接收

查看error信息

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x13e764750> { URL: http://192.168.1.122:8080/dongci/app/finshedItem } { status code: 500, headers {
    Connection = close;
    "Content-Length" = 93;
    "Content-Type" = "application/json;charset=UTF-8";
    Date = "Thu, 21 Jul 2016 05:09:59 GMT";
    Server = "Apache-Coyote/1.1";
} }, NSErrorFailingURLKey=http://192.168.1.122:8080/dongci/app/finshedItem, com.alamofire.serialization.response.error.data=<0a7b0a20 20226572 72223a20 7b0a2020 20202263 6f646522 3a203530 302c0a20 20202022 6d736722 3a2022e6 97a0e6b3 95e5a484 e79086e6 82a8e79a 84e8afb7 e6b18222 2c0a2020 20202265 76656e74 6964223a 2022220a 20207d0a 7d>, NSLocalizedDescription=Request failed: internal server error (500)}


去官方文档上查看错误原因

NSURLErrorBadServerResponse
Returned when the URL Loading system receives bad data from the server.
This is equivalent to the “500 Server Error” message sent by HTTP servers.

Available in OS X v10.2 and later.


出现这个问题,从错误信息中找到了答案Request failed: internal server error (500),这里所指的是服务端内部错误。向服务端请求查看错误,服务端查看错的方法:
1.查看tomcat控制台输出日志,从这里可以查看出tomcat这个应用是不是向服务端发送请求了,
2.查看tomcat的log日志,这里可以查看出代码到底错在哪里,什么原因导致的
我这里出现的问题是数据库中,主键冲突的原因导致报错,解决方案我就不详细说明了

好的,我来解答你的问题。 首先,需要在项目中添加`OpenSSL`库,可以通过`CocoaPods`进行添加,也可以手动添加。 然后,分别实现`AES-GCM`和`AES-ECB`的加密解密操作: ### AES-GCM加密解密 ```objective-c #import <CommonCrypto/CommonCrypto.h> #include <openssl/aes.h> #include <openssl/evp.h> #include <openssl/rand.h> #define kGCMNonceSize 12 // 96 bits #define kGCMTagSize 16 // 128 bits NSData *AESGCMEncrypt(NSData *key, NSData *nonce, NSData *aad, NSData *plainText, NSError **error) { if (nonce.length != kGCMNonceSize) { if (error != NULL) { *error = [NSError errorWithDomain:@"com.example.AESGCMEncrypt" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Invalid nonce size"}]; } return nil; } unsigned char *keyBytes = (unsigned char *)[key bytes]; unsigned char *nonceBytes = (unsigned char *)[nonce bytes]; unsigned char *aadBytes = (unsigned char *)[aad bytes]; unsigned char *plainTextBytes = (unsigned char *)[plainText bytes]; unsigned char tag[kGCMTagSize]; memset(tag, 0, kGCMTagSize); size_t cipherTextLength = plainText.length + kGCMTagSize; unsigned char *cipherTextBytes = malloc(cipherTextLength); memset(cipherTextBytes, 0, cipherTextLength); unsigned char *cipherTextPtr = cipherTextBytes; size_t cipherTextLen = 0; EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)nonce.length, NULL); EVP_EncryptInit_ex(ctx, NULL, NULL, keyBytes, nonceBytes); EVP_EncryptUpdate(ctx, NULL, (int *)&cipherTextLen, aadBytes, (int)aad.length); EVP_EncryptUpdate(ctx, cipherTextPtr, (int *)&cipherTextLen, plainTextBytes, (int)plainText.length); EVP_EncryptFinal_ex(ctx, cipherTextPtr+cipherTextLen, (int *)&cipherTextLen); EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, kGCMTagSize, tag); EVP_CIPHER_CTX_free(ctx); NSMutableData *cipherText = [[NSMutableData alloc] initWithBytes:cipherTextBytes length:cipherTextLen+kGCMTagSize]; [cipherText appendBytes:tag length:kGCMTagSize]; free(cipherTextBytes); return cipherText; } NSData *AESGCMDecrypt(NSData *key, NSData *nonce, NSData *aad, NSData *cipherText, NSError **error) { if (nonce.length != kGCMNonceSize) { if (error != NULL) { *error = [NSError errorWithDomain:@"com.example.AESGCMDecrypt" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Invalid nonce size"}]; } return nil; } unsigned char *keyBytes = (unsigned char *)[key bytes]; unsigned char *nonceBytes = (unsigned char *)[nonce bytes]; unsigned char *aadBytes = (unsigned char *)[aad bytes]; unsigned char *cipherTextBytes = (unsigned char *)[cipherText bytes]; unsigned char tag[kGCMTagSize]; memcpy(tag, cipherTextBytes+cipherText.length-kGCMTagSize, kGCMTagSize); size_t plainTextLength = cipherText.length - kGCMTagSize; unsigned char *plainTextBytes = malloc(plainTextLength); memset(plainTextBytes, 0, plainTextLength); unsigned char *plainTextPtr = plainTextBytes; size_t plainTextLen = 0; EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)nonce.length, NULL); EVP_DecryptInit_ex(ctx, NULL, NULL, keyBytes, nonceBytes); EVP_DecryptUpdate(ctx, NULL, (int *)&plainTextLen, aadBytes, (int)aad.length); EVP_DecryptUpdate(ctx, plainTextPtr, (int *)&plainTextLen, cipherTextBytes, (int)plainTextLength); EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, kGCMTagSize, tag); int ret = EVP_DecryptFinal_ex(ctx, plainTextPtr+plainTextLen, (int *)&plainTextLen); EVP_CIPHER_CTX_free(ctx); if (ret <= 0) { if (error != NULL) { *error = [NSError errorWithDomain:@"com.example.AESGCMDecrypt" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Decryption failed"}]; } return nil; } NSData *plainText = [[NSData alloc] initWithBytes:plainTextBytes length:plainTextLen]; free(plainTextBytes); return plainText; } ``` 使用示例: ```objective-c NSData *key = [NSData dataWithBytes:"01234567890123456789012345678901" length:32]; NSData *nonce = [NSData dataWithBytes:"012345678901" length:12]; NSData *aad = [@"additional authenticated data" dataUsingEncoding:NSUTF8StringEncoding]; NSData *plainText = [@"plain text" dataUsingEncoding:NSUTF8StringEncoding]; NSError *error = nil; NSData *cipherText = AESGCMEncrypt(key, nonce, aad, plainText, &error); if (cipherText != nil) { NSData *decryptedPlainText = AESGCMDecrypt(key, nonce, aad, cipherText, &error); NSLog(@"Decrypted plain text: %@", [[NSString alloc] initWithData:decryptedPlainText encoding:NSUTF8StringEncoding]); } else { NSLog(@"Encryption failed: %@", error.localizedDescription); } ``` ### AES-ECB加密解密 ```objective-c NSData *AESECBEncrypt(NSData *key, NSData *plainText) { unsigned char *keyBytes = (unsigned char *)[key bytes]; unsigned char *plainTextBytes = (unsigned char *)[plainText bytes]; size_t cipherTextLength = plainText.length + AES_BLOCK_SIZE; unsigned char *cipherTextBytes = malloc(cipherTextLength); memset(cipherTextBytes, 0, cipherTextLength); unsigned char *cipherTextPtr = cipherTextBytes; size_t cipherTextLen = 0; AES_KEY aesKey; AES_set_encrypt_key(keyBytes, (int)(key.length*8), &aesKey); AES_encrypt(plainTextBytes, cipherTextPtr, &aesKey); NSData *cipherText = [[NSData alloc] initWithBytes:cipherTextBytes length:cipherTextLen+AES_BLOCK_SIZE]; free(cipherTextBytes); return cipherText; } NSData *AESECBDecrypt(NSData *key, NSData *cipherText) { unsigned char *keyBytes = (unsigned char *)[key bytes]; unsigned char *cipherTextBytes = (unsigned char *)[cipherText bytes]; size_t plainTextLength = cipherText.length; unsigned char *plainTextBytes = malloc(plainTextLength); memset(plainTextBytes, 0, plainTextLength); unsigned char *plainTextPtr = plainTextBytes; size_t plainTextLen = 0; AES_KEY aesKey; AES_set_decrypt_key(keyBytes, (int)(key.length*8), &aesKey); AES_decrypt(cipherTextBytes, plainTextPtr, &aesKey); NSData *plainText = [[NSData alloc] initWithBytes:plainTextBytes length:plainTextLen]; free(plainTextBytes); return plainText; } ``` 使用示例: ```objective-c NSData *key = [NSData dataWithBytes:"01234567890123456789012345678901" length:32]; NSData *plainText = [@"plain text" dataUsingEncoding:NSUTF8StringEncoding]; NSData *cipherText = AESECBEncrypt(key, plainText); NSData *decryptedPlainText = AESECBDecrypt(key, cipherText); NSLog(@"Decrypted plain text: %@", [[NSString alloc] initWithData:decryptedPlainText encoding:NSUTF8StringEncoding]); ``` 注意:`AES-ECB`加密模式不安全,不建议使用。建议使用`AES-GCM`等更加安全的加密模式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值