我正在使用POST方法与iPhone应用程序中的.NET Web服务进行通信,但我没有将XML作为响应返回。这是我的代码:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"Myserver/EventService.svc"]];
NSString *api = @"/registeruser";
NSData *body = [[NSString stringWithFormat:@"%@/%@/%@/%@/%@/%@/%@/%@/%@",
api, txtFName.text ,txtLName.text,
txtUName.text, txtEmail.text, txtPassword.text,
txtCity.text, txtState.text, str] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(@"url %@",url);
NSLog(@"body %@",[NSString stringWithFormat:@"%@/%@/%@/%@/%@/%@/%@/%@/%@",
api, txtFName.text, txtLName.text,
txtUName.text, txtEmail.text, txtPassword.text,
txtCity.text, txtState.text, str]);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPBody:body];
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
NSLog(@"body len %d",[body length]);
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *XMLString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"data %@",XMLString);
请帮帮我。提前致谢。
答案 0 :(得分:1)
我已经完成了以下操作。
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"Myserver/EventService.svc/webservice/%@/%@",para1,para2]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:0 forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *XMLString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"data %@",XMLString);
答案 1 :(得分:0)
你先试过一些小东西吗?说在本地做一个http帖子?或者使用简单的html表单向该服务器发送http帖子?
我自己遇到过这个问题,并决定退一步,开始使用一个简单的http帖子来确保我的objective-c首先是正确的。
我使用this tutorial让我开始 - 希望帮助