Poco库使用

下载编译 Index of /releases

以运行调试HTTP库为例,使用vs2010打开samples解决方案(打开解决方案默认是编译32位,添加64位)

添加Foundation项目,并进行编译

 修改输出目录..\..\..\bin64

就可以编译运行了

OutPut Directory ,它的值不会直接影响到你文件的输出,但它会间接的影响其他输出,比方说默认值中包含有$(OutDir)

Linker->General->OutPut File,输出文件,虽然填的是exe的路径,但.ilk文件会按这个路径输出.Linker->Debugging->Genrerate Program Database File,输出的pdb文件

发送Http请求

如果需要JSON库,先添加后编译, 同时添加头文件目录  ..\..\..\JSON\include

案例如下,因为工程是unicode编码的

#include "Poco/JSON/Parser.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/StreamCopier.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/JSON/Query.h"
#include "Poco/JSON/PrintHandler.h" 
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/StreamCopier.h"
#include "Poco/Net/NetException.h"
#include "Poco/Net/HTMLForm.h"
#include "Poco/URI.h" 
#include <iostream> 
using namespace Poco::Dynamic;
using namespace Poco;
using std::string;
using namespace Poco::JSON;
using namespace Poco::Net;
 
int main()
{
    try {
        std::string strUrl("http://www.weather.com.cn/data/sk/101010100.html");
        Poco::URI uri(strUrl);       
 
        HTTPClientSession session(uri.getHost(), uri.getPort());
        HTTPRequest request(HTTPRequest::HTTP_GET, uri.getPathAndQuery());
        session.sendRequest(request);
        HTTPResponse response;
        std::istream &is = session.receiveResponse(response);
        const HTTPResponse::HTTPStatus &status = response.getStatus();
        if (HTTPResponse::HTTPStatus::HTTP_OK == status) {
            std::string result;
            StreamCopier::copyToString(is, result);
            std::cout << result << result.size() << std::endl;
            
        } else {
            std::cout << status << std::endl;
        }
        
 
    } catch (Poco::Exception &e) {
        std::cout << e.message() << std::endl;
    }
	char ch;
	std::cin >> ch;
    return 0;
}

1. 在引用的Poco的工程中,不能添加Poco的头文件路径 如: ../../include/Poco, 直接添加 ../../include/就可以了

    使用头文件的时候 #include "Poco/ThreadPool.h"

2.http获取服务端返回的header信息

HTTPClientSession &httpClientSession;

istream& rs = httpClientSession.receiveResponse(response);

if (reponse.has("signature"))
{
        string strSign = reponse.get("signature");
}

3.openssl加解密的时候,已经将openssl的头文件拷贝到VS工程路径下,但是遇到 #include <openssl/rsa.h>这样的头文件无法找到,

可以在VS工程中添加  .\

4.HTTPServerRequest获取get的参数

HTMLForm form(request, request.stream());
NameValueCollection::ConstIterator it;
NameValueCollection::ConstIterator end;
if (!form.empty())
{

it = form.begin();
end = form.end();
for (; it != end; ++it)
{
cout << it->first << ": " << it->second << endl;
}

}