views:

12685

answers:

15

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.

Thanks in advance!

A: 

JWD, I'm curious can you detect the service using wcftestclient? Is this a remotely hosted service or are you running it locally?

Jreeter
I can view and invoke it using wcftestclient, it is running on my macbook inside a windows XP VMWare image, inside of visual studio.
JWD
A: 

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.

Chris Porter
I have not tried a windows client, just the WCF Test Client that runs from Visual Studio. I believe the service is running correctly, my question is more focused on consuming it with objective C on the iPhone.
JWD
A: 

Try adding this to CrossDomain.xml and put it into the root directory of your service.. It will open your service up to all domains and accepts all headers.. Change to your liking.

<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
Jreeter
A: 

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.

A: 

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>
Mehmet Aras
+5  A: 

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/\"&gt; \n"
"<SOAP-ENV:Body> \n"
"<Login xmlns=\"http://tempuri.org/\"&gt;&lt;username&gt;JACKSON&lt;/username&gt;&lt;password&gt;PASSWORD&lt;/password&gt;"
"</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
JWD
Hi. I'm trying to access an svc file in a similar way but dont get a response - http://stackoverflow.com/questions/1327673/consuming-svc-service-from-iphone-app. Could really use some help! Thanks.
lostInTransit
+4  A: 

Personally i would recommend adding a REST based endpoint for the WCF service. You can run it simultaneously with the SOAP and its a LOT easier to consume on the iPhone side.

http://msdn.microsoft.com/en-us/library/dd203052.aspx

Lounges
A: 

Hello JWD,

I've tried your code and I can use myWebService SOAP but I don't know how to handle the response of the server after sending my HTTP request !

Any help would really be appreciated

Sorry for my bad english

Regards

If you want to ask a new question you should use the "Ask Question" button in the top right, not post it as an answer to an old question. If you want to reference this thread you can always link to it from the new question.
sth
A: 

Hi,

My WCF webservice returns me JSON object. How would I call it from objective C code written for the iPhone App?

http://www.XXX.com/testService.svc/testMethod?x=null

Any help is appreciated. Thanks.

sg
If you want to ask a new question you should use the "Ask Question" button in the top right, not post it as an answer to an old question. If you want to reference this thread you can always link to it from the new question.
sth
A: 

Thanks for the answer..It really helped me...

nbojja
By now you have enough reputation that you can just upvote the helpful answer and than delete this comment/"answer"...
sth
A: 

Has anyone got this to work yet with an iphone calling a wcf service sending XML?

Nathan187
A: 

can you post a source zip?

phongnt
A: 

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

yasirmturk
A: 

Hello i want to call java webservice in objective c how to call it please send source code

jgi
That's not how SO works! For starters, ask a new question!
ing0
A: 

If you have consider converting your SOAP services to RESTful services you can use this library which is a wrapper for the CFNetwork API.

http://allseeing-i.com/ASIHTTPRequest/

TALLBOY