从Cocoa向Tumblr发送POST请求
此代码段不起作用,我收到服务器的“身份验证失败”响应。有什么想法吗从Cocoa向Tumblr发送POST请求,cocoa,tumblr,nsmutableurlrequest,Cocoa,Tumblr,Nsmutableurlrequest,此代码段不起作用,我收到服务器的“身份验证失败”响应。有什么想法吗 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://www.tumblr.com/api/write"]]; [re
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];
\u tumblrLogin
和\u tumblrPassword
都是通过在我的代码中的其他地方添加百分比转义来运行字符串的。我的登录电子邮件格式为“地址”+test@test.com". 直接登录到tumblr很好,但我想知道“+”字符是否会导致编码问题?它没有逃脱。应该是吗
感谢Martin的建议,我现在使用cfurlCreateStringByAddingPercentEscape
来转义我的登录名和密码。但是,我仍然存在同样的问题,我的身份验证失败。根据对的回答,stringByAddingPercentEscapesUsingEncoding:
不会执行完全转义编码。然而,无论出于何种原因,该方法的CoreFoundation版本会:
[(NSString *) CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)[[self mutableCopy] autorelease], NULL,
CFSTR("=,!$&'()*+;@?\n\"<>#\t :/"), kCFStringEncodingUTF8) autorelease];
[(NSString*)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)[[self-mutableCopy]autorelease],NULL,
CFSTR(“=,!$&'()*+;@?\n\“#\t:/”,kCFStringEncodingUTF8)自动释放];
您也可以使用NSMutableString的replacementsofString:withString:options:
方法手动执行替换,但该方法更为重复和冗长。()问题在于您没有创建正确的HTTP POST请求。POST请求需要格式正确的多部分MIME编码正文,其中包含要发送到服务器的所有参数。您试图将这些参数设置为HTTP标头,但这根本不起作用
此代码将执行您想要的操作,请特别注意创建有效多部分MIME字符串的NSString
类别:
@interface NSString (MIMEAdditions)
+ (NSString*)MIMEBoundary;
+ (NSString*)multipartMIMEStringWithDictionary:(NSDictionary*)dict;
@end
@implementation NSString (MIMEAdditions)
//this returns a unique boundary which is used in constructing the multipart MIME body of the POST request
+ (NSString*)MIMEBoundary
{
static NSString* MIMEBoundary = nil;
if(!MIMEBoundary)
MIMEBoundary = [[NSString alloc] initWithFormat:@"----_=_YourAppNameNoSpaces_%@_=_----",[[NSProcessInfo processInfo] globallyUniqueString]];
return MIMEBoundary;
}
//this create a correctly structured multipart MIME body for the POST request from a dictionary
+ (NSString*)multipartMIMEStringWithDictionary:(NSDictionary*)dict
{
NSMutableString* result = [NSMutableString string];
for (NSString* key in dict)
{
[result appendFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n",[NSString MIMEBoundary],key,[dict objectForKey:key]];
}
[result appendFormat:@"\r\n--%@--\r\n",[NSString MIMEBoundary]];
return result;
}
@end
@implementation YourObject
- (void)postToTumblr
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:
[NSURL URLWithString:@"http://www.tumblr.com/api/write"]];
[request setHTTPMethod:@"POST"];
//tell the server to expect 8-bit encoded content as we're sending UTF-8 data,
//and UTF-8 is an 8-bit encoding
[request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
//set the content-type header to multipart MIME
[request addValue: [NSString stringWithFormat:@"multipart/form-data; boundary=%@",[NSString MIMEBoundary]] forHTTPHeaderField: @"Content-Type"];
//create a dictionary for all the fields you want to send in the POST request
NSDictionary* postData = [NSDictionary dictionaryWithObjectsAndKeys:
_tumblrLogin, @"email",
_tumblrPassword, @"password",
@"regular", @"type",
@"theTitle", @"title",
@"theBody", @"body",
nil];
//set the body of the POST request to the multipart MIME encoded dictionary
[request setHTTPBody: [[NSString multipartMIMEStringWithDictionary: postData] dataUsingEncoding: NSUTF8StringEncoding]];
NSLog(@"Tumblr Login:%@\nTumblr Password:%@", _tumblrLogin, _tumblrPassword);
[NSURLConnection connectionWithRequest:request delegate:self];
[request release];
}
@end
谢谢。根据你的建议,我确实使用了CFURLCreate…但它仍然不起作用。天哪。如果可以的话,我会给这个10000x加上星星。谢谢你的帮助。当我让你上钩的时候,你是否碰巧有一个解释headerFields的链接(你为什么需要设置8位编码)还有MIMEBORDING?您需要设置8位编码,因为请求的主体是使用UTF8编码的数据块(使用-dataUsingEncoding:
方法NSString
并传入NSUTF8StringEncoding
)。UTF8是8位编码,如果它被解释为7位编码(ASCII)可能会发生数据损坏。多部分MIME编码允许您创建包含单独部分的字符串。每个部分由用作边界标记的常量字符串描述。边界字符串是完全任意的,但不能包含在字符串的非边界内容中。可能使用Wikipedia关于MIME的文章ful:你能告诉我tumblrapi可用的工作代码参考吗?