0

我正在使用以下代码在 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];

但是这行代码^(NSData *data, NSURLResponse *response, NSError *error)返回的是我的结果,proxyHost而不是返回URLhttp://example.com/sampleid?id=506)的数据

谁能帮我解决这个问题。

提前致谢。

4

1 回答 1

0
sessionConfig.connectionProxyDictionary = proxyDict;

尝试将其注释掉,看看它是否能解决您的问题。从您对我的更改的回复来看,我的线程上的代理评论似乎您实际上并不想通过代理进行连接。还有什么价值

proxyHost 代理
端口

于 2016-10-11T15:27:45.857 回答