J'utilise le code suivant pour définir le paramètre proxy dans URLConfiguration.NSURLSessionConfiguration ne renvoyant pas les données URLRequest
NSString *proxyHost = @"192.168.xx.xxx";
NSNumber *proxyPort = [NSNumber numberWithInteger:8090];
NSString *request = [NSString stringWithFormat:
@"http://example.com/sampleid?id=506"];
NSURL *URL = [NSURL URLWithString:
[request stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:[request stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *URLrequest = [NSURLRequest requestWithURL:url];
NSDictionary *proxyDict = @{
@"HTTPEnable" : [NSNumber numberWithInt:1],
(NSString *)kCFStreamPropertyHTTPProxyHost : proxyHost,
(NSString *)kCFStreamPropertyHTTPProxyPort : proxyPort,
(NSString *)kCFProxyUsernameKey : hostUserName,
(NSString *)kCFProxyPasswordKey : hostPassword,
@"HTTPSEnable" : [NSNumber numberWithInt:1],
(NSString *)kCFStreamPropertyHTTPSProxyHost : proxyHost,
(NSString *)kCFStreamPropertyHTTPSProxyPort : proxyPort,
(NSString *)kCFProxyUsernameKey : hostUserName,
(NSString *)kCFProxyPasswordKey : hostPassword,
};
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.connectionProxyDictionary = proxyDict;
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: sessionConfig delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
__block NSDictionary *returnJsonDictionary;
__block NSError *error1;
NSURLSessionDataTask *task = [defaultSession dataTaskWithRequest:URLrequest completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"NSURLSession got the response [%@]", response);
NSLog(@"NSURLSession got the data [%@]", data);
}];
[task resume];
Mais cette ligne de code ^(NSData *data, NSURLResponse *response, NSError *error)
renvoie les résultats de mon proxyHost
avec des données HTML au lieu de renvoyer les données de URL
(http://example.com/sampleid?id=506)
Quelqu'un peut me aider à résoudre ce problème.
Merci d'avance.
Si je commente la ligne que vous avez dit, la réponse sera bien. Mais sans cette ligne, vous ne pouvez pas définir le paramètre proxy sur URLSessionConfiguration. Et j'ai mis à jour ma question avec les valeurs de 'proxyHost' et' proxyPort' –
Il semble que votre proxy est une adresse IP locale et non un serveur, ce qui explique pourquoi il pourrait avoir des problèmes. Essayez google proxies gratuit et essayez de changer votre proxyHost et proxyPort à un autre. – Charlie