请选择 进入手机版 | 继续访问电脑版
查看: 407|回复: 0

[运维管理] c代码样例-S05GK接口发送短信

715

主题

715

帖子

2162

积分

猿er

Rank: 1

积分
2162
发表于 2016-8-14 17:50:13
S05GK接口发送短信c代码样例
样例参考自速达移动(sudas.cn)接口说明,是通过Libcurl实现与接口的传输,提交相关短信内容和接收手机号码,获取提交状态。Libcurl为一个免费开源的,客户端url传输库,在curl_easy_setopt函数设置option,可以设置ftp,http,get,post等许多选项。

调用curl库访问接口样例:
  1. #include "curl/curl.h"
  2. #pragma comment(lib, "libcurl.lib")
  3. long writer(void *data, int size, int nmemb, string &content);
  4. bool CurlInit(CURL *&curl, const char* url,string &content);
  5. bool GetURLDataBycurl(const char* URL, string &content);
  6. void main()
  7. {
  8. char *url ="http://sdk.sudas.cn/SmsMmsWebService/Service.asmx/g_Submit?";
  9. string content;
  10. if ( GetURLDataBycurl(url,content))
  11. {
  12. printf("%s\n",content);
  13. }
  14. getchar();
  15. }
  16. bool GetURLDataBycurl(const char* URL, string &content)
  17. {
  18. CURL *curl = NULL;
  19. CURLcode code;
  20. string error;
  21. code = curl_global_init(CURL_GLOBAL_DEFAULT);
  22. if (code != CURLE_OK)
  23. {
  24. printf( "Failed to global init default [%d]\n", code );
  25. return false;
  26. }
  27. if ( !CurlInit(curl,URL,content) )
  28. {
  29. printf( "Failed to global init default [%d]\n" );
  30. return PM_FALSE;
  31. }
  32. code = curl_easy_perform(curl);
  33. if (code != CURLE_OK)
  34. {
  35. printf( "Failed to get '%s' [%s]\n", URL, error);
  36. return false;
  37. }
  38. long retcode = 0;
  39. code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retcode);
  40. if ( (code == CURLE_OK) && retcode == 200 )
  41. {
  42. double length = 0;
  43. code = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD , &length);
  44. printf("%d",retcode);
  45. FILE * file = fopen("1.gif","wb");
  46. fseek(file,0,SEEK_SET);
  47. fwrite(content.c_str(),1,length,file);
  48. fclose(file);
  49. //struct curl_slist *list;
  50. //code = curl_easy_getinfo(curl,CURLINFO_COOKIELIST,&list);
  51. //curl_slist_free_all (list);
  52. return true;
  53. }
  54. else
  55. {
  56. // debug1( "%s \n ",getStatusCode(retcode));
  57. return false;
  58. }
  59. curl_easy_cleanup(curl);
  60. return false;
  61. }
复制代码


回复

使用道具 举报