使用带网关保护的JSON Webservice时,在IOS中没有响应
下面的代码是简单的登录应用程序。我使用Post请求传递用户名和密码以获取详细信息。但我从web服务得到@“”响应使用带网关保护的JSON Webservice时,在IOS中没有响应,ios,iphone,objective-c,json,web-services,Ios,Iphone,Objective C,Json,Web Services,下面的代码是简单的登录应用程序。我使用Post请求传递用户名和密码以获取详细信息。但我从web服务得到@“”响应 email= emailTextField.text; password=passwordTextfield.text; NSString *post = [[NSString alloc] initWithFormat:@"UserName=%@&Password=%@",email,password]; NSData *postData = [post dataUsi
email= emailTextField.text;
password=passwordTextfield.text;
NSString *post = [[NSString alloc] initWithFormat:@"UserName=%@&Password=%@",email,password];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"http:Gateway/api/json"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];
[theRequest setHTTPBody:postData];
NSURLResponse *response;// = [[NSURLResponse alloc] init];
NSError *error;// = [[NSError alloc] init;
NSData *urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Login response: is %@",str);
要向服务器提供身份验证凭据,请使用NSURLConnection委托
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *credential;
credential = [NSURLCredential credentialWithUser:userName password:password persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
查看提供身份验证的方法。我应该调用哪种方法来传递用户名和密码凭据?NSLog(@“errot:%@”,error);这是获取操作无法完成的错误。(NSURErrorDomain错误-1012。)“用户信息=0xa219390{NSErrorFailingURLKey=http://1,NSERRORFAILINGURSTRINGKEY=http:///api/json/reply/Customer/?,NSUnderlyingError=0x8f3e3c0“该操作无法完成。(KCFerrorDomainCFN网络错误-1012)。”