2013-03-13 52 views
2

我有一个附带有accesscode的Webservices URL。我需要将访问码发布到webservices url并获得json响应。我正在使用正确的访问码和错误的访问码来获取json响应。我没有得到问题出现的地方。我需要在错误的密码进入,这里是我的代码显示警报..从webservices url获取密码并通过该密码进行访问

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。你能帮助我如何解决这一问题?

回答

10

你的情况

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

你不可错过参数URL in POST方法,例如像,
....?accesscode=abcd&type=1"

你可以使用以下代码片段,如所述in this article

在这里,我简单介绍一下如何使用POST方法。

1.使用实际的用户名和密码设置过帐字符串。

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

2.编码使用NSASCIIStringEncoding后的字符串,你也需要在NSData的格式发送后的字符串。

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

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

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

3.与像HTTP方法,与柱的字符串的长度的HTTP报头字段中的所有属性创建的URLRequest。创建URLRequest 对象并初始化它。

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

使用postData设置urlrequest的HTTPBody。现在

[request setHTTPBody:postData]; 

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

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

它返回初始化URL连接,并开始装入数据 的URL请求。您可以检查是否URL连接 正确完成或不使用if/else声明如下。

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

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

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

以上方法用于接收我们开始使用后 方法的数据。

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

这种方法,可以用来接收 连接的情况下,错误报告是不是服务器发出。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

上述方法被用来处理所述数据连接已取得 后成功。

也参照ThisThis文档POST方法。

这里是HTTPPost Method.

+0

@patel的源代码最好的例子,我更新了我的代码,如你所说,我还是我的应用程序日志记录wrng密码。为了设置网址,我没有像你说的那样使用,因为我得到一个新的url作为json响应。我用你的代码更新了我的问题。你可以看到我的代码中的最后一条语句,我如何使用url。等待回复。 – 2013-03-13 06:34:32

+0

...?accesscode = abcdtype = 1您不能在Post方法中将它传递到我的答案中。 – iPatel 2013-03-13 10:26:25