【问题标题】:Invoking a http post URL from iphone using .net web service使用 .net 网络服务从 iphone 调用 http post URL
【发布时间】: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. ---&gt; Data at the root level is invalid. Line 1, position 1.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

请帮助我。!!!

谢谢, 世斌

【问题讨论】:

标签: .net iphone http post web-services


【解决方案1】:

感谢您的支持,我得到了它的工作。这是工作代码......

-(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];

}

【讨论】:

    【解决方案2】:

    您可以使用以下方式将 HTTP 方法设置为使用 POST 而不是默认的 GET:

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

    【讨论】:

    • 谢谢,但是如何传递参数...?
    • 上面“The MYYN”评论中提供的链接正是您所需要的。就像在普通 url 中一样创建一个包含参数的字符串,从中创建一个 NSData 对象并使用 setHTTPBody 将其提供给您的请求对象:
    • 嗯...代码对我来说看起来很正确。我想这更多的是你给你的网络服务的问题?
    • 不,Web 服务在 Windows 机器上运行良好。并且正确获取了用户数据。我不确定发生了什么..
    • 我对 WebSerivces 了解不多,但我只是在谷歌上搜索了一下,发现了以下文章。似乎你必须将参数包装到一个肥皂请求中(你得到一个肥皂错误,所以我假设你需要那个)......devx.com/wireless/Article/43209/0/page/3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多