0

I am using the following code to set proxy setting in URLConfiguration.

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

But this line of code ^(NSData *data, NSURLResponse *response, NSError *error) returns the results of my proxyHost with HTML data instead of returning the data of URL(http://example.com/sampleid?id=506)

Can anyone help me to solve this.

Thanks in advance.

Balaji Kondalrayal
  • 1,743
  • 3
  • 19
  • 38

1 Answers1

0
sessionConfig.connectionProxyDictionary = proxyDict;

Try commenting that out and see if it solves your problem. From your response to my change the proxy comment on my thread it seems as if you dont actually want to connect through a proxy. Also what is the value of

proxyHost
proxyPort

Charlie
  • 222
  • 3
  • 20
  • If I comment the line you said, then the response will be fine. But without that line you cannot set the proxy setting to URLSessionConfiguration. And I updated my question with `proxyHost` and `proxyPort` values – Balaji Kondalrayal Oct 12 '16 at 09:33
  • It looks like ur proxy is a local IP and not accually a server which is why it might have problems. Try google free proxies and try to change ur proxyHost and proxyPort to a different one. – Charlie Oct 13 '16 at 16:17