Ios 目标c HTTPS登录。发布并获取正确的url状态代码
我在从浏览器返回200状态代码时遇到问题。我试图发布一个带有用户名和密码的url到一个url,并得到一个xml响应 0=错误的用户名/密码 1=成功登录 正确的url是: url不正确,但仍接收到200状态代码: xxx.com 我认为问题在于url没有正确构建 即使登录是错误的,我应该得到一个200状态码返回,但它只返回404 谢谢你的帮助Ios 目标c HTTPS登录。发布并获取正确的url状态代码,ios,iphone,objective-c,url,xcode5,Ios,Iphone,Objective C,Url,Xcode5,我在从浏览器返回200状态代码时遇到问题。我试图发布一个带有用户名和密码的url到一个url,并得到一个xml响应 0=错误的用户名/密码 1=成功登录 正确的url是: url不正确,但仍接收到200状态代码: xxx.com 我认为问题在于url没有正确构建 即使登录是错误的,我应该得到一个200状态码返回,但它只返回404 谢谢你的帮助 - (IBAction)sigininClicked:(id)sender { NSInteger success = 0; @try { i
- (IBAction)sigininClicked:(id)sender {
NSInteger success = 0;
@try {
if([[self.txtUsername text] isEqualToString:@""] || [[self.txtPassword text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0];
} else {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@/%@",
@"https://xxx.testauthenticate",
self.txtUsername.text,
self.txtPassword.text]];
NSString *post =[[NSString alloc] initWithFormat:@"%@", url];
NSLog(@"PostData: %@",post);
//NSLog(@"url: %@",url);
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
} else {
if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0];
}
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Sign in Failed." :@"Error!" :0];
}
if (success) {
[self performSegueWithIdentifier:@"login_success" sender:self];
}
}
-(iAction)sigininClicked:(id)发送方{
NSInteger成功率=0;
@试一试{
if([[self.txtextername text]IseQualtString:@”“][[self.txtPassword text]IseQualtString:@”“])){
[自我提醒状态:@“请输入电子邮件和密码”:@“登录失败!”:0];
}否则{
NSURL*url=[NSURL URLWithString:[NSString stringWithFormat:@“%@/%@/%@”,
@"https://xxx.testauthenticate",
self.txtUsername.text,
self.txtPassword.text]];
NSString*post=[[NSString alloc]initWithFormat:@“%@”,url];
NSLog(@“PostData:%@”,post);
//NSLog(@“url:%@”,url);
NSData*postData=[post-dataUsingEncoding:NSUTF8StringEncoding];
NSString*postLength=[NSString stringWithFormat:@“%lu”,(无符号长)[postData长度]];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];
[请求设置url:url];
[请求设置HttpMethod:@“POST”];
[请求设置值:HttpHeaderField的postLength:@“内容长度”];
[请求设置值:@“应用程序/xml”用于HttpHeaderField:@“接受”];
[请求设置值:@“应用程序/x-www-form-urlencoded”forHTTPHeaderField:@“内容类型”];
[请求setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHttpSCCertificate:YES for主机:[url主机]];
NSError*error=[[NSError alloc]init];
NSHTTPURLResponse*响应=nil;
NSData*urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&响应错误:&错误];
NSLog(@“响应代码:%ld”,(长)[响应状态代码]);
如果([response statusCode]>=200&&[response statusCode]<300)
{
NSString*responseData=[[NSString alloc]initWithData:urlData编码:NSUTF8StringEncoding];
NSLog(@“响应==>%@”,响应数据);
}否则{
if(error)NSLog(@“error:%@”,error);
[自我提醒状态:@“连接失败”:@“登录失败!”:0];
}
}
}
@捕获(N例外*e){
NSLog(@“异常:%@”,e);
[自我提醒状态:@“登录失败”。:@“错误!”:0];
}
如果(成功){
[self-PerformsgueWithIdentifier:@“登录成功”发件人:self];
}
}
状态代码404
表示请求的端点不存在。正如我看到的,您使用link
https://apix.pointstreak.com/performance/index.php/testauthenticate
这是无效的。尝试在浏览器中打开它,您将看到错误消息404页面未找到
设置正确的url,它就会工作 更新:
尝试将url字符串更改为:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@/%@",
@"https://apix.pointstreak.com/performance/index.php/testauthenticate",
self.txtUsername.text,
self.txtPassword.text]];
用户名和密码未放在url的末尾。或者是做得不正确。我想这就是问题所在problem@user3582441在您的示例中,您将用户名和密码设置为请求正文,而不是请求url。url看起来是正确的,但现在获得405状态代码。方法不允许如果你找到了问题的解决方案,请将其作为答案发布。将其添加到原始问题中会使其他答案更难理解,并且可能会使浏览者觉得该问题没有答案。经过几天的等待,你可以接受自己的答案来结束这个问题。干杯