Pregunta

The following code returns true for wifi connection but false while checking for cellular(wwan) network on device , here is the code

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;
}

i am getting the error as

The remote server returned an error: (403) Forbidden

help out.

¿Fue útil?

Solución

Check out Xamarin Reachability class here.

Edit:

Download and install the vodafone profile from http://db.tt/SqQGQ9Ci

Otros consejos

You can use Reachibility

#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;
    }
}

}

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top