从webservicesurl获取密码并通过该密码进行访问
我有一个Web服务URL附加访问码。 我需要将访问码发布到一个web服务url并获得json响应。 我得到正确的访问代码和不正确的访问代码的JSON响应。 我没有得到问题出现的地方。 当input错误的密码时,我需要显示警报,这里是我的代码。
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];
如果我给密码错误,我login失败,这很好。 当我更正密码,它不显示该url的内容。 我们需要将请求parsing成url。 你能帮我解决这个问题吗
在你的情况
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://my example.com/Accountservice/Security/ValidateAccess?accesscode=abcd&type=1"]];
URL in POST
方法中,你不能通过URL in POST
传递参数 ,
....?accesscode=abcd&type=1"
您可以使用以下代码片段,如本文所述 :
在这里,我简单介绍一下如何使用POST方法。
1.用实际的用户名和密码设置后缀string。
NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username",@"password"];
2.使用
NSASCIIStringEncoding
编码NSASCIIStringEncoding
string,以及需要以NSData格式发送的NSASCIIStringEncoding
string。NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
你需要发送你的数据的实际长度。 计算后期string的长度。
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
3.创build一个Urlrequest,使用所有属性,如
HTTP
方法,带有后缀string长度的http头字段。 创buildURLRequest
对象并初始化它。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
标题字段。[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
同时设置HTTP头字段的编码值。
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
使用
HTTPBody
设置HTTPBody
的HTTPBody。[request setHTTPBody:postData];
4.现在,创buildURLConnection对象。 使用URLRequest初始化它。
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
它返回初始化的url连接,并开始加载url请求的数据。 你可以使用下面的if / else语句来检查你的
URL
连接是否正确完成。if(conn) { NSLog(@”Connection Successful”) } else { NSLog(@”Connection could not be made”); }
5.要从HTTP请求接收数据,可以使用URLConnection Class Reference提供的委托方法。 代表方法如下。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
以上方法用于接收我们使用post方法得到的数据。
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
这种方法,你可以使用接收到错误报告,如果没有连接到服务器。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
上述方法用于连接成功后处理数据。
另请参阅 此 文档和POST
文档 。
这里是HTTPPost方法源代码的最好例子。