gpt4 book ai didi

encoding - 如何对 NSString 进行 URL 编码

转载 作者:行者123 更新时间:2023-12-02 18:53:56 25 4
gpt4 key购买 nike

我正在尝试对字符串进行 url 编码,但 NSURLConnection 由于“错误的 url”而失败。这是我的网址:

    NSString *address = mp.streetAddress;
NSString *encodedAddress = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *cityState= mp.cityState;
NSString *encodedCityState = [cityState stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *fullAddressURL = [NSString stringWithFormat:@"http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=<X1-ZWz1bivd5de5mz_8xo7s>&address=%@&citystatezip=%@", encodedAddress, encodedCityState];
NSURL *url = [NSURL URLWithString:fullAddressURL];

以下是 API 调用 URL 的示例:

下面是调用 API 以获取精确地址匹配“2114 Bigelow Ave”、“Seattle, WA”的示例:

http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=<ZWSID>&address=2114+Bigelow+Ave&citystatezip=Seattle%2C+WA

由于某种原因,此 URL 无法连接。有人可以帮我吗?

最佳答案

您必须先对 fullAddressURL 进行编码,然后再将其发送到 NSURL而不是单独编码地址和城市状态。

NSString *address = @"2114 Bigelow Ave";
//NSString *encodedAddress = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *cityState= @"Seattle, WA";
// NSString *encodedCityState = [cityState stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *fullAddressURL = [NSString stringWithFormat:@"http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=<X1-ZWz1bivd5de5mz_8xo7s>&address=%@&citystatezip=%@", address, cityState];
fullAddressURL = [fullAddressURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"fullAddressURL: %@",fullAddressURL);

NSURL *url = [NSURL URLWithString:fullAddressURL];

我已经测试了上面的代码,它给出了与给定链接 http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=<ZWSID>&address=2114+Bigelow+Ave&citystatezip=Seattle%2C+WA 相同的输出

关于encoding - 如何对 NSString 进行 URL 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15443040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com