0

I am sending json object to a server using the post method.Below is my code.

Code

    -(IBAction)ButtonClicked:(id)sender{

    NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithCapacity:1];
    [dic setValue:@"ra" forKey:@"Owner"];

    [dic setValue:@"YES" forKey:@"IsMale"];

    [dic setValue:[NSNumber numberWithInt:2] forKey:@"ImageId"];

    [dic setValue:@"abc@pikesol.com1"    forKey:@"EmailAddress"];
    [dic setValue:@"12341" forKey:@"Password"];
    [dic setValue:@"ra1" forKey:@"FullName"];

    [dic setValue:@"12-03-1987" forKey:@"DOB"];

    NSString *newurlString=[dic JSONRepresentation];

    NSString *url=@"https://sample.com/Discover/create";
    NSData *postData = [newurlString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSLog(@"urlString::%@",newurlString);
    NSLog(@"postLength::%@",postLength);

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:300];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self];

    if( theConnection )
    {
        webData = [[NSMutableData data] retain];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
    int code = [httpResponse statusCode];
    NSLog(@"code is : %d",code);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString* newStr = [[[NSString alloc] initWithData:webData
                                              encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"%@",newStr);
}

My Problem is I am getting the response code 405.I don't know where the problem is?kindly help me.Thanks in advance.Let me know if you need any more details.

James A Mohler
  • 10,562
  • 14
  • 41
  • 65
Tendulkar
  • 5,570
  • 2
  • 25
  • 52

1 Answers1

2

405 means - Method Not Allowed

(The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.Status Code Definitions

So, may be its because , you are using POST method, and webservice does not allow it.

waheeda
  • 1,097
  • 1
  • 9
  • 18
  • could you tell me more about "The response MUST include an Allow header containing a list of valid methods for the requested resource" – Tendulkar May 17 '12 at 07:16