0

I have an object with the following members:

NSString *reqStr = "param1=val1&param2=val2&param3=val3&..";
NSData *imageData = [NSJPEGRepresentation (myimage)];
NSData *fileContents = [NSData initWithFileContents(myfile.txt)];

How can I send this out to Windows WCF? Do I send it as stream of bytes, and attach to httprequest? Or, will this be sockets? I am not sure how to pack these things as one thing as in one stream of bytes or whatever it may be the way to format such an object.

Any help?

User97693321
  • 3,336
  • 7
  • 45
  • 69
software2007
  • 51
  • 1
  • 7
  • How do you mean send it to Windows ? are you sending an SMS, an Email, a HTTP post ? – Russ Clarke Jul 03 '12 at 00:33
  • correct, I would like to send httpRequest post to WCF on windows. – software2007 Jul 03 '12 at 00:35
  • What is the WCF endpoint configured as, binaryMessageEncoding, textMessageEncoding or mtomMessageEncoding ? – Russ Clarke Jul 03 '12 at 00:43
  • 1
    It would have to be binaryMessageEncoding.I can play with it on WCF end as long as I know how to get it out of iPhone as one thing. Right now, I know how to send reqStr as JSON and imageData attached to body of httprequest, I do it seperately though. I ould like to send everything as one message. My app will have several parameters including a txt file and an image or two that I would like to post to a sql database on windows server. So, I thought I would send this object message to WCF. – software2007 Jul 03 '12 at 00:50
  • If you want to send everything together then you either need to POST the data and mimic a HTML form posting, or you'd want to send a SOAP packet that has all the bits of data you want to bundle; I suspect the HTTP method is probably the best and you don't necessarily need WCF, consider lighterweight frameworks such as www.nancyfx.org. The Question I linked to in my answer below should give you the info you need to achieve this in IOS anyway. – Russ Clarke Jul 03 '12 at 00:58
  • NancyFX would replace the WCF service; not the IOS portion. – Russ Clarke Jul 03 '12 at 00:59

2 Answers2

1

Since you said that you already know how to send imageData which is NSData. Why not convert regStr to NSData and combine all three together as a single data and send.

To convert NSString to NSData:

NSData* strData=[regStr dataUsingEncoding:NSUTF8StringEncoding];

And use NSMutableData's appendData method to combine all three.

NSMutableData *combineData = [[NSMutableData alloc]initWithData:strData];
[combineData appendData:imageData];
[combineData appendData:fileData];
user523234
  • 14,323
  • 10
  • 62
  • 102
  • I guess this was my original question. So, are you saying I can do this? NSdata *strData = [...], NSData *imageData = [...], NSData *fileData = [...], then I can do? bodydata.append(strdata); bodyData.Append(imageData); bodyData.Append(fileData)....? – software2007 Jul 03 '12 at 01:17
0

This S/O question might help:

File Upload to HTTP server in iphone programming

If the WCF endpoint is configured as HTTP then the same principles should apply regarding the multipart/formencoding of the image in question.

Edit:

Is this answer more helpful ?

HTTP "POST" request in iOS

Community
  • 1
  • 1
Russ Clarke
  • 17,511
  • 4
  • 41
  • 45
  • This doesn't really answer my question. I know how to upload an image or a file. I would like to know how to send the reqString &image &file all in one request? How is this done in iOS? I would think it would be a stream of bytes somehow. – software2007 Jul 03 '12 at 00:58
  • In particular, the answer by Jane Sales looks pretty useful and pain free. – Russ Clarke Jul 03 '12 at 01:00
  • Added a different URL for you. – Russ Clarke Jul 03 '12 at 01:03
  • ASIHttpRequest API doesn't work for iOS5 and no longer supported. – software2007 Jul 03 '12 at 01:04
  • The 2nd link I've given shows you how to create a HTTP request and encode the various parameters into one string which you need to send to the server; That's a shame about ASI, it looked quite helpful. – Russ Clarke Jul 03 '12 at 01:05
  • It is a shame. I will try the stuff from the second link, it looks a bit more helpful. Do you know a bit about the WCF side? am thinking to create a Stream wrapper and may be take advantage of binaryreader and binaryWriter. – software2007 Jul 03 '12 at 01:13
  • I've done a bit but in the spirit of this site it'd be better if you could ask that as a new question; that way if someone comes to the site searching for WCF problems, there's a related question straight away; thanks! – Russ Clarke Jul 03 '12 at 01:16