0

I want to send the bytes to the server. So i donno Which data types is used for bytes. I have used "%s" and sent the bytes to the server. But In server side they have received 6 bytes only. But my case i want to send 32 bytes to the server. So Which data type is used for that?

EDIT:-

Here my sample code is,

 -(void)sendDevice:(NSData *)data // data value comes 32 bytes.
 {
       NSString *urlString = [NSString stringWithFormat: @"http://MyserverURL.php?Dataid=%????",[data bytes]];

       NSURL *urlToSend2 = [[NSURL alloc] initWithString:urlString];

       NSURLRequest *urlRequest2 = [NSURLRequest requestWithURL:urlToSend2              cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:50];                                                     

       NSURLConnection *theconnection=[[NSURLConnection alloc] initWithRequest:urlRequest2 delegate:self]; 

      [theconnection start];

 }

Please Guide me.

Thanks.

Pugalmuni
  • 9,350
  • 8
  • 56
  • 97
  • %s makes it a string, danger! strings terminate at a 0 byte in C (and Objective C as well) so maybe you had 6 nonzero bytes and one 0 next, e.g. Not all characters can safely appear in URL's (that's why we have url-encoding/quoting), so %s is not a good idea! – Henno Brandsma Aug 05 '10 at 17:46

5 Answers5

1

NSData is the class that generally is used for byte data. take a look at its documentation and see if thats what you need.

Jesse Naugher
  • 9,780
  • 1
  • 41
  • 56
  • Thanks for the reply. I have used NSData and sent the bytes to the server. In my case, NSData value is [data bytes], How can i post to the server, NSString *urlString = [NSString stringWithFormat: @"http://InMy serverurl.com/DTid=%????",[data bytes]]; – Pugalmuni Aug 05 '10 at 15:04
1

As Jesse suggests, raw bytes are best stored in an NSData instance. For transmission to your web server, you'll probably want to create a Base64-encoded string representation of the NSData's bytes. For that, I recommend either of the categories present at the bottom of this CocoaDev wiki page.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
1

Look like u use POST data to the server?

Like:-----

- (Boolean) pushSync: (NSString *) fromContext ToContext: (NSString *) toContext
{

    Boolean success = FALSE;

    NSString *exported = [self exportData: fromContext ToContext: toContext];

    if( exported != nil )
    {

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [self getPushURL]];

        if( request != nil )

        {

           NSURLResponse * response = nil;

           NSError * error = nil;

           [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];

           NSMutableData *postBody = [NSMutableData data];

           [postBody appendData:[exporteddataUsingEncoding:NSUTF8StringEncoding]];

           [request setHTTPBody:postBody];

           [request setHTTPMethod:@"POST"];

           NSData *xmlResult = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];   

           if( xmlResult )

           {
            [self parseXMLResult:xmlResult];

              if([replyStatus isEqualToString:@"true"])

                success = TRUE;
          }
      }

  }

   return success;

}
Pugalmuni
  • 9,350
  • 8
  • 56
  • 97
user530246
  • 11
  • 1
0

looks like you are trying to POST data to the server?

look at this question it might be similar and provide the answer

Sending POST data from iphone over SSL HTTPS

Community
  • 1
  • 1
Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
0

If you use arbitrary data, first hexify it and then use %s. On the server side you can decode it on reception. So basically do a repeated sprintf with format "%02x" and append these. In that case it will survive inside url-strings, after the ? as in your example.

Henno Brandsma
  • 2,116
  • 11
  • 12
  • to comment on myself: I saw the Base64 suggestion below (which is more efficient) but I avoided it because the standard alphabet contains /, but I saw in the link you can specify your own, cool! – Henno Brandsma Aug 05 '10 at 17:37