測試JSON RPC遠程調用(JSON用戶端)

來源:互聯網
上載者:User

標籤:blog   http   io   os   ar   java   for   sp   div   

#include <string>#include <iostream>#include <curl/curl.h>/*標題:JSon用戶端Author: KagulaLastUpdateDate:2014-05-17描述:測試JSON RPC遠程調用測試環境:Windows 8.1、Visual Studio 2013 SP1         curl-7.36.0 CPPCMS 1.0.4(JSON服務端) Java Servlet (JSON服務端)*/static std::string *DownloadedResponse;static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in){// Is there anything in the buffer?  if (buffer_in != NULL){// Append the data to the buffer    buffer_in->append(data, size * nmemb);// How much did we write?   DownloadedResponse = buffer_in;return size * nmemb;}return 0;}std::string GetJsonByPostMethod(std::string URL,std::string jsonObj){CURL *curl;CURLcode res;curl = curl_easy_init();if (curl){#ifndef _DEBUGcurl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);//Release模式下規定逾時為一秒#endifcurl_easy_setopt(curl, CURLOPT_URL, URL.c_str());curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonObj.c_str());curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, jsonObj.length());//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);curl_slist *plist = curl_slist_append(NULL,"Content-Type:application/json;charset=UTF-8");curl_easy_setopt(curl, CURLOPT_HTTPHEADER, plist);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);res = curl_easy_perform(curl);if (CURLE_OK == res){char *ct;res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);if ((CURLE_OK == res) && ct)return *DownloadedResponse;}}}int main(int argc, char *argv[]){std::string url = "localhost:8080/WageQuery/JsonServer";//"http://localhost:8080/calc";std::string jsonObj = "{\"method\":\"div\",\"params\":[12,4],\"id\":1}";//"{\"method\": \"hello\",\"name\" : \"lijun\"}";std::string  strR = GetJsonByPostMethod(url, jsonObj);std::cout << strR << std::endl;return 0;}

測試JSON RPC遠程調用(JSON用戶端)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.