【发布时间】:2009-12-22 10:25:30
【问题描述】:
当我点击一个按钮时,我需要从我的 iphone 调用一个 url。
url 将 UITextField 中的值作为参数,它也使用 POST 方法来调用 web 服务。我如何在我的 iphone 中使用 URL。我用 GET 方法做了同样的事情。但为此,我使用文本字段中的值创建了一个 url 字符串。它不能用于 POST 之一。那我该怎么办。是否有任何示例代码。Web 服务也是“.asmx”类型。这就是我所做的.....
-(IBAction)loginButtonPressed:(id)sender{
NSString *post =[NSString stringWithFormat:@"name=myUserName&pass=myPassword"];
NSLog(post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://192.168.50.194:9989/webservice1.asmx?op=LoginAuthentication"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(data);
}
我得到的错误是
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">Server was unable to process request. ---> Data at the root level is invalid. Line 1, position 1.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>
请帮助我。!!!
谢谢, 世斌
【问题讨论】:
-
iphone-make-post-request-handle-cookie : stackoverflow.com/questions/1660927/…
标签: .net iphone http post web-services