【问题标题】:Get the password from the webservices URL and access through that password从 Web 服务 URL 获取密码并通过该密码访问
【发布时间】:2013-02-28 21:49:49
【问题描述】:

我有一个附加访问代码的 Web 服务 URL。我需要将访问代码发布到 Web 服务 URL 并获得 json 响应。我收到带有正确访问码和不正确访问码的 json 响应。我没有得到问题出现的地方。我需要在输入错误密码时显示警报。

这是我的代码:

  NSString *post =[[NSString alloc] initWithFormat:@"txtsecurecode=%@",[txtsecurecode text]];
        NSLog(@"PostData: %@",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:[NSString stringWithFormat:@"http://my example.com/Accountservice/Security/ValidateAccess?accesscode=abcdtype=1"]]];

        NSURL *url;

  // I need to parse it into url here .  

        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];
        NSURLConnection *conn= [[NSURLConnection alloc] initWithRequest:request delegate:self];
        if(conn)
        {
            NSLog(@"Connection Successful");
        }
        else
        {
            NSLog(@"Connection could not be made");
        }

    NSString *responseData = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:url] encoding:NSUTF8StringEncoding];

如果我输入错误的密码,我会登录失败,没关系。当我更正密码时,它不会显示该 URL 的内容。我们需要将请求解析为 URL。

【问题讨论】:

    标签: iphone ios objective-c cocoa


    【解决方案1】:

    你的情况

     NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://my example.com/Accountservice/Security/ValidateAccess?accesscode=abcd&type=1"]];
    

    您不能使用URL in POST 方法传递参数,例如,
    ....?accesscode=abcd&type=1"

    您可以使用以下代码sn-p,如in this article所述:

    在这里,我简单描述一下如何使用 POST 方法。

    1.用实际的用户名和密码设置帖子字符串。

    NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username",@"password"];
    

    2. 使用NSASCIIStringEncoding 编码帖子字符串,以及您需要以NSData 格式发送的帖子字符串。

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

    您需要发送数据的实际长度。计算长度 的帖子字符串。

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

    3. 创建一个包含所有属性的 Urlrequest,例如 HTTP 方法、带有帖子字符串长度的 http 标头字段。创建URLRequest 对象并对其进行初始化。

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    

    设置要向该请求发送数据的 URL。

    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abcde.com/xyz/login.aspx"]]];
    

    现在,设置 HTTP 方法(POST 或 GET)。按原样写下这些行 你的代码。

    [request setHTTPMethod:@"POST"];
    

    设置HTTP头域为post数据的长度。

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    

    同时设置 HTTP 标头字段的编码值。

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
    

    用postData设置urlrequest的HTTPBody

    [request setHTTPBody:postData];
    

    4. 现在,创建 URLConnection 对象。使用 URLRequest 对其进行初始化。

    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    

    返回初始化的url连接,开始加载数据 对于 url 请求。您可以检查您是否URL 连接 仅使用 if/else 语句是否正确完成,如下所示。

    if(conn)
    {
    NSLog(@”Connection Successful”)
    }
    else
    {
    NSLog(@”Connection could not be made”);
    }
    

    5. 要接收来自 HTTP 请求的数据,您可以使用 URLConnection 类参考提供的委托方法。 委托方法如下。

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

    上述方法用于接收我们使用 post 获取的数据 方法。

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

    这个方法,你可以用来接收错误报告,以防万一 未与服务器建立连接。

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    

    上述方法用于处理连接后的数据 成功。

    另请参阅 This This 文档了解POST 方法。

    这是最好的例子,源代码为HTTPPost Method.

    【讨论】:

    • @patel,我按照你的建议更新了我的代码,但我的应用程序仍然在记录 wrng 密码。要设置 url,我没有像你说的那样使用,因为我得到了一个新的 url 作为 json 响应。我用你的代码更新了我的问题。你可以在我的代码中看到最后一条语句,我是如何使用 url 的。等待回复。
    • ...?accesscode=abcdtype=1 你不能在我输入答案的行中通过 Post 方法检查。
    猜你喜欢
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 2010-12-18
    • 1970-01-01
    相关资源
    最近更新 更多