2016-10-08 4 views
0

次のコードを使用して、URLConfigurationにプロキシ設定を設定しています。NSURLSessionConfigurationが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]; 

しかし、その代わりにURLのデータを返すのHTMLデータと私のproxyHostの結果を返します^(NSData *data, NSURLResponse *response, NSError *error)のコード行(http://example.com/sampleid?id=506は)

誰も私がこの問題を解決するのに役立ちます。

ありがとうございます。

答えて

0
sessionConfig.connectionProxyDictionary = proxyDict; 

あなたの問題を解決するかどうかを試してみてください。あなたの応答から私の変更を私のスレッドにプロキシのコメントは、実際には、プロキシを介して接続したいと思わないように思われる。また

proxyHost
proxyPortの

+0

の値は、私はあなたが言った行をコメントした場合、レスポンスは罰金がどうなるかです。しかし、その行がなければ、プロキシ設定をURLSessionConfigurationに設定することはできません。そして、私は 'proxyHost'と' proxyPort'の値で質問を更新しました –

+0

ur proxyはローカルIPであり、実際に問題があるかもしれないサーバーではありません。 google freeプロキシを試して、ur proxyHostとproxyPortを別のものに変更してみてください。 – Charlie

関連する問題