从webservices网址获取密码并通过该密码访问 [英] Get the password from the webservices url and access through that password
问题描述
我有一个Webservices网址附加accesscode。我需要post accesscode到webservices的url,并得到json响应。我得到json响应与正确的accesscode和不正确的accesscode太。我得不到问题出现。我需要在错误的密码输入时显示提醒,这里是我的代码..
I have one 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];
如果我输错密码,我得到登录失败,当我正确的密码,它不显示该网址的内容。我们需要将请求解析为url。你能帮我解决问题吗?
IF I give wrong password, I am getting login failed, thats fine. when I correct password, It doesn't showing the content of that url. we need to parse the request into url. Can you help me how to solve the issue...
推荐答案
在你的情况下 >
In Your case
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://my example.com/Accountservice/Security/ValidateAccess?accesscode=abcd&type=1"]];
您不能通过 URL传递参数 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
您需要以NSData格式发送的帖子字符串。
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。创建一个包含 HTTP
方法,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];
设置要向其发送数据的网址。
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 em>)。
Now, set HTTP method (POST or GET). Write this lines as it is in your code.
[request setHTTPMethod:@"POST"];
设置 HTTP
[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.
And here is best example with source code of HTTPPost Method.
这篇关于从webservices网址获取密码并通过该密码访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!