从 Cocoa 向 Tumblr 发送 POST 请求
此代码片段不起作用,我收到“身份验证失败”消息。来自服务器的响应。有什么想法吗?
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:
[NSURL URLWithString:@"http://www.tumblr.com/api/write"]];
[request setHTTPMethod:@"POST"];
[request addValue:_tumblrLogin forHTTPHeaderField:@"email"];
[request addValue:_tumblrPassword forHTTPHeaderField:@"password"];
[request addValue:@"regular" forHTTPHeaderField:@"type"];
[request addValue:@"theTitle" forHTTPHeaderField:@"title"];
[request addValue:@"theBody" forHTTPHeaderField:@"body"];
NSLog(@"Tumblr Login:%@\nTumblr Password:%@", _tumblrLogin, _tumblrPassword);
[NSURLConnection connectionWithRequest:request delegate:self];
[request release];
_tumblrLogin
和 _tumblrPassword
都通过代码中其他地方的 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding
运行。我的登录电子邮件的格式为“[电子邮件受保护]”。它对于直接登录 tumblr 来说效果很好,但我想知道“+”字符是否会导致编码问题?这并没有被逃脱。应该是吗?
感谢 Martin 的建议,我现在使用 CFURLCreateStringByAddingPercentEscapes
来转义我的登录名和密码。不过,我仍然遇到同样的问题,我的身份验证失败。
This code snippet isn't working, I'm getting an "Authentication Failed." response from the server. Any ideas?
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:
[NSURL URLWithString:@"http://www.tumblr.com/api/write"]];
[request setHTTPMethod:@"POST"];
[request addValue:_tumblrLogin forHTTPHeaderField:@"email"];
[request addValue:_tumblrPassword forHTTPHeaderField:@"password"];
[request addValue:@"regular" forHTTPHeaderField:@"type"];
[request addValue:@"theTitle" forHTTPHeaderField:@"title"];
[request addValue:@"theBody" forHTTPHeaderField:@"body"];
NSLog(@"Tumblr Login:%@\nTumblr Password:%@", _tumblrLogin, _tumblrPassword);
[NSURLConnection connectionWithRequest:request delegate:self];
[request release];
Both _tumblrLogin
and _tumblrPassword
are run through stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding
elsewhere in my code. My login email is of the form "[email protected]". It works just fine for logging in directly to tumblr, but I'm wondering if the "+" character is causing problems with the encoding? It's not being escaped. Should it be?
Thanks to Martin's suggestion, I'm now using CFURLCreateStringByAddingPercentEscapes
to escape my login and password. I'm still having the same issue, though, my Authentication is failing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您没有创建正确的 HTTP POST 请求。 POST 请求需要格式正确的多部分 MIME 编码正文,其中包含要发送到服务器的所有参数。您尝试将参数设置为 HTTP 标头,但这根本不起作用。
此代码将执行您想要的操作,特别注意创建有效的多部分 MIME 字符串的
NSString
类别:The problem is that you are not creating a proper HTTP POST request. A POST request requires a properly formatted multipart MIME-encoded body containing all the parameters you want to send to the server. You are trying to set the parameters as HTTP headers which won't work at all.
This code will do what you want, note especially the
NSString
categories that create a valid Multipart MIME string:根据此问题的答案,
stringByAddingPercentEscapesUsingEncoding:
不执行完整的转义编码。但是,无论出于何种原因,此方法的 CoreFoundation 版本都会这样做:您还可以使用 NSMutableString 的replaceOccurencesOfString:withString:options: 方法手动进行替换,但该方法更加重复和冗长。 (请参阅此处。)
As per the answers to this question,
stringByAddingPercentEscapesUsingEncoding:
doesn't perform a full escape encoding. For whatever reason, the CoreFoundation version of this method does, however:You can also use NSMutableString's
replaceOccurencesOfString:withString:options:
method to do the replacement manually, but that method is more repetitive and verbose. (See here.)