curl post 中文內容及請求頭信息的修改方法

    curl_slist *plist = curl_slist_append(NULL, "Content-Type: application/x-www-form-urlencoded; charset=UTF-8");

curl post 中文內容及請求頭信息的修改方法


    //--------------------------------------------------
    std::string xml_data = "";//"111";
    KeyValues::iterator i;

    for (i = kv.begin(); i != kv.end(); i++)
    {
        xml_data += i->first + "=" + i->second + "&";
    }

    curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
    curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, xml_data.c_str());

    curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, true);//應當是根據 location 自動再次請求

    //--------------------------------------------------
    //字符集//默認中文 gb2312
    //增加HTTP Header
    //   curl_slist *plist = curl_slist_append(NULL, "Client-Key:m-5be02cd9ddfb11dcaf9700142218fc6e");
    //   curl_slist_append(plist, "username:[email protected]");
    //   curl_slist_append(plist, "password:123456");
    //   curlRet = curl_easy_setopt(m_hCURL, CURLOPT_HTTPHEADER, plist);

    //curl_slist *plist = curl_slist_append(NULL, "Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
    curl_slist *plist = curl_slist_append(NULL, "Content-Type: application/x-www-form-urlencoded; charset=GB2312");
    //curl_slist_append(plist, "username:[email protected]");
    //curl_slist_append(plist, "password:123456");
    curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, plist);

    //--------------------------------------------------

這樣就可以將中文 post 過去了,至少 c# 的        string s = this.Request["name"];  可以bcgc中文了.

--------------------------------------------------

http://blog.csdn.net/collin1211/article/details/3023757


CURL常見問題
分類: 網絡編程備忘 2008-10-06 20:50 198人閱讀 評論(0) 收藏 舉報
 CURL的中文資料比較少,下面是實際工作中用到,摸索出來的,記錄之。

1、增加HTTP Header
   curl_slist *plist = curl_slist_append(NULL, "Client-Key:m-5be02cd9ddfb11dcaf9700142218fc6e");
   curl_slist_append(plist, "username:[email protected]");
   curl_slist_append(plist, "password:123456");
   curlRet = curl_easy_setopt(m_hCURL, CURLOPT_HTTPHEADER, plist);
   這樣即可在HTTP Header中加入上面的內容。

2、增加Post Form的數據
   curlRet = curl_easy_setopt(m_hCURL,CURLOPT_POSTFIELDS, "Client-Key=m-5be02cd9ddfb11dcaf9700142218fc6e&[email protected]&password=123456");
   像上面那樣,可以在Post表單中加上任意數據。

3、讓CURL記錄Cookie
   curlRet = curl_easy_setopt(m_hCURL, CURLOPT_COOKIEFILE, "");
   curlRet = curl_easy_setopt(m_hCURL, CURLOPT_COOKIEJAR, "");
   像上面那樣設置一下,試驗中發現不需要指定cookie文件名它也能工作,具體這兩個設置有沒會差別,暫不清楚,互聯網上也有人提問此問題。

--------------------------------------------------

總的來說 curl 提供的接口還是太過簡陋了.







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章