xcode HTTP 请求目标 c
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11611350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP request objective c
提问by Alessandro
I am trying to make a HTTP request in this way:
我正在尝试以这种方式发出 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
}
I am trying to make the request with these headers:
我正在尝试使用这些标头发出请求:
Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="<app-key>", oauth_signature="<app-secret>&"
But I can't understand how to. PLease help!!
但我不明白该怎么做。请帮忙!!
采纳答案by Codo
The authorization in your case is just a HTTP header. So it's:
您的情况下的授权只是一个 HTTP 标头。所以是:
[request addValue:@"OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"<app-key>\", oauth_signature=\"<app-secret>&\"" forHTTPHeaderField: @"Authorization"];
Or:
或者:
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];
回答by RaviM
This error occurs when your are passing wrong bundle or document directory path to media. Just simply verify your media path to the HTTP request body.
当您将错误的包或文档目录路径传递给媒体时,会发生此错误。只需简单地验证您的 HTTP 请求正文的媒体路径。
回答by Rahul Vyas
Try commenting all this
尝试评论所有这些
//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
//}
and use this instead of all above
并使用它而不是以上所有
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
Implement these delegate functions
实现这些委托功能
Note - self.data is NSMUtableData object declared as data in header.
注意 - 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];
}
Hope it will help
希望它会有所帮助