[英]How to manage whitespaces when querying through cURL
I am using cURL to fetch information from servers. 我正在使用cURL从服务器获取信息。 This is the query URL I get
"http://md5.jsontest.com/?text=Fri Jun 13 2014 07:41:27 GMT+0300 (EEST)"
When this is directly hit through browser I get proper response as below 这是我得到的查询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)"
}
But When I try it with cURL example i get 406 error. 但是,当我尝试使用cURL示例时,出现406错误。 Below is my sample code
下面是我的示例代码
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;
}
Kindly help !! 请帮助! Thanks in advance.
提前致谢。
Only the parameter values should be encoded.you do it 2 ways. 只能对参数值进行编码。您可以通过两种方式进行编码。
one way is replace 一种方法是替换
char* temp = "http://md5.jsontest.com/?text=Fri Jun 13 2014 07:41:27 GMT+0300 (EEST)";
with 与
char* temp = "http://md5.jsontest.com/?text=Fri%20Jun%2013%202014%2007:41:27%20GMT+0300%20%28EEST%29";
other way is using the curl_easy_escape method. 另一种方法是使用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 );
Thanks and Regards. 谢谢并恭祝安康。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.