繁体   English   中英

通过cURL查询时如何管理空格

[英]How to manage whitespaces when querying through cURL

我正在使用cURL从服务器获取信息。 这是我得到的查询URL "http://md5.jsontest.com/?text=Fri Jun 13 2014 07:41:27 GMT+0300 (EEST)"当直接通过浏览器访问时,我得到如下正确响应

{
   "md5": "c22f2d0c39cb6c9f15c170fbedfab634",
   "original": "Fri Jun 13 2014 07:41:27 GMT 0300 (EEST)"
}

但是,当我尝试使用cURL示例时,出现406错误。 下面是我的示例代码

int main(void)
{
  CURL *curl;
  CURLcode res;
int data2len;
char* temp = "http://md5.jsontest.com/?text=Fri Jun 13 2014 07:41:27 GMT+0300 (EEST)";

  curl = curl_easy_init();
  if(curl) {

    curl_easy_setopt(curl, CURLOPT_URL,temp );
    /* example.com is redirected, so we tell libcurl to follow redirection */ 
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    /* Perform the request, res will get the return code */ 
   res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}

请帮助! 提前致谢。

只能对参数值进行编码。您可以通过两种方式进行编码。

一种方法是替换

char* temp = "http://md5.jsontest.com/?text=Fri Jun 13 2014 07:41:27 GMT+0300 (EEST)";

char* temp = "http://md5.jsontest.com/?text=Fri%20Jun%2013%202014%2007:41:27%20GMT+0300%20%28EEST%29";

另一种方法是使用curl_easy_escape方法。

int data2len;
char temp[1024]="";
strcat(temp,"http://md5.jsontest.com/?text=");
char* param1="Fri Jun 13 2014 07:41:27 GMT+0300 (EEST)";
curl = curl_easy_init();
if(curl) {
    strcat(temp,curl_easy_escape(curl,param1,strlen(param1)));
    curl_easy_setopt(curl, CURLOPT_URL,temp ); 

谢谢并恭祝安康。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM