从 webservice URL 获取密码并通过该密码访问 [英] Get the password from the webservices URL and access through that password

查看:23
本文介绍了从 webservice URL 获取密码并通过该密码访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个附加了访问代码的 Web 服务 URL.我需要将访问代码发布到网络服务 URL 并获得 json 响应.我收到带有正确访问码和不正确访问码的 json 响应.我不明白问题出在哪里.我需要在输入错误密码时显示警报.

I have a Webservices URL which appends with accesscode. I need to post accesscode to a webservices URL and get json response. I am getting json response with correct accesscode and with incorrect accesscode too. I am not getting where the issue arising. I need to display alert when wrong password enters.

这是我的代码:

  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.

If I give wrong password, I am getting login failed, that's fine. when I correct password, it doesn't showing the content of that URL. We need to parse the request into URL.

推荐答案

In Your case

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

你不能通过URL in POST方法传递参数,例如,
....?accesscode=abcd&type=1"

You can not pass parameter with URL in POST method such like ,
....?accesscode=abcd&type=1"

您可以使用以下代码片段,如所述在本文中:

You can use the following code snippet, as described in this article:

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

Here, I simple describe how can use of POST method.

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

1. Set post string with actual username and password.

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

2. 使用 NSASCIIStringEncoding 对 post 字符串以及需要以 NSData 格式发送的 post 字符串进行编码.

2. Encode the post string using NSASCIIStringEncoding and also the post string you need to send in NSData format.

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

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

You need to send the actual length of your data. Calculate the length of the post string.

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

3. 创建一个带有所有属性的 Urlrequest,如 HTTP 方法、带有 post 字符串长度的 http 标头字段.创建 URLRequest对象并初始化它.

3. Create a Urlrequest with all the properties like HTTP method, http header field with length of the post string. Create URLRequest object and initialize it.

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

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

Set the Url for which your going to send the data to that request.

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

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

Now, set HTTP method (POST or GET). Write this lines as it is in your code.

[request setHTTPMethod:@"POST"];

设置 HTTP 头字段和 post 数据的长度.

Set HTTP header field with length of the post data.

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

还要设置 HTTP 标头字段的编码值.

Also set the Encoded value for HTTP header Field.

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

使用 postData 设置 urlrequest 的 HTTPBody.

Set the HTTPBody of the urlrequest with postData.

[request setHTTPBody:postData];

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

4. Now, create URLConnection object. Initialize it with the URLRequest.

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

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

It returns the initialized url connection and begins to load the data for the url request. You can check that whether you URL connection is done properly or not using just if/else statement as below.

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

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

5. To receive the data from the HTTP request , you can use the delegate methods provided by the URLConnection Class Reference. Delegate methods are as below.

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

上面的方法用于接收我们使用post得到的数据方法.

Above method is used to receive the data which we get using post method.

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

这个方法,可以用来接收错误报告,以防万一未连接到服务器.

This method , you can use to receive the error report in case of connection is not made to server.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

以上方法用于连接建立后的数据处理成功.

The above method is used to process the data after connection has made successfully.

另请参考 这个 这个 <POST 方法的强>文档.

Also Refer This and This documentation for POST method.

这里是 HTTPPost 方法.

这篇关于从 webservice URL 获取密码并通过该密码访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆