Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception while adding argument to NSMutableURLRequest

I am getting the following exception when adding an xml body to a POST request. I get the same exception when doing the same with GET. Any help would be appreciated.

Code

- (IBAction)buttonTapped:(id)sender {
NSLog(@"buttonTapped");

//Creating the request
NSMutableURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.1:9081/Transport/services/DriverService"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval: 30.0];
[ request setHTTPMethod: @"POST"];
//creating connection and sending it
NSURLConnection *connection = [ [NSURLConnection alloc] initWithRequest:request delegate:self];

NSString *xmlString = @"<Driver type=\"GetName\"><DriverID>1</DriverID></Driver>";

[request setHTTPBody:[xmlString 
                      dataUsingEncoding:NSUTF8StringEncoding]];

//checking if connection is good
if (connection){
    NSLog (@"Connection successfull!");
    self.responseData = [NSMutableData data];
}
else
{
    NSLog (@"Connection failed");
}
}

Exception

Exception:
2011-03-02 09:22:12.055 XML[1125:207] buttonTapped

2011-03-02 09:22:12.056 XML[1125:207] -[NSURLRequest setHTTPMethod:]: unrecognized selector sent to instance 0x4b1a970

2011-03-02 09:22:12.057 XML[1125:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLRequest setHTTPMethod:]: unrecognized selector sent to instance 0x4b1a970'

*** Call stack at first throw:
(
    0   CoreFoundation                      0x00dafbe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f045c2 objc_exception_throw + 47
    2   CoreFoundation                      0x00db16fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00d21366 ___forwarding___ + 966
    4   CoreFoundation                      0x00d20f22 _CF_forwarding_prep_0 + 50
    5   XML                                 0x00002bc4 -[XMLViewController buttonTapped:] + 164
    6   UIKit                               0x002b8a6e -[UIApplication sendAction:to:from:forEvent:] + 119
    7   UIKit                               0x003471b5 -[UIControl sendAction:to:forEvent:] + 67
    8   UIKit                               0x00349647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    9   UIKit                               0x003481f4 -[UIControl touchesEnded:withEvent:] + 458
    10  UIKit                               0x002dd0d1 -[UIWindow _sendTouchesForEvent:] + 567
    11  UIKit                               0x002be37a -[UIApplication sendEvent:] + 447
    12  UIKit                               0x002c3732 _UIApplicationHandleEvent + 7576
    13  GraphicsServices                    0x016e5a36 PurpleEventCallback + 1550
    14  CoreFoundation                      0x00d91064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    15  CoreFoundation                      0x00cf16f7 __CFRunLoopDoSource1 + 215
    16  CoreFoundation                      0x00cee983 __CFRunLoopRun + 979
    17  CoreFoundation                      0x00cee240 CFRunLoopRunSpecific + 208
    18  CoreFoundation                      0x00cee161 CFRunLoopRunInMode + 97
    19  GraphicsServices                    0x016e4268 GSEventRunModal + 217
    20  GraphicsServices                    0x016e432d GSEventRun + 115
    21  UIKit                               0x002c742e UIApplicationMain + 1160
    22  XML                                 0x000028dc main + 102
    23  XML                                 0x0000286d start + 53
)
terminate called after throwing an instance of 'NSException'
like image 958
Steve Avatar asked Mar 02 '11 14:03

Steve


1 Answers

NSMutableURLRequest *request = [NSURLRequest requestWithURL:...

should be

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:...

Although you declare request to have NSMutableURLRequest type, you create an instance of NSURLRequest class, which does not responds to -setHTTPMethod: selector.

like image 65
Vladimir Avatar answered Oct 18 '22 23:10

Vladimir