2013-03-11 49 views
0

我在iphone中使用bigcommerce api来从中获取数据,所以我在xml解析的帮助下做到了这一点,但要获取它要求登录到bigcommerce网站的命令列表,然后解析数据如果有人帮我在这的话,我会很感激,请你告诉我通过XML解析如何才能发送登录凭据,然后打在URL解析数据.....iphone上的bigcommerce api

三江源

我我正在写这个代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
// Add the navigation controller's view to the window and display. 
[self.window addSubview:navigationController.view]; 
[self.window makeKeyAndVisible]; 

// Override point for customization after application launch. 
NSString *string=[NSString stringWithFormat:@"https://www.labradorhometraining.com/api/v2"]; 
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:string]]; 

// NSString *dataString = [NSString stringWithFormat:@"{\"screenName\":\"%@\",\"password\":\"%@\",\"pushToken\":\"%@\",\"deviceType\":\"%@\"}", Screentxtf.text,passtxtf.text, str, deviceType]; 

[request setRequestMethod:@"GET"]; 
[request appendPostData:[string dataUsingEncoding:NSUTF8StringEncoding]]; 
// Basic YWRtaW46cGFzc3dvcmQ= 
[request addRequestHeader:@"Content-Type" value:@"application/xml"]; 
[request addRequestHeader:@"Authorization: Basic ZGVtb2tleTpkZW1vdG9rZW4= " value:[NSString stringWithFormat:@"%@ %@",@"api", @"c275ab4076f87"]]; 
[request setUseSessionPersistence:NO]; 
[request setUseCookiePersistence:NO]; 
[request setCacheStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; 
[request setDelegate:self]; 
[request startAsynchronous]; 
return YES; 
} 

这是在根视图控制器

-(void)gototselect{ 
NSString *string=[NSString stringWithFormat:@"https://www.labradorhometraining.com/api/v2/orders.xml"]; 
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:string]]; 

// NSString *dataString = [NSString stringWithFormat:@"{\"screenName\":\"%@\",\"password\":\"%@\",\"pushToken\":\"%@\",\"deviceType\":\"%@\"}", Screentxtf.text,passtxtf.text, str, deviceType]; 

[request setRequestMethod:@"PUT"]; 
// [request appendPostData:[string dataUsingEncoding:NSUTF8StringEncoding]]; 
[request addRequestHeader:@"Authorization" value:[NSString stringWithFormat:@"%@ %@",@"api", @"c2714076f87"]]; 
[request allowCompressedResponse]; 
[request setUseSessionPersistence:NO]; 
[request setUseCookiePersistence:NO]; 
[request setCacheStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; 
[request setDelegate:self]; 
[request startAsynchronous]; 

} 

回答

1

我对ASIHTTPRequest API没有任何意见,也从来没有用过我的职业生涯。但是,使用普通的客观-C我分担你如何验证HTTP请求一些代码,

// Setup NSURLConnection 
NSURL *URL = [NSURL URLWithString:url]; 
NSURLRequest *request = [NSURLRequest requestWithURL:URL 
            cachePolicy:NSURLRequestUseProtocolCachePolicy 
           timeoutInterval:30.0]; 

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
[connection start]; 
[connection release]; 

// NSURLConnection Delegates 
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
if ([challenge previousFailureCount] == 0) { 
    NSLog(@"received authentication challenge"); 
    NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"USER" 
                   password:@"PASSWORD" 
                  persistence:NSURLCredentialPersistenceForSession]; 
    NSLog(@"credential created"); 
    [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; 
    NSLog(@"responded to authentication challenge");  
} 
else { 
    NSLog(@"previous authentication failure"); 
} 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
... 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
... 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
... 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
... 
} 

上面的代码中,我从NSURLConnection and Basic HTTP Authentication in iOS此链接复制。 另外我想分享一些更多的链接与你。请参阅苹果文档的HTTP请求http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html 而这篇文章http://www.cocoanetics.com/2010/12/nsurlconnection-with-self-signed-certificates/ ..你会得到很多有关这项工作的信息和战术。

+0

谢谢你的帮助 – Priyanka 2013-03-11 12:57:49

+0

@Priyanka,欢迎光临。 – Tirth 2013-03-11 13:04:02

+0

@Priyanka,对不起,我不能给你我的电子邮件ID,但你可以在这里见我http://chat.stackoverflow.com/rooms/682/iphone-ipad – Tirth 2013-03-13 12:56:12