0

我在 iphone 中使用 bigcommerce api 从中获取数据,所以我在 xml 解析的帮助下执行此操作,但要获取它要求登录 bigcommerce 网站的订单列表,然后在有人帮助我的情况下解析数据那么我将非常感激,请通过xml解析告诉我我们如何发送登录凭据然后点击url来解析数据.....

谢谢你

我正在写这段代码

- (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];

}
4

1 回答 1

1

我对 ASIHTTPRequest API 一无所知,而且我的职业生涯也从未使用过它。但是使用普通的objective-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 {
...
}

我从iOS 中的 NSURLConnection 和 Basic HTTP Authentication这个链接复制了上面的代码。另外,我想与您分享更多链接。请参阅苹果文档以获取 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/ .. 你会得到很多关于这项工作的信息和策略。

于 2013-03-11T11:29:17.703 回答