2011-07-21 97 views
1

我正在使用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); 

请帮帮我。提前致谢。

+0

作为回应你会得到什么? – kennbrodhagen

+0

这是你正在与之通信的真实URL地址(*'Myserver/EventService.svc'*)吗?你遇到了什么错误? – Perception

+0

感谢您回复我。我变得空白(没有)作为回应。 没有那不是我的实际服务器。我的实际服务器正在运行,并在浏览器中返回xml。 –

回答

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); 
0

您是否尝试过较小的东西?说本地做一个HTTP帖子?或者用简单的html表单做一个http post到该服务器?

我自己已经遇到了这个问题,并决定退后一步,并开始一个简单的http post,以确保我的objective-c是第一个正确的。

我以前this tutorial让我开始-hope它有助于

相关问题