Xcode中的webservices 405错误

时间:2012-05-17 06:50:25

标签: iphone xcode web-services http-status-code-405

我使用post方法将json对象发送到服务器.Below是我的代码。

代码

    -(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);
}

我的问题是我收到响应代码405.我不知道问题出在哪里?请帮助我。提前谢谢。如果您需要更多详细信息,请告诉我。

1 个答案:

答案 0 :(得分:2)

405表示 - 方法不允许

(Request-URI标识的资源不允许使用Request-Line中指定的方法。响应必须包含Allow标头,其中包含所请求资源的有效方法列表。Status Code Definitions

所以,可能是因为,你正在使用POST方法,而webservice不允许它。