Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS的url中检查发布用户名和密码是否有效_Ios_Objective C_Json_Post_Get - Fatal编程技术网

如何在iOS的url中检查发布用户名和密码是否有效

如何在iOS的url中检查发布用户名和密码是否有效,ios,objective-c,json,post,get,Ios,Objective C,Json,Post,Get,我是这个领域的新手。我需要检查用户名和密码是否有效 我尝试的是我有一个本地URL,用户名是23,密码是rama,所以当我将该名称传递给URL时,点击该URL后,我们会在浏览器中显示“用户名有效” 但是现在我需要在两个文本字段中输入用户名和密码,然后单击提交按钮显示它是否有效 我跟着走了,但运气不好。请告诉我任何想法 我试过这样,但没有任何运气: username = @"23"; password = @"rama"; NSString *postString = [NSString strin

我是这个领域的新手。我需要检查用户名和密码是否有效

我尝试的是我有一个本地URL,用户名是
23
,密码是
rama
,所以当我将该名称传递给URL时,点击该URL后,我们会在浏览器中显示“用户名有效”

但是现在我需要在两个
文本字段中输入
用户名
密码
,然后单击提交按钮显示它是否有效

我跟着走了,但运气不好。请告诉我任何想法

我试过这样,但没有任何运气:

username = @"23";
password = @"rama";
NSString *postString = [NSString stringWithFormat:@"userid=%@&password=%@",username, password];
NSLog(@"%@", postString);
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://192.168.3.196:8090/SaveDollar/rest/users/findByUser/userid/password"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
提前谢谢

  • 使用
    nsurlconnectionelegate创建一个类。
  • 添加此方法:

    - (void)connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response  
     {  
          [_data setLength:0];  
          NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
          NSInteger code = [httpResponse statusCode];
          if (code >= 200 && code <= 299 ) {
              NSLog(@"~~~~~ Request Maker Success! Code: %ld", (long)code);
          } else {
              NSLog(@"~~~~~ Request Maker Error! Code: %ld \n URL: %@", (long)code, _urlSent);
          }
     }
    
  • 更新。

    您将需要一些web服务。我用
    rama
    23
    尝试了你的URL,但没有响应


    假设您的服务正常。如果密码和名称匹配,它将返回代码
    200。
    您可以在浏览器web inspector中检查代码。无需解析页面中的web内容。您的Objective-C代码将侦听服务器的任何响应。如果
    code>=200,并且我没有弄错,那么您是在检查某个远程数据库中的用户名和密码吗?或者让我重新表述一下:输入的数据是否有效,是否来自请求的URL?@Piotr YES来自URL,所以逻辑应该在URL端。使用您提供的指南。在URL端,您可以通过两个字段访问POST as表格:用户名和密码。检查它们是否匹配并返回答案。@Piotr如果我不是ObjC程序员,请锁定我的代码。我不知道这是否相关,但变量名不匹配-username1、password1,我不确定问题出在哪里。服务器端还是客户端?感谢重播,但用户名和密码附加到Url,但请告诉我如何检查有效用户或我的web服务是否有效用户和密码返回是无效返回NO@PavanAlapati我更新了我的答案。不要犹豫,多问一些问题。祝你好运
    
    NSString *someurl = @"http://%@:%@/rest?_name=%@&_pwd=%@";
    
    NSString *urlString = [NSString stringWithFormat:someurl, @"address", @"port", @"username", @"password"];
    NSString *urlStringFixed = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStringFixed];
    NSLog(@"URL: %@", url);
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    [theRequest addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest setHTTPMethod:@"GET"];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    
    if (connection){
        // Connection exist
    }