如何在C ++中创建URL Shortener? [英] How to make a URL Shortener in C++?

查看:152
本文介绍了如何在C ++中创建URL Shortener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中创建一个URL Shortener,它使用API​​来简短地记录api请求是这样的: http://sitename.com/api.php?url=http://urltoshort.com



如何在C ++中创建它因此将要求用户插入长URL,然后按Enter键将其缩短并自动生成返回短网址任何人都可以帮我吗?问候PS我有这个代码:

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:

#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;
}



但请回复 http://sitename.com/api.php?url=http://urltoshort.com 如何使其连接到网站并返回缩短的URL?


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?

推荐答案

对于Windows,请使用WinInet或WinHttp API。



http ://msdn.microsoft.com/en-us/library/windows/desktop/hh227298%28v=vs.85%29.aspx [ ^ ]



WinInet示例:



C ++中的异步HTTP请求WinINet包装器 [ ^ ]



WinHttp例子:



使用C ++的简单Windows HTTP包装器 [ ^ ]



对于Linux(或Windows),请使用libcurl。



http://curl.haxx.se/libcurl/wininet.html [ ^ ]



这个例子应该这样做。



http://curl.haxx.se/libcurl/c/simple.html [ ^ ]
For Windows, use the WinInet or WinHttp APIs.

http://msdn.microsoft.com/en-us/library/windows/desktop/hh227298%28v=vs.85%29.aspx[^]

WinInet example:

An Asynchronous HTTP Request WinINet Wrapper in C++[^]

WinHttp example:

A Simple Windows HTTP Wrapper Using C++[^]

For Linux (or Windows), use libcurl.

http://curl.haxx.se/libcurl/wininet.html[^]

This example should do it.

http://curl.haxx.se/libcurl/c/simple.html[^]


您似乎不理解客户端 - 服务器的概念。



您的程序将播放客户端rol e 服务器请求一些数据。



客户端请求来自服务器的数据。

服务器返回 resopnse (错误或成功加上已恢复的数据)。



为了成功相互交谈,两个角色必须就某些沟通协议达成一致。



请求协议编码在URL以及发送到服务器的一些其他数据。

响应协议基于requestng URL中提到的协议以及发送到服务器的其他数据。 />


请参见 HTTP协议 [ ^ ]了解客户端和服务器之间HTTP通信的一些背景信息。



要实现Web客户端,您可以使用裸机低级功能,也可以依赖在您的环境(Windows,Linux,Unix,...)上,您可以使用一些现有的类/库。



Unix / Linux:参见例如 http://www.binarytides.com/server-client-example-c-sockets-linux / [ ^ ]用于基于C的服务器/客户端代码示例。



CURL:或者您可以使用libcurl,请参阅 http://en.wikipedia.org/wiki/CURL [ ^ ]。



CP:您也可以使用一些已经发明的客户端,例如 C ++中功能齐全的Windows HTTP Wrapper [< a href =http://www.codeproject.com/Articles/66625/A-Fully-Featured-Windows-HTTP-Wrapper-in-Ctarget =_ blanktitle =New Window> ^ ]。



或者在Windows / .Net上,您可以使用 http://msdn.microsoft.com/en-us/library/system.net.webclient(v = vs.110)的.aspx [ ^ ]



干杯

Andi
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


这篇关于如何在C ++中创建URL Shortener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆