2014-02-13 68 views
0

我正在嘗試從objectiove-c調用Web服務。如何在NSURLConnection中爲Web服務調用設置Web代理信息

我的代碼是

-(BOOL)requestWebServiceURL:(NSURL *)URL withPostData:(NSString*)postData 
    { 
     NSLog(@"%d",[NSThread currentThread].isMainThread ? 1:0); 
     NSLog(@"*****requesting WebService URL***for class name ==** %@ ",self.callingClass); 
     NSLog(@"POstDataRecieved %@",postData); 
     NSData *dataToPost = [postData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 
     NSString *dataToPostLength = [NSString stringWithFormat:@"%ld", (unsigned long)[dataToPost length]]; 
     NSMutableURLRequest *r = [[[NSMutableURLRequest alloc] init]autorelease]; 
     [r setURL:URL]; 
     [r setHTTPMethod:@"POST"]; 
     [r setValue:dataToPostLength forHTTPHeaderField:@"Content-Length"]; 
     [r setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [r setHTTPBody:dataToPost]; 
     [r setTimeoutInterval:60.0]; 
     NSLog(@"\n\nSENDING data"); 
     self.receivedData = [[[NSMutableData alloc] init]autorelease]; 
     NSURLConnection *URLConnection =[[NSURLConnection alloc] initWithRequest:r delegate:self startImmediately:YES]; 
     NSLog(@"____>>>> %@",URLConnection); 
     NSLog(@"**____ %d",[NSThread currentThread].isMainThread ? 1:0); 
     return TRUE; 
    } 

如何設置此Web服務調用的代理服務器信息,即(服務器地址,端口,用戶名和密碼)。

謝謝

回答

0

NSURL類不會有這些方法。你需要使用CFURL。

您還可以使用委託方法didReceiveAuthenticationChallenge:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    if ([challenge previousFailureCount] == 0) { 
     NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"username" 
                    password:@"password" 
                   persistence:NSURLCredentialPersistenceForSession]; 
     [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; 
    } 
} 
+0

可以PLZ提供樣品或一些指針,在哪裏可以找到這些樣本 – singh