我正在使用POST方法与iPhone应用程序中的.NET Web服务进行通信,但我没有收到XML作为响应。这里是我的代码:如何使用POST方法在iPhone中使用.NET Web服务?
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);
请帮帮我。提前致谢。
作为回应你会得到什么? – kennbrodhagen
这是你正在与之通信的真实URL地址(*'Myserver/EventService.svc'*)吗?你遇到了什么错误? – Perception
感谢您回复我。我变得空白(没有)作为回应。 没有那不是我的实际服务器。我的实际服务器正在运行,并在浏览器中返回xml。 –