Question

J'ai un problème en ce qui concerne les appels XML au service web REST, je dois passer des paramètres suivants à mes services Web au format suivant;

http://XXX.XXX.XXX.XXX/gayanAPI/GayanRESTService .svc / login

entrer image description ici

J'écrire un code pour this.But sa marque une erreur. ( Code de réponse: 400 ) Mon exemple de code est ci-dessous;

  //prepare request
NSString *urlString = [NSString stringWithFormat:@"http://XXX.XXX.XXX.XXX/gayanAPI/GayanRESTService.svc/login"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

//set headers
NSString *contentType = [NSString stringWithFormat:@"application/xml"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<Login xmlns="">"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<UserId>%@</UserId>",@"200"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<PassWord>%@</PassWord>",@"123"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<ResId>%@</ResId>",@"1000"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<DeviceId>%@</DeviceId>",@"1234"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"</Login>"] dataUsingEncoding:NSUTF8StringEncoding]];

//post
[request setHTTPBody:postBody];

//get response
NSHTTPURLResponse* urlResponse = nil;  
NSError *error = [[NSError alloc] init];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
    NSLog(@"Response: %@", result); 
    //here you get the response 
}

Quelle est la raison. Je ce que je fait une erreur quand Im créant requête XML. S'il vous plaît aidez-moi à résoudre ce problème? pouvez-vous me donner un code pour cela? Je me marre avec cela.

Merci beaucoup.

Était-ce utile?

La solution

Dans cette ligne,

[postBody appendData:[[NSString stringWithFormat:@"<Login xmlns="">"] dataUsingEncoding:NSUTF8StringEncoding]];

Vous devez citer entre guillemets ( ") avec barre oblique inverse (\) comme suit:

[postBody appendData:[[NSString stringWithFormat:@"<Login xmlns=\"\">"] dataUsingEncoding:NSUTF8StringEncoding]];

Je ne suis pas sûr si elle provoque l'erreur, cependant.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top