0

タイトルはほとんどそれをすべて言います。私のアプリには、次の場所にあるファイル myFile.ext の URL とパスワードがあります。

https://myserver.com/stuff.cgi?db=mydb

UIApplication の canOpenURL および openURL メソッドに渡されると、適切な動作が得られる NSURL オブジェクトを作成したいと考えています。

これは可能ですか?もしそうなら、どのように?また、注意すべきセキュリティ上の問題はありますか?

明確化のために編集:

次のコードは URL 要求を生成し、サーバーに送信されると、アプリは正常にファイルをダウンロードします。しかし、私がやりたいのは、openURL で開くことです。

+ (NSMutableURLRequest *) requestForFileNamed: (NSString *) filename {
    NSString *url = [NSString stringWithFormat:@"%@&user=%@&getbinfile=%@", serverLocation, username, filename];
    NSString *body = [NSString stringWithFormat:@"password=%@", password];
    return [XMLRequestBuilder postRequestWithURL:url body:body];
}

XMLRequestBuilder メソッド:

+ (NSMutableURLRequest *) requestWithURL: (NSString *) url body: (NSString *) body method: (NSString *) method {
    NSURL * theURL = [NSURL URLWithString:url];
    NSMutableURLRequest * ret = [NSMutableURLRequest requestWithURL:theURL];
    [ret setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    [ret setHTTPMethod: method];
    [ret setTimeoutInterval:kDefaultTimeoutInterval];
    return ret;
}


+ (NSMutableURLRequest *) postRequestWithURL: (NSString *) url body: (NSString *) body {
    return [XMLRequestBuilder requestWithURL:url body:body method:@"POST"];
}
4

1 に答える 1

1

(@bodnarbmが指摘したように)HTTP認証が必要だと仮定すると、それはかなり簡単です。didReceiveAuthenticationChallenge を実装するだけです。Apple のドキュメントからのサンプルを次に示します。

[自分の好みの名前] と [自分の好みのパスワード] をユーザー名/パスワードに変更するだけです。

-(void)connection:(NSURLConnection *)connection
        didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge previousFailureCount] == 0) {
        NSURLCredential *newCredential;
        newCredential=[NSURLCredential credentialWithUser:[self preferencesName]
                                                 password:[self preferencesPassword]
                                              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
        [self showPreferencesCredentialsAreIncorrectPanel:self];
    }
}

リンクは次のとおりです: http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

更新: 上記のコメントから、HTTP 認証を使用しているようには見えません (したがって、上記のコードは適用されませんが、他の誰かを助けるためにそのままにしておきます)。

問題に戻る: リクエストで HTTP メソッド ヘッダーの値を「POST」に設定していますか? 本文で pwd を送信しようとしているのに (POST のように)、他のパラメーターが URL に (GET として) あるのはなぜですか? 他のパラメーターを POST 要求の本文に移動します。コードを投稿すると、どこが間違っているかを簡単に確認できる場合があります。

于 2010-02-22T19:13:05.150 に答える