2011-10-06 22 views
1

iosから安全なURLにアクセスしようとしています。基本的にurlはユーザーにUsernameとPasswordを入力するよう促します。 iosからユーザー名とパスワードを送信するにはどうすればよいですか?ここでiosから保護されたURLにアクセスする方法

マイコード は、私がここでJSONパーサーの

- (NSString *)stringWithUrl:(NSURL *)url{ 
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url 
              cachePolicy:NSURLRequestReturnCacheDataElseLoad 
             timeoutInterval:30]; 
// Fetch the JSON response 
NSData *urlData; 
NSURLResponse *response; 
NSError *error; 

// Make synchronous request 
urlData = [NSURLConnection sendSynchronousRequest:urlRequest 
           returningResponse:&response 
              error:&error]; 

// Construct a String around the Data from the response 
return [[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding] autorelease];} 


- (id) objectWithUrl:(NSURL *)url{ 
SBJsonParser *jsonParser = [[[SBJsonParser alloc]init] autorelease]; 
NSString *jsonString = [self stringWithUrl:url]; 

// Parse the JSON into an Object 
return [jsonParser objectWithString:jsonString error:nil]; } 

にアクセスするために使用している方法である私は私の辞書にJSONのキーを取得していたコードの一部です。

- (NSDictionary *) downloadFeed { 
id response = [self objectWithUrl:[NSURL URLWithString:@"http://mysite.com/Services/Secure.svc/GetList?id=2127"]]; 

NSDictionary *feed = (NSDictionary *)response; 
return feed; } 

誰かがこのURLにユーザー名とパスワードを渡すことができますか?

+0

どのような意味でプロンプトが表示されますか?ウェブサイトのログインフォーム、[基本的なHTTP認証](http://en.wikipedia.org/wiki/Basic_access_authentication)? – Alex

+0

基本的なHTTP認証、より明確な画像を得るためにコードを更新しました! – HardCode

+0

「http:// user:[email protected]/...」 –

答えて

3

単純に基本認証を処理するASIHTTPRequestに切り替えるか、またはNSMutableRequestを使用して、Authorizationヘッダーを正しく設定し、base64でエンコードされたuser:passwordペアを設定します。

+0

+1、ASIHTTPRequest、非常に便利でBSDスタイルのライセンス。 – jbat100

+0

ログインコントローラーにASIHTTPRequestを使用しています。しかし、私はログイン後にJSON Parserを使用しています。あなたは、URLの資格情報を使用して十分に安全ではないと思いますか?さて、私は配布にHTTPSを使用します! – HardCode

+0

ここでユーザー名とパスワードを割り当てますか? NSURL * url = [NSURL URLWithString:@ "http:// @ http://mysite.com/Services/Secure.svc/GetList?id = 2127"]; ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL:url]; [request setRequestMethod:@ "GET"]; – HardCode

関連する問題