Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to make a URL Shortener in C++ that uses an API to short url the api request is this: http://sitename.com/api.php?url=http://urltoshort.com

How can I make it in C++ So the user will be asked to insert the long url and then press Enter to short it and there will be automatically return the short URL Anyone can help me please? Regards P.S I have this code:
C++
#include <iostream>
using namespace std;
int main ()
{
    string longurl, shorturl;
    cout<<"Please insert long URL"<<endl;
    cin>>longurl;
    shorturl = "http://sitename.com/api.php?url="+longurl;
    cout<<"Short URL:"<<shorturl<<endl;
    system("pause");
    return 0;
}

But return me http://sitename.com/api.php?url=http://urltoshort.com How can I make it to connect to the website and return me the shorted URL?
Posted
Updated 31-Jan-14 9:46am
v2
Comments
Philippe Mori 31-Jan-14 22:17pm    
URL shortening is a service that some web site provide. So you simply have to connect to one of those site, send the information and then interpret the answer. For more détails some of the solution belows might help.

You seem not to understand the concept of client-server.

Your program would play the client role that requests some data from the server.

The client requests data from the server.
The server returns a resopnse (error or success plus resuested data).

In order to successfully talk to each other, both roles must agree on some communication protocol.

The request protocol is encoded in the URL plus some additional data sent to the server.
The response protocol is based on the protocol mentioned in the requestng URL plus the additional data sent to the server.

See HTTP Protocol[^] for some background on HTTP communication between client and server.

To implement a web client, you may work on the bare low level functions, or depending on your environment (Windows, Linux, Unix, ...) you may use some existing classes/libraries.

Unix/Linux: see e.g. http://www.binarytides.com/server-client-example-c-sockets-linux/[^] for C based server/client code example.

CURL: or you might use the libcurl, see http://en.wikipedia.org/wiki/CURL[^].

CP: you might also use some already invented client, e.g. A Fully Featured Windows HTTP Wrapper in C++[^].

Or on Windows/.Net, you might use http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.110).aspx[^]

Cheers
Andi
 
Share this answer
 
 
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