使用 3G 连接的 ASIHTTPRequest 参数问题(使用 Wifi 时正常)
我的 iPhone 应用程序的一位用户抱怨说,当使用他的移动数据连接时,该应用程序无法工作(但通过 Wifi 工作正常)。我的应用程序向第三方 REST API 发出请求,我为此使用 ASIHTTPRequest 库。
我的 HTTP 请求的一个参数是用户名(即电子邮件地址),因此我使用以下代码对用户名进行编码:
-(NSString *) encodeString:(NSString *) string
{
NSString * returnString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef) string,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 );
return [returnString autorelease];
}
然后将其内置到 URL 中,如下所示:
// Setup the url we need to make the request to the sharkscope API
NSString * urlString = [NSString stringWithFormat: @"%SERVICEADDRESSHERE?Username=%@&Password=%@",
encodedUserName, m_md5Key];
NSURL * url = [NSURL URLWithString:urlString];
request = [[ASIHTTPRequest requestWithURL:url] retain];
从我的应用程序发送的是包含以下内容的请求用户名:
Username=Fred.Bloggs%40wanadoo.fr
对于这个用户,使用他的移动数据连接时在服务器上收到的内容是:
Username=Fred.Bloogs%2540wanadoo.fr
很明显,% 字符在我调用 requestWithURL 和它到达服务器之间的某个位置被编码。我无法理解原因,因为我有数百个其他用户可以通过他们的数据连接很好地运行请求。
我是否需要对传递到 requestWithURL 的值进行编码?
有谁知道 % 字符在哪个阶段被重新编码。我猜它一定在 ASIHTTP 库中,但无法弄清楚在这种情况下什么情况会触发它。
I have one user of my iPhone application who is complaining it does not work when using his mobile data connection (but works fine via Wifi). My application make a request to a third party REST API and I use the ASIHTTPRequest library for this.
One parameter of my HTTP request is a username (which is an email address), so I encode the username using the following code:
-(NSString *) encodeString:(NSString *) string
{
NSString * returnString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef) string,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 );
return [returnString autorelease];
}
This then gets built into a URL as follows:
// Setup the url we need to make the request to the sharkscope API
NSString * urlString = [NSString stringWithFormat: @"%SERVICEADDRESSHERE?Username=%@&Password=%@",
encodedUserName, m_md5Key];
NSURL * url = [NSURL URLWithString:urlString];
request = [[ASIHTTPRequest requestWithURL:url] retain];
What get's sent out of my application is a request with the following Username:
Username=Fred.Bloggs%40wanadoo.fr
For this one user, what is received at the server when using his mobile data connection is:
Username=Fred.Bloogs%2540wanadoo.fr
So clearly the % character is being encoded somewhere between me making the call to requestWithURL and it arriving at the server. I can't understand the reason because I have hundreds of other users who can run the requests fine via their data connection.
Do I even need to encode the values passed into requestWithURL?
Does anyone have any idea at what stage the % character is being re-encoded. I'm guessing it must be in ASIHTTP library, but can't work out what circumstances would trigger it in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
损坏的发生可能是由于运营商在 3G 连接上安装了代理或透明代理。
可能的解决方法:
为了证明这一理论,您可以尝试要求他在使用 3g 连接时在 iOS 上的 Safari 中输入相同/相似的请求。
The corruption is probably happening because of a proxy or transparent proxy on the 3G connection, put in place by the carrier.
Possibly workounds:
To prove this theory, you could try asking him to enter the same / similar request in Safari on iOS when he's using the 3g connection.