基本认证和3G上的NSURLConnection

时间:2014-01-15 16:41:55

标签: ios json wifi nsurlconnection 3g

我目前正在开发一款iOS应用程序,它需要基本HTTP身份验证来访问网络服务。 我使用iOS模拟器和公司的WiFi网络开发了网络部分。

我的一些同事通过3G网络使用自己的iPad试用了我的应用程序。看来我管理连接所做的工作不正常......我无法解释为什么我的所有工作都不能与3 / 4G连接一起工作。

我试着看看 connectionDidFinishLoading 方法中发生了什么。

  • 使用WiFi,我点击的网址会返回一个正确的JSON文件。
  • 使用3G,我有一个甜蜜的(null)

好的,让我们看看 didReceiveResponse 方法中发生了什么......

借助WiFi,我明白了:

NSHTTPURLResponse: 0x95c7130 { URL: URL_giving_json} { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Type" = "application/json";
    Date = "Wed, 15 Jan 2014 15:33:05 GMT";
    "Keep-Alive" = "timeout=15";
    Server = "JAS BHV3";
    "Set-Cookie" = "SMSESSION=cookie; Path=/appname/; HttpOnly, ASID=.a1s1; path=/";
    "Transfer-Encoding" = Identity; } }

通过3G,我明白了:

NSHTTPURLResponse: 0x97d8530 { URL: Connection_URL&TARGET=URL_giving_json} { status code: 200, headers {
    "Cache-Control" = "no-store";
    Connection = "Keep-Alive";
    "Content-Encoding" = gzip;
    "Content-Length" = 3296;
    "Content-Type" = "text/html; charset=iso-8859-1";
    Date = "Wed, 15 Jan 2014 15:28:05 GMT";
    "Keep-Alive" = "timeout=15";
    Server = Apache;
    Vary = "Accept-Encoding";
} }

这是我试过的:

- (void)hitURLWithURLString:(NSString *)URLToHit
{
    NSURL *url = [NSURL URLWithString:URLToHit];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:30.0];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
}

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge previousFailureCount] == 0) {
        // 1 - Received authentication challenge
        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:self.login
                                                                    password:self.password
                                                                 persistence:NSURLCredentialPersistenceForSession];
        // 2 - Credential created
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
        // 3 - Responded to authentication challenge
    }
    else
    {
        // 1 - Previous authentication failure
        [self.delegate authenticationDidFail:self];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [self.delegate didFinishLoading:self fulldata:self.receivedData];
    NSError *error;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:self.receivedData options:kNilOptions error:&error];
    NSLog(@"--->Finished : %@", [dictionary description]); // gives (null) here
    connection = nil;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Connection failed ! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    [self.delegate didFailWithError:self error:error];
}

有谁知道这个问题?为什么我不能通过3G连接设备获得我的JSON?

非常感谢你的帮助。

请原谅我的英语,我是法国人......:)

0 个答案:

没有答案