ios - Wrong data returned from NSURLResponse when trying to invoke web service method -


im rather new @ this.. got tomcat server running , have web service method there when invoked, returns string. when try use

nsoperationqueue *backgroundqueue = [[nsoperationqueue alloc] init];  // url request  nsmutableurlrequest *request = [[nsmutableurlrequest alloc] initwithurl:  [nsurl urlwithstring:@"http://localhost:8080/webservicetutorialclient/samplehelloproxy/input.jsp?method=18"]];  [request sethttpmethod:@"post"]; // send request   [nsurlconnection sendasynchronousrequest:request                                    queue:backgroundqueue                        completionhandler:^(nsurlresponse *response, nsdata *data, nserror *error){                            nsstring *result = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];                            nslog(@"%@", result);                        }]; 

i contents of page, rather string method supposed return. prolly looks silly have no idea how string. response (when nslog reponse object see that), assume contains html data returned anyway.

how invoke method tomcat server, getting string returned me? alot time!

edit: logged response:

<html> <head> <title>inputs</title> </head> <body> <h1>inputs</h1>   <form method="post" action="result.jsp" target="result"> <input type="hidden" name="method" value="18"> <br> <input type="submit" value="invoke"> <input type="reset" value="clear"> </form>   </body> </html> 

edit 2: hello service diagram. how invoke code specific method? hello service

as discussed in chat, code in original question conflates html user interface testing web service actual web service itself. sample code making http request of html page used testing web service. want interact directly web service.

having said that, using soap/wsdl web service interface makes process pretty complicated. in how access soap services iphone discussion, agree schwa's conclusion: "don't". more point, if you're writing new web service, rest service delivering json going easier work with. or, if you're stuck soap-based service, write new web service consumes soap service , delivers json.

if want ios app interact directly soap service defined particular wsdl, seems there 2 approaches: first, use 1 of source code generators (like sudzc.com). underwhelmed class of solution out there. second, use tool soap ui manually create xml request, , use template ios.

so, tried out latter approach:

  1. you mentioned followed the tutorial. found this article more useful in getting web service going, if had success former, that's fine.

  2. i created web service, hellotest, hello class single method, sayhello, takes 1 parameter, name, , returns "hello " + name:

    package com.tutorial;  public class hello {     public string sayhello(string name){         return "hello " + name;     } } 
  3. i had eclipse generate wsdl me. particulars uninteresting, it's not entirely dissimilar yours:

    <?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://tutorial.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetnamespace="http://tutorial.com">     <wsdl:documentation>         please type service description here     </wsdl:documentation>     <wsdl:types>         <xs:schema attributeformdefault="qualified" elementformdefault="qualified" targetnamespace="http://tutorial.com">             <xs:element name="sayhello">                 <xs:complextype>                     <xs:sequence>                         <xs:element minoccurs="0" name="name" nillable="true" type="xs:string"/>                     </xs:sequence>                 </xs:complextype>             </xs:element>             <xs:element name="sayhelloresponse">                 <xs:complextype>                     <xs:sequence>                         <xs:element minoccurs="0" name="return" nillable="true" type="xs:string"/>                     </xs:sequence>                 </xs:complextype>             </xs:element>         </xs:schema>     </wsdl:types>     <wsdl:message name="sayhellorequest">         <wsdl:part name="parameters" element="ns:sayhello"/>     </wsdl:message>     <wsdl:message name="sayhelloresponse">         <wsdl:part name="parameters" element="ns:sayhelloresponse"/>     </wsdl:message>     <wsdl:porttype name="helloporttype">         <wsdl:operation name="sayhello">             <wsdl:input message="ns:sayhellorequest" wsaw:action="urn:sayhello"/>             <wsdl:output message="ns:sayhelloresponse" wsaw:action="urn:sayhelloresponse"/>         </wsdl:operation>     </wsdl:porttype>     <wsdl:binding name="hellosoap11binding" type="ns:helloporttype">         <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>         <wsdl:operation name="sayhello">             <soap:operation soapaction="urn:sayhello" style="document"/>             <wsdl:input>                 <soap:body use="literal"/>             </wsdl:input>             <wsdl:output>                 <soap:body use="literal"/>             </wsdl:output>         </wsdl:operation>     </wsdl:binding>     <wsdl:binding name="hellosoap12binding" type="ns:helloporttype">         <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>         <wsdl:operation name="sayhello">             <soap12:operation soapaction="urn:sayhello" style="document"/>             <wsdl:input>                 <soap12:body use="literal"/>             </wsdl:input>             <wsdl:output>                 <soap12:body use="literal"/>             </wsdl:output>         </wsdl:operation>     </wsdl:binding>     <wsdl:binding name="hellohttpbinding" type="ns:helloporttype">         <http:binding verb="post"/>         <wsdl:operation name="sayhello">             <http:operation location="sayhello"/>             <wsdl:input>                 <mime:content type="application/xml" part="parameters"/>             </wsdl:input>             <wsdl:output>                 <mime:content type="application/xml" part="parameters"/>             </wsdl:output>         </wsdl:operation>     </wsdl:binding>     <wsdl:service name="hello">         <wsdl:port name="hellohttpsoap11endpoint" binding="ns:hellosoap11binding">             <soap:address location="http://localhost:8080/hellotest/services/hello.hellohttpsoap11endpoint/"/>         </wsdl:port>         <wsdl:port name="hellohttpsoap12endpoint" binding="ns:hellosoap12binding">             <soap12:address location="http://localhost:8080/hellotest/services/hello.hellohttpsoap12endpoint/"/>         </wsdl:port>         <wsdl:port name="hellohttpendpoint" binding="ns:hellohttpbinding">             <http:address location="http://localhost:8080/hellotest/services/hello.hellohttpendpoint/"/>         </wsdl:port>     </wsdl:service> </wsdl:definitions> 
  4. i used soap ui take wsdl , create example request.

    <?xml version="1.0" encoding="utf-8"?> <soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tut="http://tutorial.com">     <soap:header/>     <soap:body>         <tut:sayhello>             <!--optional:-->             <tut:name>rob</tut:name>         </tut:sayhello>     </soap:body> </soap:envelope> 

    it may have been unique configuration, had play around soap envelope generated soap ui, replacing soap 1.2 uri:

    http://www.w3.org/2003/05/soap-envelope 

    with soap 1.1 uri:

    http://schemas.xmlsoap.org/soap/envelope/ 

    i also, purposes of example, replaced ? placeholder "rob" (as wanted web service hello me).

  5. anyway, can submit request in ios via following objective-c code:

    nsurl *url = [nsurl urlwithstring:@"http://localhost:8080/hellotest/services/hello"]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url];  nsstring *filename = [[nsbundle mainbundle] pathforresource:@"request1" oftype:@"xml"]; nsdata *requestbodydata = [nsdata datawithcontentsoffile:filename]; request.httpbody = requestbodydata;  [request sethttpmethod:@"post"]; [request addvalue:@"http://tutorial.com" forhttpheaderfield:@"soapaction"]; [request addvalue:@"text/xml; charset=utf-8" forhttpheaderfield:@"content-type"];  nsoperationqueue *queue = [[nsoperationqueue alloc] init]; [nsurlconnection sendasynchronousrequest:request                                    queue:queue                        completionhandler:^(nsurlresponse *response, nsdata *data, nserror *error) {                            nslog(@"%s", __function__);                            if (data)                            {                                nslog(@"%@", [[nsstring alloc] initwithdata:data                                                                   encoding:nsutf8stringencoding]);                            }                        }]; 

    clearly, how create nsdata request may vary (to keep "simple" put xml of soap request text file called request1.xml , included in bundle). likewise, how initiate request may (i used nsurlconnection method, sendasynchronousrequest). gives idea of how you'd send manually created soap request.

  6. that generates response looks (i've prettified it, response):

    <?xml version='1.0' encoding='utf-8'?> <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">     <soapenv:body>         <ns:sayhelloresponse xmlns:ns="http://tutorial.com">             <ns:return>hello rob</ns:return>         </ns:sayhelloresponse>     </soapenv:body> </soapenv:envelope> 

    you can parse answer xml parser, such nsxmlparser.

for me, "take home" message wouldn't unless absolutely had to. think json web services easy create, , it's far easier consume responses in ios.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -