1
我有一個關於xml調用REST Web服務的問題,我必須將以下參數傳遞給我的Web服務,格式如下:其他Web服務調用 - XML POST請求 - iPhone
http://XXX.XXX.XXX.XXX/gayanAPI/GayanRESTService.svc/login
所以我寫this.But其做出一些錯誤代碼。 (響應代碼:400)我的示例代碼如下;
//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
}
它是什麼原因。我在創建XML請求時犯了一個錯誤。請幫我解決這個問題?你能給我一個這樣的代碼嗎?我厭倦了這一點。
非常感謝。
是的,非常感謝。:) –