使用.net Web服务从iphone调用http post URL

前端之家收集整理的这篇文章主要介绍了使用.net Web服务从iphone调用http post URL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我点击按钮时,我需要从我的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. ---&gt; Data at the root level is invalid. Line 1,position 1.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

请帮帮我.!!!

谢谢,
世斌

解决方法

您可以使用以下内容将HTTP方法设置为使用POST而不是默认GET:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL];
[request setHTTPMethod:@"POST"];
原文链接:https://www.f2er.com/html/232343.html

猜你在找的HTML相关文章