0

I am trying to fetch xml using NSMutableRequest and NSURL connection but I am receiving 0 bytes but when I passed ?WSDL in the link so in response get the schema, but I want to fetch xml and then will parse using TouchXML. My 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 is 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 with 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 it is returning me 0 bytes and no xml, original website name is not disclose for security.

I also tried HelloSOAP.xcodeproj sample code aswell but I am unable to get through it.

user366584
  • 1,016
  • 1
  • 16
  • 34

2 Answers2

0

If the web service requires authentication, then you need to provide it.

You're not setting any authentication in the request, so it won't send any.

Depending on the web service, the authentication might be as simple as basic and that is just the username and password base64 encoded.

Morten Fast
  • 6,322
  • 27
  • 36
  • Can u post any sample code as I am passing authentication in header, and before I used ASIHTTP library , as u can find sample commented code above in which I passed username and password, but doesnt work for me, although username and password as correct. – user366584 Jul 21 '11 at 10:21
0

If your problem is because of the authentication than take a look at my question and answer as I had to deal with it there: SOAP and XML Response Parsing Samples for iPhone/iPad?

Other than that without knowing more about what the service expects and what you are passing, I don't know how much help you'll be able to get. As it is now, I don't see anything wrong with the code that you 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 the code from my answer to the linked question for ease of access.

Also, I'm re-reading your code, is your username and password actually, "username" and "password"? If so, and you copy and pasted the above code, when you set variable password near the top, you have misspelled "password".

-Karoly

Community
  • 1
  • 1
Karoly S
  • 3,180
  • 4
  • 35
  • 55