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

标签 .net iphone http post web-services

当我点击一个按钮时,我需要从我的 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. ---&gt; Data at the root level is invalid. Line 1, position 1.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

请帮助我。!!!

谢谢, 世斌

最佳答案

感谢您的支持,我开始工作了。这是工作代码...

-(IBAction)loginButtonPressed:(id)sender{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(loginID.text);
NSLog(loginPassword.text);
NSString *soapMsg = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><IphoneAuthenticateduser xmlns=\"http://tempuri.org/\"><UserName>%@</UserName><Passwordtext>%@</Passwordtext></IphoneAuthenticateduser></soap:Body></soap:Envelope>",loginID.text,loginPassword.text];
NSLog(soapMsg);
NSURL *url = [NSURL URLWithString:@"http://192.168.50.194:9989/Iphoneauthentication.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];

[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/IphoneAuthenticateduser" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];

if(!response){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error"
                                                    message:@"Failed to Connect to the Internet"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}
else{
    xmlResult=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:urlData];
    XMLParser *parser = [[XMLParser alloc] initXMLParser];
    [xmlParser setDelegate:parser];
    sucess = [xmlParser parse];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    if(sucess){
        NSLog(appDelegate.message);
        if(appDelegate.message != NULL){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Failed"
                                                            message:appDelegate.message
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
            appDelegate.message = NULL;
        }
        else{
            UserViewController *userController = [[UserViewController alloc] initWithNibName:@"UserViewController"
                                                                                  bundle:nil];
            [self presentModalViewController:userController animated:YES];
            [userController release];
        }
    }
    else{
        NSLog(@"Parsing Failed");
    }
}
[pool release];

关于.net - 使用 .net Web 服务从 iphone 调用 http post URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1945479/

相关文章:

javascript - 如何在 AngularJS 中正确使用 HTTP.GET?具体来说,对于外部 API 调用?

c# - 如何设置所有者绘制工具提示的位置?

c# - 允许双击标题栏以最大化对话框但没有最大最小按钮

iphone - 如何以编程方式或强制[无需用户交互]调用 UIImagePickerController Delegate?

iphone - 禁用 UIView 的自动旋转

javascript - 隐藏字段是否总是提交项目列表中的第一个值?

.net - 当我恢复我的项目 NETSDK1139 时出现 Dotnet maui 错误

c# - 关于使用 Monitor.TryEnter 和锁定对象的问题

iphone - 钥匙串(keychain)组访问以在我现有的应用程序之间共享数据

javascript - 在 Angular $http 调用中使用 then/catch