iPhone 上的 REST API 问题
我正在开发一个 iPhone 应用程序,它将使用 RESTful API 与 Web 服务交互。我使用 NSURLConnection 来完成这项工作。直到今天,网络代码都能很好地满足我的所有需求。
例如,URL(排除站点)是这样的:
/Listing/SearchAd?page=1&sortBy=DateDesc&q=BMW&id_category=3016&psize=20
如果“q”参数(本例中为 BMW)少于 3 个字符,或者完全删除 q 参数,我的应用程序可以取回正确的数据。但如果 q 超过 3 个字符,它只能返回带有空数据的合法 json 结构。
但最奇怪的是,如果我使用另一个测试应用程序(Rested,一个用于测试 REST 服务的 mac 应用程序)来测试相同的 URL,它可以返回所有正确的数据。所以应该不是服务的问题。
我的应用程序中的一些代码:
NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *con = [NSURLConnection connectionWithRequest:req delegate:self];
if (con) {
NSMutableData *data = [[NSMutableData alloc] init];
self.responseData = data;
[data release];
} else {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
我使用 netcat 并得到以下结果: (令牌字段用于 Web 服务的身份验证) 使用
Rested 的请求:
GET /Listing/SearchAd?page=1&sortBy=DateDesc&q=BMW&id_category=3016&psize=20 HTTP/1.1
Host: localhost:2000
User-Agent: Rested/1.3 CFNetwork/520.0.13 Darwin/11.0.0 (x86_64) (MacBookPro7%2C1)
Accept: */*
Token: {E53D7DED-510A-414D-824D-3433077CF064}
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
Connection: keep-alive
使用我的应用程序的请求:
GET /Listing/SearchAd?page=1&sortBy=DateDesc&q=BMW&id_category=3016&psize=20 HTTP/1.1
Host: localhost:2000
User-Agent: Pazar/1.0 CFNetwork/485.13.9 Darwin/11.0.0
Token: {E53D7DED-510A-414D-824D-3433077CF064}
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: keep-alive
我的应用程序获取的请求的原始数据只有 37 个字节长。使用SBJsonParser转换后,它只是根级字典,数据字段为空。
那么可能出了什么问题呢?
I'm working on an iPhone app that will interact with a web service using RESTful API. I use NSURLConnection to do the work. The network code was working very well for all my needs until today.
For an instance, the URL(exclude the site) is like this:
/Listing/SearchAd?page=1&sortBy=DateDesc&q=BMW&id_category=3016&psize=20
If the "q" parameter (BMW in this example) is less than 3 characters, or remove the q parameter entirely, my app can get back the right data. But if the q has more than 3 characters it can only get back a legal json structure with empty data.
But the most weird thing is, if I use another testing app (Rested, a mac app for testing REST services) to test the same URL, it can get back all the right data. so it shouldn't be a problem of the service.
Some code from my app:
NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *con = [NSURLConnection connectionWithRequest:req delegate:self];
if (con) {
NSMutableData *data = [[NSMutableData alloc] init];
self.responseData = data;
[data release];
} else {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
I used netcat and get the following result:
(the Token field is used for authentication for the web service)
The request using Rested:
GET /Listing/SearchAd?page=1&sortBy=DateDesc&q=BMW&id_category=3016&psize=20 HTTP/1.1
Host: localhost:2000
User-Agent: Rested/1.3 CFNetwork/520.0.13 Darwin/11.0.0 (x86_64) (MacBookPro7%2C1)
Accept: */*
Token: {E53D7DED-510A-414D-824D-3433077CF064}
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
Connection: keep-alive
The request using my app:
GET /Listing/SearchAd?page=1&sortBy=DateDesc&q=BMW&id_category=3016&psize=20 HTTP/1.1
Host: localhost:2000
User-Agent: Pazar/1.0 CFNetwork/485.13.9 Darwin/11.0.0
Token: {E53D7DED-510A-414D-824D-3433077CF064}
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: keep-alive
The raw data my app get for the request is only 37 bytes long. After conversion using SBJsonParser, it's only the root level dictionary with the data field empty.
What could possibly be wrong then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些调试技巧:
使用
netcat -l -p 2000
并使用http://localhost:2000/
作为端点来捕获原始请求。也使用 Rested 捕获请求并比较两者。另外,记录您的原始回复以查看其中的真实内容。也许您的 JSON 转换有问题。
编辑
由于请求看起来相同,让我们更进一步:日志代理。这是我最喜欢的 netcat 代理,它将 localhost:3000 转发到 localhost:2000 并记录到 infile 和 outfile。根据需要进行修改,然后使用 Rested 和您的应用程序再次进行测试,看看真正返回了什么:
Some debugging tips:
Use
netcat -l -p 2000
and usehttp://localhost:2000/
as your endpoint to capture the raw request. Capture the request using Rested as well and compare the two.Also, log your raw response to see what is really in it. Perhaps your JSON conversion has an issue.
EDIT
Since the requests seem identical lets go a step further: A logging proxy. Here is my favorite netcat proxy, which forwards localhost:3000 to localhost:2000 and logs to infile and outfile. Modify as needed and test again with Rested and your app to see what really comes back.: