使用.NET Web服务从iPhone调用一个HTTP POST网址 [英] Invoking a http post URL from iphone using .net web service

查看:336
本文介绍了使用.NET Web服务从iPhone调用一个HTTP POST网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用一个网址从我的iPhone,当我点击一个按钮。

I need to call a url from my iphone when i tap on a button.

的URL是从的UITextField取一个值作为参数,也它使用的是POST方法来调用web服务。我如何使用URL在我的iPhone。我做了同样的用GET方法。但是对于我创建了一个URL字符串从文本框的值。它不能被利用为POST之一。还等什么768,16我做的。有什么样$ C $下的..另外,Web服务是一种的.asmx类型。这是我做了什么......

The url is taking a value from the UITextField as argument, also it is using a POST method to invoke the webservice. how do i use the URL in my iphone. I had done the same with GET method. but for that i created a url string with the value from the textfield. it cant be use for the POST one. so what shoud i do for that. is there any sample code for this.. Also the web service is a ".asmx" type. This is what i had done.....

-(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开始使用这样的事情:

You can set the HTTP method to use POST instead of the default GET using something like this:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL];
[request setHTTPMethod:@"POST"];

这篇关于使用.NET Web服务从iPhone调用一个HTTP POST网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆