从webservices url获取密码并通过该密码访问

kum*_*eer 2 iphone cocoa objective-c ios

我有一个附加了访问代码的Webservices URL.我需要将访问代码发布到webservices 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];
Run Code Online (Sandbox Code Playgroud)

如果我给错了密码,我登录失败,没关系.当我更正密码时,它不会显示该网址的内容.我们需要将请求解析为url.你能帮我解决一下这个问题吗?

iPa*_*tel 10

在你的情况下

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

你不能用像这样的方法传递参数URL in POST,
....?accesscode=abcd&type=1"

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

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

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

NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username",@"password"];
Run Code Online (Sandbox Code Playgroud)

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

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
Run Code Online (Sandbox Code Playgroud)

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

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
Run Code Online (Sandbox Code Playgroud)

3.创建一个Urlrequest,其中包含所有属性,例如HTTP方法,http标题字段和post字符串的长度.创建URLRequest 对象并初始化它.

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
Run Code Online (Sandbox Code Playgroud)

设置要将数据发送到该请求的URL.

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abcde.com/xyz/login.aspx"]]];
Run Code Online (Sandbox Code Playgroud)

现在,设置HTTP方法(POST或GET).在代码中写下这一行.

[request setHTTPMethod:@"POST"];
Run Code Online (Sandbox Code Playgroud)

HTTP使用发布数据的长度设置标题字段.

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
Run Code Online (Sandbox Code Playgroud)

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

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
Run Code Online (Sandbox Code Playgroud)

HTTPBody使用postData 设置urlrequest.

[request setHTTPBody:postData];
Run Code Online (Sandbox Code Playgroud)

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

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
Run Code Online (Sandbox Code Playgroud)

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

if(conn)
{
NSLog(@”Connection Successful”)
}
else
{
NSLog(@”Connection could not be made”);
}
Run Code Online (Sandbox Code Playgroud)

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

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
Run Code Online (Sandbox Code Playgroud)

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
Run Code Online (Sandbox Code Playgroud)

此方法,您可以使用在未连接到服务器的情况下接收错误报告.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
Run Code Online (Sandbox Code Playgroud)

上述方法用于在连接成功后处理数据.

有关方法,请参阅 文档 文档POST.

这是HTTPPost方法源代码的最佳示例.