Skip to content

Instantly share code, notes, and snippets.

@Kentzo
Created September 5, 2010 12:44
Show Gist options
  • Select an option

  • Save Kentzo/565999 to your computer and use it in GitHub Desktop.

Select an option

Save Kentzo/565999 to your computer and use it in GitHub Desktop.
{
NSString *httpBodyString = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\
<soap:Body>\
<CheckCustomer xmlns=\"http://tempuri.org/\">\
<ABN>%@</ABN>\
</CheckCustomer>\
</soap:Body>\
</soap:Envelope>", _lastScannedCustomer.ABN];
NSData *httpBodyData = [httpBodyString dataUsingEncoding:NSUnicodeStringEncoding];
[httpBodyString release];
NSURL *webserviceURL = ...;
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:webserviceURL];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[urlRequest addValue:[NSString stringWithFormat:@"%d", [httpBodyData length]] forHTTPHeaderField:@"Content-Length"];
[urlRequest addValue:@"http://tempuri.org/CheckCustomer" forHTTPHeaderField:@"SOAPAction"];
[urlRequest setHTTPBody:httpBodyData];
self._connectionData = [NSMutableData data]; // controller's NSData object
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
// then add this to your controller
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_connectionData appendData:data];
}
- (NSURLRequest *)connection:(NSURLConnection *)aConnection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {
return request;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] > 0) {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
else {
[[challenge sender] useCredential:[challenge proposedCredential] forAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse {
if ([aResponse class] == [NSHTTPURLResponse class]) {
self._response = (NSHTTPURLResponse *)aResponse; // controller's NSHTTPURLResponse object
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
DDXMLDocument *document = [[DDXMLDocument alloc] initWithData:_connectionData options:0 error:NULL];
DDXMLElement *rootElement = [document rootElement];
[document release];
NSUInteger statusCode = [_response statusCode];
if (statusCode >= 200 && statusCode < 400) {
NSError *error = nil;
NSString *xpath = @"/soap:Envelope/soap:Body/myns:CheckCustomerResponse/myns:CheckCustomerResult";
NSArray *elements = [rootElement nodesForXPath:xpath error:&error];
}
}
- (void)connection:(NSURLConnection *)aConnection didFailWithError:(NSError *)aError {
// Deal with error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment