将 JSON 数据放入 NSURL 中

发布于 2024-12-05 00:38:00 字数 668 浏览 0 评论 0原文

我正在尝试做一些看似理所当然的事情,但由于某种原因不起作用。

我正在尝试发出 JSON 数据的获取请求,并在 URL 中发送 JSON“数据”参数。

这是我正在使用的代码:

NSDictionary *whatToPost= [NSDictionary dictionaryWithObjectsAndKeys:
 username, @"username", 
 password, @"password", nil];

NSString *url = [NSString stringWithFormat:@"http://domain.com/user/get?data=%@",
 [whatToPost JSONString]];
NSURL *theUrl = [NSURL URLWithString:
 [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//NSLog(url) will produce this:
  http://domain.com/user/get?data={"username":"test1","password":"poop"}

当我设置断点时,theUrl 为空。我不太明白为什么它会被破坏,但我认为发送 { 或 " 会破坏它。有什么想法吗?我应该切换到 POST 吗?

I'm trying to do something that seems like a no brainer, but for some reason isn't working.

I'm trying to make a get request for JSON data, sending a JSON 'data' parameter in the URL.

Here's the code I'm using:

NSDictionary *whatToPost= [NSDictionary dictionaryWithObjectsAndKeys:
 username, @"username", 
 password, @"password", nil];

NSString *url = [NSString stringWithFormat:@"http://domain.com/user/get?data=%@",
 [whatToPost JSONString]];
NSURL *theUrl = [NSURL URLWithString:
 [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//NSLog(url) will produce this:
  http://domain.com/user/get?data={"username":"test1","password":"poop"}

When I set a breakpoint, theUrl is null. I can't quite figure out why it's breaking, but I figure something about sending the {'s or "'s is breaking it. Any ideas? Should I just switch to POST?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘过的浮云 2024-12-12 00:38:00
NSDictionary *whatToPost= [NSDictionary dictionaryWithObjectsAndKeys:
 username, @"username", 
 password, @"password", nil];

NSString *url = [NSString stringWithFormat:@"http://domain.com/user/get?data=%@",
 [whatToPost JSONString]];
NSURL *theUrl = [NSURL URLWithString:
 [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

您切换了方法

stringByAddingPercentEscapesUsingEncoding:

使用给定编码返回接收者的表示形式,以确定将接收者转换为合法 URL 字符串所需的转义百分比。

通过stringByReplacingPercentEscapesUsingEncoding

返回一个新字符串,该字符串是通过在接收器中用给定编码确定的匹配字符替换所有百分比转义符而生成的。

请参阅:
http://developer.apple.com/library /mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
http://blog.evandavey.com/2009/ 01/如何对 url-encode-nsstring-in-objective-c.html

NSDictionary *whatToPost= [NSDictionary dictionaryWithObjectsAndKeys:
 username, @"username", 
 password, @"password", nil];

NSString *url = [NSString stringWithFormat:@"http://domain.com/user/get?data=%@",
 [whatToPost JSONString]];
NSURL *theUrl = [NSURL URLWithString:
 [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

you switched method

stringByAddingPercentEscapesUsingEncoding:

Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.

by stringByReplacingPercentEscapesUsingEncoding

Returns a new string made by replacing in the receiver all percent escapes with the matching characters as determined by a given encoding.

see:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
http://blog.evandavey.com/2009/01/how-to-url-encode-nsstring-in-objective-c.html

奶茶白久 2024-12-12 00:38:00

您应该考虑在请求中发送 HTTP 身份验证标头,而不是在 URL 本身中发送用户名和密码。

像这样的东西:
[请求 setValue:[whatToPost 描述] forHTTPHeaderField:@"授权"];

当然,这假设您控制着事务的另一端,即您正在访问的 Web 服务。

You should look into sending an HTTP Authentication header in the request instead of sending the username and password in the URL itself.

Something like:
[request setValue:[whatToPost descripiption] forHTTPHeaderField:@"Authorization"];

Of course, this assumes that you are in control of the other end of the transaction, i.e. the web service that you are hitting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文