Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everyone!


Few days ago I decided to use a WebAPI to make a little program. (Steam WebAPI to be exact)

Now, I know you send some sort of URL (request) and it returns an answer in JSON format. (Or xml or vdf in this instance)

I know how to edit this information (I believe I can learn :p) But I have no idea how to request them in C language. (to send a URL and wait for answer)

I got a suggestion to use Python to mess with all this but I'm already kinda familiar with C/C++ so I want to use it. I found this; http://uriparser.sourceforge.net/ [^]. Am I on the right path? Can I fetch *text* from links like this using these Libraries? *http://api.steampowered.com/IEconItems_440/GetSchema/v0001/?key=6D52161D23855A4E3319B63ED165272E[^]

Or is there any way else to do this in C?

Also, let's say I did somehow manage to use the WebApp to get some info. After that how would I send datas? (Like, add a friend or invite a player to a group)


Please point me in the right direction. Thanks a lot in advance!
Posted
Comments
enhzflep 28-Oct-12 13:41pm    
tl;dr - Basically speaking, you just need to put the right info in the header of the request. The server looks at it then decides what to return to you.
Basically, you want to start out with the ability to download a file from a URL. You may then go on to add extra info to the request header, or look into sending a POST request.

There's lots of 'how to download a file in c' type articles here on CP. Here's some code I threw together some time ago for a RSS reader. There's lots of things that could be done better, but the intention was to make the code clear and readable.

Download file in C

1 solution

HTTP is a simple protocol. You make a tcp connection and send a request then wait for the response. You repeat this over and over. In case of HTTP 1.0 you always make new connections for each request while in HTTP 1.1 you can send and receive multiple requests over the same connection (you can even use pipelining but some HTTP clients and severs don't handle that well).

Both a request and a response contains the following things:
- HTTP protocol in use (1.0 or 1.1)
- Request parameters (like url) in case of a request or response status (HTTP status code) in case of a response
- Headers: these are string key/value pairs that contain only ASCII characters (!!!).
- Optional payload (it can be zero bytes or even arbitrary binary data)

Some important notes:
- I don't recommend using unicode strings in the header (because its ASCII according to the standard) however its possible, when both the client and server side is under my control I convert the strings to utf8 and then I url-encode the strings or base64 encode before sending. If the server isn't yours then you don't know how the server handles non-ascii characters or url encoding.
- The requested url and the header part of the request has a size limit in most servers (configurable and different in each server) so don't try to send big data in header key-value pairs or encoded as query string parameters in your url! Only the payload has unlimited size! With POST method you can send key-value parameters in the payload.

If you want to go the C/C++ route then use libcurl as your HTTP client to perform your requests to the server: http://curl.haxx.se/libcurl/c/libcurl-tutorial.html[^]
You can find libraries for a lot of languages for JSON on its website: http://www.json.org/[^]

Independently form your project I highly recommend learning python. Not only because dealing with http is easy in it but because today its one of the best and simplest scripting languages with really good library support and its quite platform independent. I always prototype HTTP server and client behavior in this language. You can use it to implement HTTP server side behavior as cgi or wsgi (by extending well known servers like Apache). If you use only a subset of python (RPython) then you can convert it to binary executables using PyPy.
There should be at least one scripting language in your toolbox and python is a really good choice.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900