我正在尝试在服务器上发布数据,但它无法从服务器端给出任何响应,请检查我的代码。
不为此网址提供任何值NSString * urlLoc = @“http://www.vallesoft.com/vlcc_api/bookappointment.php”;
NSString *requestString = [NSString stringWithFormat:@"name=%@&mobileno=%@&emailid=%@&dob=%@&password=%@&gender=%@&ref=%@&fbflag=0",goSignUpName,goSignUpMobile,goSignUpEmailId,goSignUpDOB,goSignUpPassword,goSignUpGender,goSignUpReferenceId] ;
NSData *postData = [requestString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlLoc]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
if(data.length>0 && connectionError==nil)
{
responseData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"LOGIN Response ==> %@", responseData);
if ([responseData intValue] == 1)
{
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Thanks" message:@"Registered" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
for(id controller in [self.navigationController viewControllers])
{
if([controller isKindOfClass : [ViewController class]]){ [self.navigationController popToViewController: controller animated: YES];
break;
}
}
}
else if ([responseData intValue] == 2)
{
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Already Reegistered" message:@"Please choose other Details" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
else if (responseData==NULL)
{
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Not Book Try Again!!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
}];
}
答案 0 :(得分:0)
我认为,问题在于您的服务器代码。从任何来源发送请求时,它不会给出任何响应。您可以使用Postman(Google Chrome插件)进行验证
更新
好的,服务器正在等待GET请求,但您正在发出POST请求
请使用以下方法。
-(void)sendToServerHandler:(NSString*)serviceURL showLoader:(BOOL)showLoader completionBlock:(void (^)(BOOL success, NSDictionary *responseData))completionBlock
{
NSURL *url = [[NSURL alloc] initWithString:serviceURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:600.0];
[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *connectionError)
{
dispatch_async(dispatch_get_main_queue(),
^{
if(showLoader)
{
[appDelegate hideLoaderInstantaneously];
}
// handle response
if (connectionError)
{
completionBlock(false, nil);
return;
}
else
{
NSDictionary *parsedDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if(parsedDictionary==nil)
{
completionBlock(false, nil);
return;
}
completionBlock(true, parsedDictionary);
}
});
}];
}
它应该给你适当的回应
使用
调用它NSString *URL = @"http://www.vallesoft.com/vlcc_api/registration.php?name=qq&mobileno=123456789&emailid=hk@gmail&dob=2015-10-26&password=234&gender=MALE&ref=02&fbflag=0";
[self sendToServerHandler:URL showLoader:showLoader completionBlock:^(BOOL success, NSDictionary *responseData)
{
NSLog(@"response data --> %@", reponse data);// RESPONSE FROM SERVER
}];
更新
当您使用默认情况下被iOS 9 App Transport Security阻止的http连接时。我希望你会为你的URL添加一个例外。如果没有,下面是解释的方式。
如果您尝试在启用ATS的情况下发出HTTP请求(使用NSURLSession或像AFNetworking这样的库),您会看到如下错误: 错误域= NSURLErrorDomain代码= -1004“无法连接到服务器”。 UserInfo = 0x12ed5bad0 {NSUnderlyingError = 0x12ee495b0“操作无法完成。(kCFErrorDomainCFNetwork error -1004。)” 或这个: NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9802) 或这个: App Transport Security已阻止明文HTTP(http://)资源加载,因为它不安全。可以通过应用程序的Info.plist文件配置临时例外。
以下是完全禁用ATS的方法。打开Info.plist,并添加以下行:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
点击链接了解更多详情: Enable http connection in iOS 9
答案 1 :(得分:0)
从iOS上发送HTTP呼叫的角度来看,您发布的代码是正常的。 但它有一个可能的问题:您正在创建“requestString”参数作为GET,但您将其作为POST正文发送。你确定要这么做吗?
除此之外似乎没什么问题。
尝试将“urlLoc”设置为虚拟值;例如“http://www.google.com”,您将看到在异步回调中出现错误。