我正在尝试以这种方式发出HTTP请求:
NSString *urlString = [NSString stringWithFormat:@"https://api.dropbox.com/1/oauth/request_token"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
//set headers
NSString *contentType = [NSString stringWithFormat:@"text/xml"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
oauth_version="1.0"
oauth_signature_method="PLAINTEXT"
oauth_consumer_key="<app-key>"
oauth_signature="<app-secret>&"
//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
//post
[request setHTTPBody:postBody];
//get response
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSLog(@"Response: %@", result);
//here you get the response
}我正在尝试使用以下标头发出请求:
Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="<app-key>", oauth_signature="<app-secret>&"但我不明白该怎么做。PLease救命!!
发布于 2012-07-23 19:37:33
在您的示例中,授权只是一个HTTP标头。所以它是:
[request addValue:@"OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"<app-key>\", oauth_signature=\"<app-secret>&\"" forHTTPHeaderField: @"Authorization"];或者:
NSString* oauth_version=@"1.0";
NSString* oauth_signature_method=@"PLAINTEXT";
NSString* oauth_consumer_key=@"<app-key>";
NSString* oauth_signature=@"<app-secret>&";
NSString* authHeader = [NSString stringWithFormat: @"OAuth oauth_version=\"%@\", oauth_signature_method=\"%@\", oauth_consumer_key=\"%@\", oauth_signature=\"%@\"",
oauth_version, oauth_signature_method, oauth_consumer_key, oauth_signature];发布于 2012-07-23 19:20:22
尝试注释所有这些
//NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
//NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//NSLog(@"Response Code: %d", [urlResponse statusCode]);
//if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
// NSLog(@"Response: %@", result);
//here you get the response
//}并使用它而不是上面的所有
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];实现这些委托函数
注意- self.data是在标题中声明为数据的NSMUtableData对象。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
[self.data appendData:d];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"")
otherButtonTitles:nil] autorelease] show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
// Do anything you want with it
[responseText release];
}
// Handle basic authentication challenge if needed
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
//I'm using HTTP Digest Authentication in your case it could be different
NSURLCredential *credential = [NSURLCredential credentialWithUser:HTTP_DIGEST_USER
password:HTTP_DIGEST_PASSWORD
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}希望它能帮助
发布于 2012-12-07 14:09:47
当您将错误的捆绑包或文档目录路径传递到介质时,会出现此错误。只需验证您指向HTTP请求正文的媒体路径。
https://stackoverflow.com/questions/11611350
复制相似问题