2013-03-11 47 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