Posting an image and textual based data to a wcf service - wcf

I have a requirement to write a web service that allows me to post an image to a server along with some additional information about that image.
I'm completely new to developing web services (normally client side dev) so I'm a little stumped as to what I need to look into and try.
How do you post binary data and plain text into a servic?
What RequestFormat should I use?
It looks like my options are xml or json. Can I use either of these?
Bit of a waffly question but I just need some direction rather than a solution as I can't seem to find much online.

After reading this guide to building restuful services I figured I was going about the problem the wrong way. The image and text are actually two seperate resources and so should probably be handled seperately. I now have a service that uploads an image and returns a uri to that image and a seperate service to post textual data relating to that image along with the uri to that image.

Though I don't have experience with WCF, I can tell you a painless way to handle POSTing/PUTting of binary data in a REST API (especially one with a mix of text + binary) is to encode the binary data as base64 and treat it much like any other text data in your API.
Yes, there is a slight overhead with base64 in terms of size and an additional encode/decode process, however, base64 is typically only 1.37x larger than binary.
I find in many cases the overhead is well worth avoiding the pain that can be involved with binary data in APIs, especially when you need to POST/PUT a combination of binary and text data. If you wanted to POST an image and additional meta/text data you could easily do so with a json string ("image" would be your base64 encoded image)...
{
"image":"aGVsbG8...gd29ybGQ=",
"user" : 1234,
"sub_title": "A picture from my trip to Pittsburgh"
}
This is by no means the best solution for all cases, and as I said, I'm not a WCF expert, but it's certainly something to consider in the general case to make your life easier.

If you are using WebServiceHost in WCF 3.5 then you need to read this. If you have to use WCF to do HTTP based stuff then try and get onto .Net 4. I believe they have made a whole lot of things much easier.
If you are stuck with 3.5, welcome to a world of pain. Find everything you can written by Aaron Skonnard on the subject. And as I suggested in the comments of the other question, learn how to use SvcTrace.

Related

Uploading binary files to a REST API via VBA

I am in the process of using VBA in SolidWorks' to integrate Autodesk's PLM 360 into SolidWorks. The problem is that I have very little experience interacting with REST APIs, and it seems like uploading files to a cloud application is probably the most complicated part of that. In any case, it is very important for me to be able to do two things with VBA from SolidWorks: (1) add images to an item's description (documentation), and (2) upload and attach files to an associated item (documentation).
In any case, the PLM 360 documentation explains things fairly clearly, and this blog post (specifically, the approach using WinHttp.WinHttpRequest.5.1) seems to do a decent job of explaining the VBA side, but I'm struggling to figure out which things are important from each of the given examples.
Here are the main things that are puzzling me at the moment:
The PLM 360 documentation seems to indicate that one request is being sent in two parts, but I don't understand how to do that in VBA.
The code in the blog post surrounds the binary file with STR_BOUNDARY. Is that necessary in all cases, or is that something that's necessary only for some APIs?
The code in the blog post includes the Content-Disposition and a few other things in the binary portion. Again, is this necessary in all cases, or is that something that's necessary only for some APIs?
The caveat to the pvToByteArray function is confusing to me. I understand that it is converting back to a byte array the string containing the file data, but if #3 above is not necessary in PLM 360, is it necessary to convert the file to a string and then back to a binary "blob", or does that result in it being possible to pass baBuffer without failure?
How do I include the rest of the information in the request for creating the item? I suppose this is somewhat related to #1 above, but I don't understand how to send API calls in two parts.
Ultimately, I'm posting this question here because I don't think people on the SolidWorks forum would have enough experience with REST APIs to be able to help, and experience shows that the people in the PLM 360 forum don't have enough experience with VBA to be able to help. StackOverflow seems like the place with the best chance of having people with experience in both areas.

Hacking the source code of iOS app

I have developed an app for iPhone which communicates with a web server though XML based communication model.
In one of my source files, NetworkLayer, I created XML objects and send them to the web server. I have also declared all constants used in my app and also URLs (used to access my web server) in MyApp_Prefix.pch.
I want to ask if there is any way that some hacker can get access to my source code of generating XML objects or MyApp_Prefix.pch file if he has .app file of my app? Can anyone please help me in this regard?
No, he can't get your source code. But he can look at the HTTP requests and responses to see what XML you have created and what the server has sent back. Does that matter?
A hacker could reverse-engineer your code with some effort by looking at what your code tells the device to do. With some effort and knowledge of assembly and reverse engineering, one can see much of what your code contains. This does however require some serious effort and lots of time, so for most apps, it is unlikely that anyone would attempt to do so.
A much easier way would be to intercept the data on it's way to or from the server, and unless you are obfuscating the data, encrypting it or using SSL, you can't prevent this.
If you are worried about protecting your data, you should try some simple obfuscation. There are many ways to do this, the most popular one being XOR:ing your data with a key both the client and the server knows. Applying the key will flip the bits in your data and quickly and easily turn it into unreadable gibberish. Applying the same key again will flip the same bits again and you have perfectly readable XML.
It should be noted that XOR Encryption is quite possible and relatively easy to crack, especially since the key has to be stored as a part of the application, but it requires lots of time and effort to break through and doesn't qualify as encryption legally (eg. you shouldn't need to go through the whole Encryption Export thing when releasing the app), while still keeping the data gibberish-y enough to throw off most people - which is usually enough, unless your data is really sensitive, eg. if you're transferring payment credentials or similar.

how write XML for any website

I am doing Objective C programming and I want send and receive requests(Login/data fetching) over the client/server.
Now the problem is Should I do it using XML or any other Method.
Also I know nothing how to write XML for any particular website.
I am hassling for many days.Can anybody help?
XML particularly SOAP is very bloated and the support in Objective-C is severely lacking. I would recommend JSON for lightweight use and in fact Apple use it for their Push Notification server.
If you DO want SOAP then check sudzc.com for an online objective-c generator from a WSDL.
There's no such animal as "any particular website." Some return data in HTML, RSS, ATOM, or JSON format, others may use a custom XML schema all their own. Likewise with the data you send; they may expect requests via SOAP or HTTP, with any type and number of inputs the creators chose.
In short, you need to find out exactly what is expected by the one particular site with which you're trying to communicate, and give it what it wants. That's why programmers get paid the big bucks, because there's no easy "do what I mean" button. :-)
Your question is whether you should use xml or any other method of communication.
XML has been designed to standardize communication, which is especially handy for communicating between several parties, as the structure of the document can be formally written down in a document and can be validated, so there is no discussion afterwards about the syntax of the document.
Although that is a noble idea, XML is relatively complex and not as light weight as, for example, json.
As long as you are writing your own client that communicates to your own server, the protocol used between those 2 can be anything, and do not need to be XML.
Therefore I would suggest using a lightweight and easy to understand protocol. Json is gaining popularity due to its simplicity.
If you have control over the output of the data on your server, I'd suggest you output the data directly as a plist. Plists are native dictionary objects that can be directly instantiated with [NSDictionary dictionaryWithContentsofURL:].
Take a look at the PList programming guide for proper formatting.

GWT Data Serialization

I'm looking for the algorithm that Google's Web Toolkit uses to serialize data posted to the server during an AJAX request. I'm looking to duplicate it in another language so that I can tie in another of my projects with a GWT project.
Any help is much appreciated!
The GWT-RPC serialization is heavily tied to Java. It even sends Java class names over the wire.
I suggest you use something like JSON to communicate with the server. This way, you can use any programming language with the GWT server.
Update: There are no definitive references to the GWT-RPC format, and a mailing list post explains that decision:
The GWT RPC format is intentionally opaque JSON. This makes it
somewhere between difficult and impossible to add a non-GWT agent to
the RPC discussion. There isn't really a nice work-around for
creating a non-Java server-side implementation but, because your
RemoteServiceServlet implementation just has to implement your
synchronous RPC interface, it's quite possible for non-GWT clients to
talk to the same server-side business logic, just without using the
RPC protocol.
and the little detail which surfaced was
The wire format is plain text. It's actually JSON. It's just
unreadable JSON because the assumption is that both the producing and
consuming code is auto-generated and can make all kinds of assumptions
about the structure of the text.
I've written a design document explaining the GWT-RPC wire format. Hopefully you'll find it useful.

Is it advisable to send/return data in .plist format to/from an API being accessed via iOS?

We're deciding between using JSON vs. Property List (binary) for our API, which will be accessed by iPhone/iPad/iPod Touch.
Are there any speed advantages?
The server guys are going to understand JSON better.
Plists work really well and have much, much better type safety. The real issue you'll run into with JSON is someone server side adds a few quotes around a number and suddenly your app is crashing.
But, JSON is compact, easy to read (unlike binary plists), and as noted is really well understood. So just be very careful in the parsing code, and try out JSON.
According to Sam Soffes, JSON.
edit: he talks about xml-based plists, not binary plists. Either way, json will typically be easier to generate from web based api's.