RESTWeb服务调用-XML POST请求-iPhone

RESTWeb服务调用-XML POST请求-iPhone,iphone,web-services,rest,ios5,xcode4.2,Iphone,Web Services,Rest,Ios5,Xcode4.2,我对RESTWeb服务的xml调用有一个问题,我必须以以下格式将以下参数传递给我的web服务: 所以我写了一个代码,但是它会出错。(响应代码:400)下面是我的示例代码 //prepare request NSString *urlString = [NSString stringWithFormat:@"http://XXX.XXX.XXX.XXX/gayanAPI/GayanRESTService.svc/login"]; NSMutableURLRequest *request =

我对RESTWeb服务的xml调用有一个问题,我必须以以下格式将以下参数传递给我的web服务:

所以我写了一个代码,但是它会出错。(响应代码: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 
}
//准备请求
NSString*urlString=[NSString stringWithFormat:@]http://XXX.XXX.XXX.XXX/gayanAPI/GayanRESTService.svc/login"];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];
[请求设置URL:[NSURL URLWithString:urlString]];
[请求设置HttpMethod:@“POST”];
//设置标题
NSString*contentType=[NSString stringWithFormat:@“应用程序/xml”];
[请求addValue:contentType for HttpHeaderField:@“内容类型”];
//创造身体
NSMutableData*postBody=[NSMutableData];
[postBody appendData:[[NSString stringWithFormat:@”“]dataUsingEncoding:NSUTF8StringEncoding];
[postBody appendData:[[NSString stringWithFormat:@“%@”,@“200”]数据使用编码:NSUTF8StringEncoding];
[postBody appendData:[[NSString stringWithFormat:@“%@”,“123”]dataUsingEncoding:NSUTF8StringEncoding];
[postBody appendData:[[NSString stringWithFormat:@“%@,@“1000”]数据使用编码:NSUTF8StringEncoding];
[postBody appendData:[[NSString stringWithFormat:@“%@”,“1234”]数据使用编码:NSUTF8StringEncoding];
[postBody appendData:[[NSString stringWithFormat:@”“]dataUsingEncoding:NSUTF8StringEncoding];
//职位
[请求setHTTPBody:postBody];
//得到回应
NSHTTPURLResponse*urlResponse=nil;
NSError*error=[[NSError alloc]init];
NSData*responseData=[NSURLConnection sendSynchronousRequest:request ReturnInResponse:&urlResponse错误:&error];
NSString*result=[[NSString alloc]initWithData:responseData编码:NSUTF8StringEncoding];
NSLog(@“响应代码:%d”,[urlResponse statusCode]);
如果([urlResponse statusCode]>=200&&[urlResponse statusCode]<300){
NSLog(@“响应:%@”,结果);
//这是你得到的答复
}
原因是什么。我在创建XML请求时犯了一个错误。请帮我解决这个问题?你能给我一个密码吗?我受够了

非常感谢。

在这一行

[postBody appendData:[[NSString stringWithFormat:@"<Login xmlns="">"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@”“]dataUsingEncoding:NSUTF8StringEncoding];
您需要用反斜杠(\)引用双引号(“),如下所示:

[postBody appendData:[[NSString stringWithFormat:@"<Login xmlns=\"\">"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@”“]dataUsingEncoding:NSUTF8StringEncoding];
不过,我不确定这是否导致了错误