2011-09-22 64 views
0

我测试在iPhone模拟器的应用中,它实际上使得使用的NSDatadataWithContentsOfURL:方法的HTTP请求。使用Objective-C设置代理用户名和密码 - iPhone

我的Mac与用户名&密码保护代理设置。默认情况下,模拟器不考虑我的Mac中提供的代理设置。我收到以下回复。有没有一种方法可以以编程方式提供代理用户名和密码,以便模拟器可以读取值并允许请求发生。

响应:

<HTML><HEAD> 
<TITLE>Access Denied</TITLE> 
</HEAD> 
<BODY> 
<FONT face="Helvetica"> 
<big><strong></strong></big><BR> 
</FONT> 
<blockquote> 
<TABLE border=0 cellPadding=1 width="80%"> 
<TR><TD> 
<FONT face="Helvetica"> 
<big>Access Denied (authentication_failed)</big> 
<BR> 
<BR> 
</FONT> 
</TD></TR> 
<TR><TD> 
<FONT face="Helvetica"> 
Your credentials could not be authenticated: "Credentials are missing.". You will not be permitted access until your credentials can be verified. 
</FONT> 
</TD></TR> 
<TR><TD> 
<FONT face="Helvetica"> 
This is typically caused by an incorrect username and/or password, but could also be caused by network problems. 
</FONT> 
</TD></TR> 
<TR><TD> 
<FONT face="Helvetica" SIZE=2> 
<BR> 
For assistance, contact your network support team. 
</FONT> 
</TD></TR> 
</TABLE> 
</blockquote> 
</FONT> 
</BODY></HTML> 

感谢 Sudheer

回答

1
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ 

    if ([challenge previousFailureCount] == 0) { 
     NSURLCredential *newCredential; 
     newCredential=[NSURLCredential credentialWithUser:@"ABC" 
               password:@"ABC" 
               persistence:NSURLCredentialPersistenceNone]; 
     [[challenge sender] useCredential:newCredential 
       forAuthenticationChallenge:challenge]; 
      } else { 

     [[challenge sender] cancelAuthenticationChallenge:challenge]; 
     // inform the user that the user name and password 
     // in the preferences are incorrect 
    } 

}