我尝试使用下面的代码在目标C中实现基本身份验证,但我不断获得http状态500作为我的响应。此POST请求看起来是否正确设置?如果是这样 - 为什么状态500错误?提前感谢您的帮助。
NSString *oauthUserID = @"APP_ID";
NSString *oauthUserPW = @"APP_PASSWORD";
NSString *brmOauthUrl;
brmOauthUrl = @"http://AUTHENTICATION_URL";
// initialize post
NSString *post = [NSString stringWithFormat:@"%@:%@",oauthUserID,oauthUserPW];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postValue = [postData base64EncodedStringWithOptions:1];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
// set up request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:brmOauthUrl]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"Basic %@",postValue] forHTTPHeaderField:@"Authorization"];
[request setHTTPBody:postData];
// make connection.
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(connection) {
NSLog(@"Connection Successful = %@",connection);
} else {
NSLog(@"Connection could not be made = %@",connection);
}
[connection start];
以下是NSLog(@"连接成功=%@",连接);行打印出来:
`{ URL: http://AUTHENTICATION_URL/oauth/token } { status code: 500, headers {
Connection = "keep-alive";
"Content-Type" = "text/plain";
Date = "Thu, 05 Mar 2015 13:39:56 GMT";
"Set-Cookie" = "connect.sid=COOKIE_SID; Path=/; HttpOnly";
"Transfer-Encoding" = Identity;
"X-Powered-By" = Express;
} }`