0

URLに垂直パイプを入れようとしています

入力文字列: http://testURL.com/Control?command=dispatch|HOME|ABC :ユーザー名

-(NSString *)getURLEncodedString:(NSString *)stringvalue{

NSMutableString *output = [NSMutableString string];
const unsigned char *source = (const unsigned char *)[stringvalue UTF8String];
int sourceLen = strlen((const char *)source);
for (int i = 0; i < sourceLen; ++i) {
    const unsigned char thisChar = source[i];
    if (thisChar == ':' || thisChar == '/' || thisChar == '?' || thisChar == '=' || thisChar == '|' || thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
               (thisChar >= 'a' && thisChar <= 'z') ||
               (thisChar >= 'A' && thisChar <= 'Z') ||
               (thisChar >= '0' && thisChar <= '9')) {
        [output appendFormat:@"%c", thisChar];
    } else {
        [output appendFormat:@"%%%02X", thisChar];
    }
}
return output;
}

上記のメソッドを呼び出した後の出力文字列: http://testURL.com/Control?command=dispatch|HOME|ABC:User%20Name

ここで、上記のエンコード文字列を [[NSURL URLWithString:encodedString]; に渡すと、

Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0xae9d760 {NSUnderlyingError=0xaec8ed0 "bad URL", NSLocalizedDescription=bad URL} を取得しています

この人たちについて何か意見はありますか?URLをencodedStringのように見せたいです。

ありがとうございました!

4

1 に答える 1

2

文字列を手動でエンコード/エスケープする理由が本当にわかりません...とにかく、これはうまく機能します:

NSString *urlString = @"http://testURL.com/Control?command=dispatch|HOME|ABC:User Name";
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

どの出力:

http://testURL.com/Control?command=dispatch%7CHOME%7CABC:User%20Name

NSURL結局のところ、メソッドでエンコードしなかったため、コードを取得していない垂直バーが好きではないようですbad URL

于 2013-06-06T13:45:07.710 に答える