Xcode with web service

Discussion in 'iOS Programming' started by Suki.N, Sep 29, 2011.

  1. Suki.N macrumors newbie

    Joined:
    Sep 29, 2011
    #1
    I had a Objective-C program which call a Web-service method to insert data into SQL. But i failed in inserting "&" and "+". Any idea on how i can solve this problem??
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
  3. Suki.N thread starter macrumors newbie

    Joined:
    Sep 29, 2011
    #3
    I try to store user text message into sql. so it could be a single "&" or "&" with other text.
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    I'm still not sure I correctly understand your problem. Is the problem that a text message may containing a & or + and that "corrupts" your URL?

    For example, you can't send a text message like "You & me + him" because you're producing URLs like the following?

    http://myserver/dosomething?msg=You+&+me+++him
     
  5. z3r0inc, Sep 29, 2011
    Last edited: Sep 29, 2011

    z3r0inc macrumors regular

    Joined:
    Oct 16, 2009
    #5
    Can you show us what you're sending? Just put placeholders for the sensitive parts.
     
  6. Suki.N, Sep 29, 2011
    Last edited: Sep 29, 2011
  7. lefooey macrumors member

    Joined:
    Dec 26, 2009
    Location:
    Spokane, WA
    #7
    You have to encode your URL query strings.

    Code:
    NSString *unencoded = @"+";
    NSString *encoded = [unencoded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    That should point you in the right direction.
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    For a list of characters that must be encoded, see this:
    http://en.wikipedia.org/wiki/Percent-encoding

    For example, you must encode a literal percent character as %25.

    You also ought to know about the application/x-www-form-urlencoded MIME-type.

    And since SQL data insertion was mentioned, you should also be aware of little Bobby Tables:
    http://xkcd.com/327/
     
  9. Suki.N thread starter macrumors newbie

    Joined:
    Sep 29, 2011
    #9
    Thanks crown33.

    I able to insert the user input sms into db.

    Now the problem is when i input "&&&", the program successfully insert into sql. but when i retrieve the data from sql, it give me "&" only.

    Any idea what is going on??

    Below is my code:
    Code:
    ‎
    - (void) downloadingMessages:(NSString *)fromOpp{ 
         NSString *queryString = [NSString
         stringWithFormat:@"http://testing.com/sms_receive.asmx/receive_sms?FromPhone=%@&ToPhone%@",
         fromOpp, fromNumber];
         NSURL *url = [NSURL URLWithString:queryString];
         NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
         [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
         [req addValue:0 forHTTPHeaderField:@"Content-Length"];
         [req setHTTPMethod:@"GET"];
         NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
         if (conn) {
                webData = [[NSMutableData data] retain];
         }
    }
    
     

Share This Page