当我点击按钮时,我需要从我的iPhone拨打网址.
url从UITextField获取一个值作为参数,它也使用POST方法来调用webservice.我如何使用我的iPhone中的URL.我用GET方法做了同样的事情.但为此我创建了一个带有textfield值的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>
请帮帮我.!!!
谢谢,
世斌