将JSON对象请求发送到服务器

时间:2013-02-13 04:38:11

标签: ios objective-c json cocoa-touch

Iam向服务器发送JSON对象请求,但服务器返回状态码405.如何解决此问题。请任何人帮助我。 我的代码:

+(NSData *)GpBySalesDetailed:(NSMutableDictionary *)spDetailedDict{

NSLog(@"spDetailedDict:%@",spDetailedDict);
NSString *dataString = [spDetailedDict JSONRepresentation];
NSLog(@"%@dataString",dataString);

return [dataString dataUsingEncoding:NSUTF8StringEncoding];
}
-(void)requestWithUrl:(NSURL *)url WithJsonData:(NSData *)JsonData
{
   NSMutableURLRequest *urlRequest=[[NSMutableURLRequest alloc]initWithURL:@"http://srbisolutions.com/SmartReportService.svc/GpBySalesPersonDetailed];
if (JsonData != nil) {
    [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:JsonData];
}
else
{
    [urlRequest setHTTPMethod:@"GET"];  
}
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
[conn start];
}

3 个答案:

答案 0 :(得分:1)

HTTP代码405表示“方法不允许”,它不接受对此特定URI的发布请求。服务器必须配置为接受POST请求,或者它应该提供另一个URI。

答案 1 :(得分:0)

试试这个

 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:YOURURL  
                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0 ];

        NSLog(@"final request is %@",request);


        [request setHTTPMethod:@"POST"];

        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

     //Here postData is a Dictionary with key values in web services format  use ur own dic

        [request setHTTPBody:[[self convertToJSON:postData] dataUsingEncoding:NSUTF8StringEncoding]];  


        NSString    *contentLength = [NSString stringWithFormat:@"%d",[[request HTTPBody] length]]; 
        [request setValue:contentLength forHTTPHeaderField:@"Content-Length"];

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

        if (connection) 
        {
            self.responseData = [NSMutableData data];
        }






//============JSON CONVERSION========
-(NSString *)convertToJSON:(id)requestParameters
{
    NSData *jsonData   = [NSJSONSerialization dataWithJSONObject:requestParameters options:NSJSONWritingPrettyPrinted error:nil];

    NSLog(@"JSON DATA LENGTH = %d", [jsonData length]);

    NSString *jsonString    = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSLog(@"JSON STR LENGTH = %d", [jsonString length]);

    return jsonString;

}

答案 2 :(得分:0)

 NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"yourURL"]];


            [theRequest setHTTPMethod:@"POST"];

            NSDictionary *jsonRequest =
            [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@//add your objects
                                                 ]
                                        forKeys:[NSArray arrayWithObjects:
                                                 title,
                                                 link,
                                                 nil]];

            NString *jsonBody = [jsonRequest JSONRepresentation];
            NSLog(@"The request is %@",jsonBody);

            NSData *bodyData = [jsonBody dataUsingEncoding:NSUTF8StringEncoding];
            [theRequest setHTTPBody:bodyData];

            [theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
            // create the connection with the request

            // and start loading the data

            theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];