2012-05-17 41 views
0

我正在使用post方法將json對象發送到服務器。下面是我的代碼。Xcode中的webservices 405錯誤

代碼

-(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:@"[email protected]" forKey:@"EmailAddress"]; 
    [dic setValue:@"12341" forKey:@"Password"]; 
    [dic setValue:@"ra1" forKey:@"FullName"]; 

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

    NSString *newurlString=[dic JSONRepresentation]; 

    NSString *[email protected]"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); 
} 

我的問題是我得到的響應代碼405.I不知道是哪裏的問題?好心幫助advance.Let me.Thanks我知道如果你需要更多的細節。

回答

1

405手段 - 不允許的方法

(在Request-Line中指定的方法不允許由Request-URI所標識的資源的應答必須包括允許含有的用於有效方法的列表頭所請求的資源。Status Code Definitions

所以,可能是它的,因爲你正在使用POST方法,和WebService不允許它。

+0

你多告訴我一些關於「應答必須包括一個允許包含的表頭請求資源的有效方法「 – Tendulkar