Last active
December 15, 2015 00:59
-
-
Save rpunt/5176523 to your computer and use it in GitHub Desktop.
Retrieve a list of roomlists from Exchange 2010 via SOAP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Retrieve a list of roomlists from Exchange 2010 via SOAP. | |
// This method then uses SMXMLDocument to retrieve the e-mail addresses for those roomlists, | |
// which can be used to query the individual rooms | |
// DEPENDENCIES: AFNetworking v1 (https://github.com/AFNetworking/AFNetworking) | |
// SMXMLDocument (https://github.com/nfarina/xmldocument) | |
// | |
// NOTE: I used AFHTTPRequestOperation instead of AFXMLRequestOperation because I wanted the XML, not an XML parser | |
// (which is what AFXMLRequestOperation returns) | |
- (void) readRoomLists { | |
NSString *soapMessage = [NSString stringWithFormat:@"" | |
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>" | |
@"<soap:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\" xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\">" | |
@"<soap:Header>" | |
@"<t:RequestServerVersion Version =\"Exchange2010_SP2\"/>" | |
@"</soap:Header>" | |
@"<soap:Body>" | |
@"<m:GetRoomLists />" | |
@"</soap:Body>" | |
@"</soap:Envelope>" | |
@""]; | |
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[self makeExchangeRequestWithMessage:soapMessage]]; | |
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { | |
NSError *error_ = nil; | |
SMXMLDocument *xml = [SMXMLDocument documentWithData:responseObject error:&error_]; | |
if (!error_) { | |
SMXMLElement *Body = [xml.root childNamed:@"Body"]; | |
SMXMLElement *GetRoomListsResponse = [Body childNamed:@"GetRoomListsResponse"]; | |
SMXMLElement *RoomLists = [GetRoomListsResponse childNamed:@"RoomLists"]; | |
for (SMXMLElement *room in [RoomLists childrenNamed:@"Address"]) { | |
SMXMLElement *element = [room childNamed:@"EmailAddress"]; | |
NSString *email = [element value]; | |
// do something with the roomlist e-mail address here | |
} | |
NSLog(@"finished reading roomLists"); | |
} | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
NSLog(@"error: %@", operation.responseString); | |
}]; | |
[operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) { | |
[self handleAuthChallenge:connection :challenge]; | |
}]; | |
[operation start]; | |
} | |
- (NSMutableURLRequest *)makeExchangeRequestWithMessage:(NSString *)soapMessage { | |
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://OutlookWebAccessServer.domain.tld/ews/exchange.asmx"]]; | |
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; | |
[request setValue:@"" forHTTPHeaderField:@"SOAPAction"]; | |
[request setHTTPMethod:@"POST"]; | |
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; | |
return request; | |
} | |
-(void) handleAuthChallenge:(NSURLConnection *) connection :(NSURLAuthenticationChallenge *) challenge { | |
if( [[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodNTLM ) { | |
NSLog(@"trying NTLM..."); | |
if( [challenge previousFailureCount] > 0 ) { | |
// Avoid too many failed authentication attempts which could lock out the user | |
NSLog(@"NTLM backing out, too many failures"); | |
[[challenge sender] cancelAuthenticationChallenge:challenge]; | |
} else { | |
NSLog(@"NTLM success!"); | |
[[challenge sender] useCredential:[NSURLCredential credentialWithUser:self.soapUsername password:self.soapPassword persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge]; | |
} | |
} else { | |
// Authenticate in other ways than NTLM if desired or cancel the auth like this: | |
[[challenge sender] cancelAuthenticationChallenge:challenge]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment