简体   繁体   English

来自iPhone中服务器数据库的验证用户名和密码

[英]validation username and password from server database in iphone

hi friends i am doing here validation for username and password from server database by given url of server i think i am going wrong i don't think my code is right please some help me how to validation from server database for username and password in iphone 嗨,朋友们,我在这里通过服务器的给定URL从服务器数据库验证用户名和密码,我认为我做错了,我认为我的代码不正确,请一些帮助我如何从iPhone的服务器数据库验证用户名和密码

-(IBAction)buttonClick:(id)sender
{

NSString* username = nameInput.text;
NSString* pass = passInput.text;

if([nameInput.text isEqualToString:@"" ]&& [passInput.text isEqualToString:@""]) 
{

    //greeting.text = @"Input Your Value";
    //[nameInput resignFirstResponder];
    //[passInput resignFirstResponder];
    //return;
}

NSString *post = 
[[NSString alloc] initWithFormat:@"uname=%@ & pwd=%@",username,pass];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];  

NSURL *url = [NSURL URLWithString:@"http://server:85/VirtualTerminal/RecordAttendance.aspx"];//@"https://www.google.com/accounts/ServiceLogin?service=reader&passive=1209600&continue=http://www.google.co.in/reader/?hl%3Den%26tab%3Dwy&followup=http://www.google.co.in/reader/?hl%3Den%26tab%3Dwy&hl=en"];   //@"http://www.chakrainteractive.com/mob/iphone/login/chckusr.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];  
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
[theRequest setHTTPBody:postData];  


NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )
{
    webData = [[NSMutableData data] retain];
    //test *t=[[test alloc]initWithNibName:@"test" bundle:nil];
    //[self presentModalViewController:t animated:YES];
    //[t release];

}
else
{

}
//}
[nameInput resignFirstResponder];
[passInput resignFirstResponder];
nameInput.text = nil;
passInput.text = nil;
k
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"loginStatus");
greeting.text = loginStatus;
[loginStatus release];



[connection release];
[webData release];
}

Try setting with the following HTTPHeaderFields . 尝试使用以下HTTPHeaderFields进行设置。

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];     
[request setValue:@"UTF-8" forHTTPHeaderField:@"charset"];      
[request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];

I believe that the login call may be a "GET" method instead of "POST". 我相信登录调用可能是“ GET”方法而不是“ POST”。

Once again confirm with the server URL that you make the call. 再次使用服务器URL确认您进行了呼叫。

Thanks. 谢谢。

You can use this code: 您可以使用以下代码:

-(IBAction)btnclick1:(id)sender

{

    recordResult=FALSE;


    greeting.text=@"Processing";


    NSString *soapMessage=[NSString stringWithFormat:@" WEB SERVICE ",nameInput.text,passInput.text];


    NSLog(@"%@",soapMessage);


NSURL *url=[NSURL URLWithString:@"http://SOME URL.asmx"];

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];

    NSString *msgLength=[NSString stringWithFormat:@"%d",[soapMessage length]];




    [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [theRequest addValue:@"http://tempuri.org/webservicename" forHTTPHeaderField:@"SOAPAction"];

    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];

    [theRequest setHTTPMethod:@"POST"];

    [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self];

    [myActivity startAnimating];

    if(theConnection)

    {

        webData=[[NSMutableData data]retain];

    }

    else

    {

        NSLog(@"theConnection is NULL");

    }


        [nameInpt resignFirstResponder];

        [passinpt resignFirstResponder];
}
-----

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    [webData setLength: 0];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [webData appendData:data];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    NSLog(@"ERROR with theConenction");

    [connection release];

    [webData release];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSLog(@"DONE.Received Bytes:%d",[webData length]);

    NSString *theXML =[[NSString alloc]initWithBytes:[webData mutableBytes]length:

[webData length] encoding:NSUTF8StringEncoding];

    NSLog(@"%@",theXML);

    [theXML release];

    if(xmlParser)
    {

        [xmlParser release];
        //xmlParser=nil;
    }

    xmlParser=[[NSXMLParser alloc]initWithData:webData];

    [xmlParser setDelegate:self];

    [xmlParser setShouldResolveExternalEntities:YES];

    [xmlParser parse];

    [myActivity stopAnimating];

    [webData release];
     //webData = nil; 

    [connection release];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName


{
    if([myTxt1.text isEqualToString:@"webservresult"])

{

        if(!soapResult)

        {

            soapResult=[[NSMutableString alloc]init];

        }

        recordResult=TRUE;

    }

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

    if(recordResult)

    {

        [soapResult appendString:string];

    }

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

{

    if([myTxt1.text isEqualToString:@"wsresult"])

    {

        recordResult=FALSE;     

        greeting.text=[NSString stringWithFormat:@"webservicename Result is:%@",soapResult];

        [soapResult release];

        soapResult=nil;

    }
}

that's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM