Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

iphone - Get the password from the webservices URL and access through that password

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.

Here is my code:

  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];

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In Your case

 NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://my example.com/Accountservice/Security/ValidateAccess?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:

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

1. Set post string with actual username and password.

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

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. 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];

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"]]];

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

[request setHTTPMethod:@"POST"];

Set HTTP header field with length of the post data.

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

Also set the Encoded value for HTTP header Field.

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

Set the HTTPBody of the urlrequest with postData.

[request setHTTPBody:postData];

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

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

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. 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

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.

Also Refer This and This documentation for POST method.

And here is best example with source code of HTTPPost Method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...