Sep 7

libcurl库

A.在windows环境下编译libcurl库

Download Microsoft Platform SDK
========================================================
    http://www.microsoft.com/downloads/details.aspx?FamilyId=A55B6B43-E24F-4EA3-A93E-40C0EC4F68E5&displaylang=en
    Install it


Download Zlib Source
========================================================
    from http://www.zlib.net/
    http://www.zlib.net/zlib123.zip


Build Zlib
========================================================
    Use Visual C++ 6
    zlib123\projects\visualc6\zlib.dsw


Download Openssl Source
========================================================
    from http://www.openssl.org/source/
    http://www.openssl.org/source/openssl-0.9.8g.tar.gz


Build Openssl
========================================================
    maybe need to install active perl
    http://lccnc.skycn.com/down/ActivePerl-5.8.8.822-MSWin32-x86-280952.zip

    # run build shell
    cd openssl-***
    ms\32all.bat


Download Curl Source
========================================================
    from http://curl.haxx.se/
    http://curl.haxx.se/download/curl-7.18.1.zip


Build Curl
========================================================
    1. Pre Build
  
    a. create bat file for debug
    call "C:\Program Files\Microsoft Visual Studio\VC98\Bin\vcvars32.bat"
    set CFG=debug-dll-ssl-dll-zlib-dll
    set OPENSSL_PATH=D:\svn_work\MDS\USS\trunk\openssl-0.9.8g
    set ZLIB_PATH=D:\svn_work\MDS\USS\trunk\zlib123-dll
    nmake -f Makefile.vc6

    b. create bat file for release
    call "C:\Program Files\Microsoft Visual Studio\VC98\Bin\vcvars32.bat"
    set CFG=release-dll-ssl-dll-zlib-dll
    set OPENSSL_PATH=D:\svn_work\MDS\USS\trunk\openssl-0.9.8g
    set ZLIB_PATH=D:\svn_work\MDS\USS\trunk\zlib123
    nmake -f Makefile.vc6

    c. save shell code to bat file
    Save to : curl-***\lib\build.bat

    d. modify curl-***\lib\Makefile.vc6
    Find
        CFLAGS     = /I. /I../include /nologo /W3 /GX /DWIN32 /YX /FD /c /DBUILDING_LIBCURL
    set as
        CFLAGS     = /I. /I../include /nologo /W3 /GX /DWIN32 /YX /FD /c /DBUILDING_LIBCURL  /I"C:\Program Files\Microsoft Platform SDK\Include"

    e. copy zlib123\projects\visualc6\Win32_DLL_Release\zlib1.lib to curl-7.18.1\lib\zdll.lib


    2. Build
    cd curl-***\lib
    build.bat
  

在Windows下使用libcurl、curlpp
<!--[if !vml]--><!--[endif]-->

1. 下载libcurl、curlpp源代码

http://curl.haxx.se/

http://rrette.com/textpattern/index.php?s=cURLpp

例如下载

http://curl.haxx.se/download/curl-7.16.2.tar.bz2

http://rrette.com/files/curlpp/curlpp-0.7/curlpp-0.7.0.tar.gz

2. 下载zlib

http://www.zlib.net/

例如下载

http://www.zlib.net/zlib123-dll.zip

3. 编译OpenSSL

参考Windows下Openssl安装以及编程(Visual C++版)

4. 编译libcurl,以VS2005为例

将curl-7.16.2.tar.bz2解压到某目录,例如C:\curl,进入C:\curl\lib目录。

将zlib123-dll.zip解压到某目录,例如C:\zlib

设Openssl的目录为C:\openssl

进入Visual Studio 2005命令提示,进入C:\curl\lib

编译Debug版本。

set CFG=debug-dll-ssl-dll-zlib-dll

set OPENSSL_PATH=C:/openssl

set ZLIB_PATH=C:/zlib/include

nmake -f Makefile.vc8

其输出:libcurld_imp.lib, libcurld.dll

编译Release版本。

set CFG=release-dll-ssl-dll-zlib-dll

set OPENSSL_PATH=C:/openssl

set ZLIB_PATH=C:/zlib/include

nmake -f Makefile.vc8

其输出:libcurl_imp.lib, libcurl.dll

如果需要编译其他版本,可查看设定相应的CFG 参数即可。

5.编译curlpp

将curlpp-0.7.0.tar.gz解压到某目录,例如C:\curlpp\curlpp

set CFG=release

nmake -f Makefile.msvc

如果需要编译其他版本,可查看设定相应的CFG 参数即可。

需要注意的是可能需要对原文件进行一定的修改。

一种方案是:

修改Makefile.msvc中LIBCURL_PATH

修改dllfct.h关于CURLPPAPI的宏定义

#define CURLPPAPI 

6.测试

该程序是备份CUBlog程序的C++版,需要boost库

#include <iostream>
#include <string>
#include <queue>

#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/tuple/tuple.hpp>

#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>

#pragma comment(lib,"libcurlpp.lib")
#pragma comment(lib,"libcurl_imp.lib")

using namespace std;

#define MAX_FILE_LENGTH 20000

class WriterMemoryClass
{
    public:
    // Helper Class for reading result from remote host
    WriterMemoryClass(){
        this->m_pBuffer = NULL;
        this->m_pBuffer = (char*) malloc(MAX_FILE_LENGTH * sizeof(char));
        this->m_Size = 0;
    }
    ~WriterMemoryClass() {
        if (this->m_pBuffer)
            free(this->m_pBuffer);
    }
    void* Realloc(void* ptr, size_t size) {
        if(ptr)
            return realloc(ptr, size);
        else
            return malloc(size);
    }
    // Callback must be declared static, otherwise it won't link...
    size_t WriteMemoryCallback(char* ptr, size_t size, size_t nmemb) {
        // Calculate the real size of the incoming buffer
        size_t realsize = size * nmemb;
        // (Re)Allocate memory for the buffer
        m_pBuffer = (char*) Realloc(m_pBuffer, m_Size + realsize);
        // Test if Buffer is initialized correctly & copy memory
        if (m_pBuffer == NULL) {
            realsize = 0;
        }
       
        memcpy(&(m_pBuffer[m_Size]), ptr, realsize);
        m_Size += realsize;
        // return the real size of the buffer...
        return realsize;
    }
   
    // Public member vars
    char* m_pBuffer;
    size_t m_Size;
};

int main(int argc,char * argv[])
{
    string list_base="http://blog.chinaunix.net/u/8780/article.php?frmid=0&page=";
    string art_base="http://blog.chinaunix.net/u/8780/showart.php?id=";
    boost::regex rexp("<a href=\"showart_([0-9]{6}).*?><font.*?><b>(.*?)</b></font></a>");
    boost::regex fnp("[/\\:*?\"<>]");
    typedef boost::tuple<string,string> element;
    queue<element> q;
    bool save=true;
    if(argc==1)
        save=false;
       
    try {
        cURLpp::Cleanup cleaner;
        cURLpp::Easy request;
        cURLpp::Easy handler;
       int i=0;
        bool cond=true;
        while(cond) {
            i++;
            cond=false;
            string list_url=list_base+boost::lexical_cast<string>(i);
            cerr<<list_url<<endl;
            // Get the content
            WriterMemoryClass mWriterChunk;
            // Set the writer callback t
 

 

Sep 7

一、              概念

1.         为什么要使用libcurl

1)        作为http的客户端,可以直接用socket连接服务器,然后对到的数据进行http解析,但要分析协议头,实现代理这样太麻烦了。

2)        libcurl是一个开源的客户端url传输库,支持FTPFTPSTFTPHTTPHTTPSGOPHERTELNETDICTFILELDAP,支持WindowsUnixLinux等平台,简单易用,且库文件占用空间不到200K

2.         getpost方式

客户端在http连接时向服务提交数据的方式分为getpost两种

1)        Get方式将所要传输的数据附在网址后面,然后一起送达服务器,它的优点是效率比较高;缺点是安全性差、数据不超过1024个字符、必须是7位的ASCII编码;查询时经常用此方法。

2)        Post通过Http post处理发送数据,它的优点是安全性较强、支持数据量大、支持字符多;缺点是效率相对低;编辑修改时多使用此方法。

3.         cookiesession

1)        cookie
cookie
是发送到客户浏览器的文本串句柄,并保存在客户机硬盘上,可以用来在某个Web站点会话之间持久地保持数据。cookie在客户端。

2)        session
session
是访问者从到达某个特定主页到离开为止的那段时间。每一访问者都会单独获得一个session,实现站点多个用户之间在所有页面中共享信息。session在服务器上。

3)        libcurl中使用cookie
保存cookie, 使之后的链接与此链接使用相同的cookie

a)         在关闭链接的时候把cookie写入指定的文件
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");

b)        取用现在有的cookie,而不重新得到cookie
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");

b)        httphttps的区别

1)        Http是明文发送,任何人都可以拦截并读取内容

2)        Https是加密传输协议,用它传输的内容都是加密过的,httpshttp的扩展,其安全基础是SSL协议

c)        base64编码

1)        为什么要使用base64编码
如果要传一段包含特殊字符比较多的数据,直接上传就需要处理转意符之类的很多问题,用base64编码,它可以把数据转成可读的字串,base64a-z, A-Z, +/总计64个字符组成。

2)        传送base64编码的注意事项
由于base64的组成部分有加号,而加号是url中的转意字符,所以无论是get方式还是post,传到服务器的过程中,都会把加号转成空格,所以在传base64之前需要把base64编码后的加号替换成”%2B”,这样就可以正常发送了。

二、              例程

d)        代码

#include <stdio.h>

#include <curl/curl.h>

bool getUrl(char *filename)

{

    CURL *curl;

    CURLcode res;

    FILE *fp;

    if ((fp = fopen(filename, "w")) == NULL)  // 返回结果用文件存储

         return false;

    struct curl_slist *headers = NULL;

    headers = curl_slist_append(headers, "Accept: Agent-007");

    curl = curl_easy_init();    // 初始化

    if (curl)

    {

         curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");// 代理

         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改协议头

         curl_easy_setopt(curl, CURLOPT_URL,                "http://www.google.com/search?hl=en&q=xieyan0811&btnG=Google+Search&aq=f&oq=xieyan081");

         curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

         res = curl_easy_perform(curl);   // 执行

         curl_slist_free_all(headers);

         curl_easy_cleanup(curl);

    }

    fclose(fp);

    return true;

}

bool postUrl(char *filename)

{

    CURL *curl;

    CURLcode res;

    FILE *fp;

    if ((fp = fopen(filename, "w")) == NULL)

         return false;

    curl = curl_easy_init();

    if (curl)

    {

         curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // 指定cookie文件

         // curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");

         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "&logintype=uid&u=xieyan&psw=xxx86");    // 指定post内容

         curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");

         curl_easy_setopt(curl, CURLOPT_URL, "http://mail.sina.com.cn/cgi-bin/login.cgi");   // 指定url

         curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

         res = curl_easy_perform(curl);

         curl_easy_cleanup(curl);

    }

    fclose(fp);

    return true;

}

int main(void)

{

    getUrl("/tmp/get.html");

    postUrl("/tmp/post.html");

}

e)         编译

        g++ main.cpp -o main -lcurl

Sep 7
switch重新定位一下就可以了。
Sep 7

计算机技术与软件专业技术资格(水平)考试
 
各 地 实 施 机 构 网 址

2006.5.8
地区 单位名称 网址
全国 全国软考办 www.ceiaec.org
北京 北京人事考试中心 www.bjpta.gov.cn  网上报名
上海 上海市职业能力考试院 www.spta.gov.cn
天津 天津电子信息应用教育中心 www.teiaec.org.cn
重庆 重庆市信息产业局 www.cqit.gov.cn
湖北 湖北微机办 www.hbsoft.net
广东 广东省人事考试中心 www.gdkszx.com.cn  网上报名
江苏 江苏省软考办 www.jsiid.gov.cn
浙江 浙江省软考办 www.zjrjks.org  网上报名
湖南 湖南电子信息应用教育中心 www.hniec.org
辽宁 辽宁省信息技术教育中心 www.lnitec.com  网上报名
江西 江西省软考办 www.jxdii.gov.cn
安徽 安徽省人事考试中心 www.apta.gov.cn  网上报名
陕西 陕西省软考办 www.shaanxirk.cn  网上报名
河南 河南省信息产业厅 www.hnrsks.com
河北 河北省职称考试中心 www.hebpta.com.cn  网上报名
新疆 新疆信息化办公室 www.xjxxb.gov.cn
兵团 新疆生产建设兵团信息办软考办 www.btxxb.gov.cn
山东 山东省人事考试中心 www.rsks.sdrs.gov.cn
福建 福建省教育中心 www.info-edu.gov.cn  网上报名
黑龙江 黑龙江省人事考试中心 www.hljeiec.net
黑龙江 黑龙江电子信息应用教育中心 www.hljeiec.net
四川 四川省人事考试中心 www.scpta.gov.cn
吉林 吉林省人事厅职考办 www.jlzkb.com
广西 广西省人事考试中心 www.gxpta.com.cn
甘肃 甘肃省职考中心 www.rst.gansu.gov.cn
贵州 贵州省信息厅软考办 www.gzrj.gov.cn  网上报名
宁波 宁波软考办 www.nbrkb.net
山西 山西省电子信息办 www.sxsia.org.cn
云南 云南省软考办 www.ynxr.com
海南 海南人力资源开发局职称考试部 www.himprec.org.cn
内蒙古 内蒙古人事考试中心  
宁夏 宁夏人事考试中心 www.nxpta.gov.cn  网上报名
青海 青海发展改革委员会信息中心 www.qhei.gov.cn
西藏 西藏人事厅考试中心  
香港 香港泛亚教育中心 www.apec.org.hk
澳门 澳门生产力暨转移中心 www.cpttm.org.mo

  如有网友发现某些省份联系方式有所改变,请留言,谢谢!

分页: 1/1 第一页 1 最后页 [ 显示模式: 摘要 | 列表 ]