以下代码对于wifi连接返回true,但在设备上检查蜂窝(wwan)网络时返回false 这是代码
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.Timeout = 25000;
request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.UseDefaultCredentials=true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response.StatusCode == HttpStatusCode.OK;
}
catch (Exception e)
{
return false;
}
我收到错误
远程服务器返回错误:(403)禁止
帮忙。
答案 0 :(得分:3)
答案 1 :(得分:0)
您可以使用可达性
#import "Reachability.h"
+(bool)internetConnection
{
Reachability* reachability;
reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
NetworkStatus netStatus = [reachability currentReachabilityStatus];
[reachability startNotifier];
switch (netStatus)
{
case NotReachable:
{
//[self showLoadingView:@"Internet Unavailable!!"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"NetworkFail" object:self];
return NO;
break;
}
case ReachableViaWWAN:
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"NEtworkPass" object:self];
return YES;
break;
}
case ReachableViaWiFi:
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"NEtworkPass" object:self];
return YES;
break;
}
}
}