I am unable to use AFNetworking NSMutableURLRequest
to upload a picture -- it always times out.
I can make successful GET
requests to the same server:
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);
}];
But the AFNetworking NSMutableURLRequest
always times out.
My code looks like this:
// 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];
Here is the error I receive:
Failure Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7fab1ada7d30
{NSUnderlyingError=0x7fab1ad7de00 "The request timed out."
NSErrorFailingURLStringKey=http://192.168.0.105/upload2.php,
NSErrorFailingURLKey=http://192.168.0.105/upload2.php,
NSLocalizedDescription=The request timed out.}
I can find no problems on the server. Any suggestions?