c# - How to parse .Net Webservice on iphone with Authentication -
i trying fetch xml using nsmutablerequest , nsurl connection receiving 0 bytes when passed ?wsdl in link in response schema, want fetch xml , parse using touchxml. code below.
nsstring *username = @"username"; nsstring *password = @"pasword"; nsstring *soapmessage = [nsstring stringwithformat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<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/\">\n" "<soap:header>\n" "<requestauthenticator xmlns=\"http://tempuri.org/\">\n" "<password>%@</password>\n" "<username>%@</username>\n" "</requestauthenticator>\n" "</soap:header>\n" "<soap:body>\n" "<methodname xmlns=\"http://tempuri.org/\"/>\n" "</soap:body>\n</soap:envelope>\n",password,username]; nslog(@"%@",soapmessage); nsurl *url = [nsurl urlwithstring:@"http://websitename.com/website_english/wservice/website_service.asmx"]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; /*asihttprequest *request = [asihttprequest requestwithurl:url]; [request setusekeychainpersistence:yes]; [request setusername:@"username"]; [request setpassword:@"password"]; [request setdelegate:self]; [request startasynchronous];*/ nsstring *msglength = [nsstring stringwithformat:@"%d", [soapmessage length]]; [request addvalue: @"text/xml; charset=utf-8" forhttpheaderfield:@"content-type"]; [request addvalue: @"http://tempuri.org/methodname" forhttpheaderfield:@"soapaction"]; [request addvalue: msglength forhttpheaderfield:@"content-length"]; [request sethttpmethod:@"post"]; [request sethttpbody: [soapmessage datausingencoding:nsutf8stringencoding]]; nsurlconnection *theconnection = [[nsurlconnection alloc] initwithrequest:request delegate:self]; if( theconnection ) { webdata = [[nsmutabledata data] retain]; } else { nslog(@"theconnection null"); } -(void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { [webdata setlength: 0]; } -(void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [webdata appenddata:data]; } -(void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { nslog(@"error theconenction"); [connection release]; [webdata release]; } -(void)connectiondidfinishloading:(nsurlconnection *)connection { nslog(@"done. received bytes: %d", [webdata length]); nsstring *thexml = [[nsstring alloc] initwithbytes: [webdata mutablebytes] length:[webdata length] encoding:nsutf8stringencoding]; nslog(@"%@",thexml); [thexml release]; }
but returning me 0 bytes , no xml, original website name not disclose security.
i tried hellosoap.xcodeproj sample code aswell unable through it.
if problem because of authentication take @ question , answer had deal there: soap , xml response parsing samples iphone/ipad?
other without knowing more service expects , passing, don't know how you'll able get. now, don't see wrong code have.
edit:
- (void)connection:(nsurlconnection *)connection didreceiveauthenticationchallenge:(nsurlauthenticationchallenge *)challenge { nsurlcredential *mycreds = [[nsurlcredential alloc] initwithuser:@"**username**" password:@"**password**" persistence:no]; [challenge.sender usecredential:mycreds forauthenticationchallenge:challenge]; [mycreds release]; }
i edited in code answer linked question ease of access.
also, i'm re-reading code, username , password actually, "username" , "password"? if so, , copy , pasted above code, when set variable password near top, have misspelled "password".
-karoly
Comments
Post a Comment