我无法使用AFNetworking NSMutableURLRequest
上传图片 - 它总是超时。
我可以向同一台服务器发出成功GET
次请求:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
但AFNetworking NSMutableURLRequest
总是超时。
我的代码如下所示:
// 1. Create AFHTTPRequestSerializer which will create your request.
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
// 2. Create an NSMutableURLRequest.
NSMutableURLRequest *request =
[serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://192.168.0.105/upload2.php"
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:@"upfile"
fileName:@"file://Users/wangyan/Downloads/idcard.png"
mimeType:@"image/jpeg"];
}];
// 3. Create and use AFHTTPRequestOperationManager to create an AFHTTPRequestOperation from the NSMutableURLRequest that we just created.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *operation =
[manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure %@", error.description);
}];
// 4. Set the progress block of the operation.
[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
long long totalBytesWritten,
long long totalBytesExpectedToWrite) {
NSLog(@"Wrote %ld/%ld", (long)totalBytesWritten, (long)totalBytesExpectedToWrite);
}];
// 5. Begin!
[operation start];
以下是我收到的错误:
失败错误域= NSURLErrorDomain代码= -1001“请求超时。”的UserInfo = 0x7fab1ada7d30
{NSUnderlyingError = 0x7fab1ad7de00“请求超时。”
NSErrorFailingURLStringKey = http://192.168.0.105/upload2.php,
NSErrorFailingURLKey = http://192.168.0.105/upload2.php,
NSLocalizedDescription =请求超时。}
我在服务器上找不到任何问题。 有什么建议吗?