Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 卷曲';发出http请求时的奇怪行为(错误400)_C_Http_Curl_Request_Libcurl - Fatal编程技术网

C 卷曲';发出http请求时的奇怪行为(错误400)

C 卷曲';发出http请求时的奇怪行为(错误400),c,http,curl,request,libcurl,C,Http,Curl,Request,Libcurl,我有这样的代码,可以向Yandex Translate API发出http请求: const char url[] = "https://translate.yandex.net/api/v1.5/tr.json/translate"; const char key[] = "secret.key.here"; char buf[4096] = { 0 }; char input[1024] = "Hello world. H"; snprintf(buf, sizeof(buf), "%s?k

我有这样的代码,可以向Yandex Translate API发出http请求:

const char url[] = "https://translate.yandex.net/api/v1.5/tr.json/translate";
const char key[] = "secret.key.here";
char buf[4096] = { 0 };
char input[1024] = "Hello world. H";
snprintf(buf, sizeof(buf), "%s?key=%s&lang=ru&text=\"%s\"", 
         url, key, input);

struct string response;
init_string(&response);

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, buf);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
    res = curl_easy_perform(curl);
    if(res != CURLE_OK) {
        fprintf(stderr, "curl_easy_perform() failed: %s\n", 
                curl_easy_strerror(res));
    }
    curl_easy_cleanup(curl);
}

printf("%s\n", response.data);
执行
snprintf
后,
buf
包含这样的url:

https://translate.yandex.net/api/v1.5/tr.json/translate?key=secret.key.here&lang=ru&text="Hello world. H"
响应后,
响应。数据包含:

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>

400错误请求
400错误请求

nginx/1.6.2
但如果我重新分配
字符输入[1024]=“你好,世界。”(不带“H”),在http请求后,我得到正确的响应:
{“code”:200,“lang”:“en-ru”,“text”:[“\”БаааСааа,Мааа!\”]}


有什么问题吗?

您需要对输入文本进行编码,以便在URL中使用。试试这个卷发


您是否尝试发送其他非英语文本。该网站可能使用HTTP响应代码400来告诉您必须以实际英语发送,否则无法翻译。因此,它与旋度本身无关。@TJohnson
echo-n“Hello world.T”|/翻译{“code”:200,“lang”:“en-ru”,“text”:[“\”Сааааааааааа,аа1072。终端截图。然而,当我通过浏览器发出相同的请求时,我得到了代码200。
char *input = curl_easy_escape(curl, "Hello world. H", 0);