0

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];
}
4

3 に答える 3

1

HTTPコード405は「メソッドは許可されていません」を意味し、この特定のURIのPOSTリクエストを受け入れません。サーバーはPOST要求を受け入れるように構成するか、別のURIを提供する必要があります。

于 2013-02-13T04:52:56.640 に答える
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 に答える
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 に答える