0

我正在向服务器发送 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];
}
4

3 回答 3

1

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

于 2013-02-13T04:52:56.640 回答
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];
于 2013-02-13T05:43:53.720 回答
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;

}
于 2013-02-13T05:14:12.450 回答