views:

906

answers:

5

I'm writing a small program (using Cocoa Touch), which communicates with a webservice. The code for calling the webservice, is the following:

- (IBAction)send:(id)sender
{
    if ([number.text length] > 0)
    {
     [[UIApplication sharedApplication] beginIgnoringInteractionEvents];  
     [activityIndicator startAnimating];
     NSString *modded;
     modded = [self computeNumber];
     NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:      [NSURL URLWithString:@"https://tester:%=&[email protected]/RPC2"]];
     [theRequest setHTTPMethod:@"POST"];
     [theRequest addValue:@"text/xml" forHTTPHeaderField:@"content-type"];
     [theRequest setCachePolicy:NSURLCacheStorageNotAllowed];
     [theRequest setTimeoutInterval:5.0];
     NSString* pStr = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>samurai.SessionInitiate</methodName><params><param><value><struct><member><name>LocalUri</name><value><string></string></value></member><member><name>RemoteUri</name><value><string>sip:%@@sipgate.net</string></value></member><member><name>TOS</name><value><string>text</string></value></member><member><name>Content</name><value><string>%@</string></value></member><member><name>Schedule</name><value><string></string></value></member></struct></value></param></params></methodCall>", modded, TextView.text];
     NSData* pBody = [pStr dataUsingEncoding:NSUTF8StringEncoding];
     [theRequest setHTTPBody:pBody];
     NSURLConnection *theConnection = [[NSURLConnection alloc]     initWithRequest:theRequest delegate:self];

     if (!theConnection)
     {
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                     message:@"A Connection could not be established!"
                    delegate:nil 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles: nil];
      [alert show];
      [alert release];
      sendButton.enabled = TRUE;
      return;
     }
     [pStr release];
     [pBody release];
    }
}

The Username and Password have to be in the URL, and it works in most cases, but when the password consists of special characters like in the example "%=&=-Test2009", the Webservice does not respond. If it is something like "Test2009" it works fine. Does anyone have an idea why, and maybe a solution for that?

+2  A: 

You must use URL safe characters in your string when making the URL, use this NSString method stringByAddingPercentEscapesUsingEncoding: which will do this for you.

Daniel
+1  A: 

Special characters have to be URL encoded, the URL you requested,

https://tester:%=&amp;[email protected]/RPC2

..is invalid, specifically the %= part (% is used for escaping characters, for example %20 is used to represent a space), so..

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:
    [NSURL URLWithString:@"https://tester:%=&amp;[email protected]/RPC2"]];

..should change to something like:

NSString *theAddress = [NSString stringWithFormat:@"https://%@:%@@%@",
                        [@"tester" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                        [@"%=&=-Test2009" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                        @"example.com"];

NSURL *theURL = [NSURL URLWithString:theAddress];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL];

This requests the URL https://tester:%25=&amp;[email protected]

dbr
Although you should have absolutely no need to be doing all this anyway. Instead, use the -connection:didReceiveAuthenticationChallenge: delegate method to send the username and password; don't mess about lumping it into the URL.
Mike Abdullah
A: 

what if, i hv a URL like, www.google.com.php?to=%@&from=@%&message=%@;

somthing like dat,

and m getting the value of message which can be of 1 line or may be of 10 lines,containg space, and symbols ,special characters. the user is going to insert values of message and i hv to pass that message(which contains special characters) in this condition i cant make a pre defined URL,bcoz i hv no idea whe the special character will come.

so how to overcome this.

the URL should automaticlally create a link which may comntains special characters.

suggestion are always appreciated;

regards shishir

shishir.bobby
A: 

Thanks for reply...

but where should i put % sign,since in the message feilds,what user is going to insert,i never know,he can put ^&^^&*(^&#$#@$@%# anything lots of spaces anything...

so ,wat should i do for this?

regards shishir

shishir.bobby
A: 

thats great

it works

thankks daniel

i can send space special characters but only !@#$% these apart from those i can send ^&*()_+|\={}[]:";'>?/,<

these

is there any other way,i can send thse charatcers too

trhanks by teh way daniel

shishir.bobby