Skip to main content

Consume WCF Web Service using Objective-C on iPhone


I am having a hard time consuming a very simple (Hello World) WCF web service in my iPhone app. From what I have read, you must manually create the request message then send it to the web service URL.



I was able to accomplish this on a .asmx web service, but not with a WCF service.



How do I know the correct format of the request SOAP message?



The web service I am trying to hit has a format of: http://xxx.xxx.xxx.xxx:PORT/IService1/ (running locally in a VM)



I apologize for the lack of information, I am pretty lost.



Any and all help is much appreciated.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. Thank to everyone that helped here. I ended up figuring it out and thought I would share my results. I know this is not a comprehensive solution, so shoot me a message or comment if you require more detail.

    //Variables used
    NSMutableData *webData;
    NSMutableString *soapResults;
    NSXMLParser *xmlParser;
    BOOL recordResults;

    //Web Service Call
    NSString *soapMessage = [NSString stringWithFormat:
    @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    "<SOAP-ENV:Envelope \n"
    "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
    "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
    "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
    "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
    "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n"
    "<SOAP-ENV:Body> \n"
    "<Login xmlns=\"http://tempuri.org/\"><username>JACKSON</username><password>PASSWORD</password>"
    "</Login> \n"
    "</SOAP-ENV:Body> \n"
    "</SOAP-ENV:Envelope>"];

    NSURL *url = [NSURL URLWithString:@"http://172.16.0.142:8731/Service1/"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://tempuri.org/IService1/Login" forHTTPHeaderField:@"Soapaction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if(theConnection) {
    webData = [[NSMutableData data] retain];
    }
    else {
    NSLog(@"theConnection is NULL");
    }

    //Implement the NSURL and XMLParser protocols
    #pragma mark -
    #pragma mark NSURLConnection methods

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection

    #pragma mark -
    #pragma mark XMLParser methods

    -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
    namespaceURI:(NSString *)namespaceURI
    qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict

    -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
    namespaceURI:(NSString *)namespaceURI
    qualifiedName:(NSString *)qName

    ReplyDelete
  2. Have you tried to access the WCF service as a Web Service from a windows client? I haven't configured WCF to run as an asmx styled Web Service but I believe there are some specific settings that have to be tweaked to get WCF to act like in that manner. Working with a windows client would take out any iphone based issues out of the picture for testing purposes and would help you narrow your testing to the service itself.

    ReplyDelete
  3. Caged / httpriot Simple HTTP Rest Library for iPhone and Cocoa projects.

    ReplyDelete
  4. You need to make sure that you are using BasicHttpBinding (not WSHttpBinding) which is aimed more are clients that are not on the .NET 3.0 platform. This Binding does not support security, reliability or ordered delivery.

    ReplyDelete
  5. What kind of error are you getting when you hit our WCF service? So asmx is working but not WCF? One thing you need to pay attention is that in .NET 2.0, the web services (asmx) supported both SOAP 1.1 and SOAP 1.2 messages. However, basicHttpBinding in WCF handles only SOAP 1.1. So if you're sending SOAP 1.2 messages from your iPhone client to the WCF service with basicHttpBinding, this could very well be the problem you're having. In order to support SOAP 1.2, you need to use wsHttpBinding or create a custom binding pretty much the same as basicHttpBinding but you specify the message version to be SOAP 1.2.

    <customBinding>
    <binding name="customHttpBindingWithSoap12">
    <textMessageEncoding messageVersion="Soap12"/>
    <httpTransport />
    </binding>
    </customBinding>

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex