gpt4 book ai didi

CURLOPT_WRITEFUNCTION 获取xml内容

转载 作者:太空狗 更新时间:2023-10-29 15:05:57 30 4
gpt4 key购买 nike

我用过

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

size_t write_data(void *ptr, size_t size, size_t count, void *stream)
{
printf("%*.*s", size * count, size * count, ptr);
}

要获得以下输出,但我只需要获得正文内容

* About to connect() to 10.10.1.112 port 8081 (#0)
* Trying 10.10.1.112... * connected
* Connected to 10.10.1.112 (10.10.1.112) port 8081 (#0)
> POST /iit_mobile/services/application?wsdl HTTP/1.1
Host: 10.10.1.112:8081
Accept: */*
Content-type:application/soap+xml; charset=utf-8; action="http://wsdlclass.wsdlcreat.sims.test.com/userloginMethod"
Content-Length: 629

< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Content-Type: application/soap+xml;charset=utf-8
< Transfer-Encoding: chunked
< Date: Tue, 11 Jun 2013 13:22:35 GMT
<
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 338 0 338 0 0 36 0 --:--:-- 0:00:09 --:--:-- 0* Connection #0 to host 10.10.1.112 left intact

* Closing connection #0
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><userloginMethodResponse xmlns="http://wsdlclass.wsdlcreat.sims.test.com"/></soapenv:Body></soapenv:Envelope>

代码:

curl = curl_easy_init();
if(curl)
{

out_fd = fopen (filename, "w");
curl_easy_setopt(curl, CURLOPT_FILE, out_fd);

headers = curl_slist_append(headers, "Content-type:application/soap+xml; charset=utf-8; action=\"http://wsdlclass.wsdlcreat.sims.test.com/login\"");
curl_easy_setopt(curl, CURLOPT_URL, tmpbuff);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);


curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, buffer_size);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buffer);

curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, Timeout);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER,errmsg);

printf("The Server%s:Performing Transaction.....\n",__func__);
res = curl_easy_perform(curl);
printf("res=after culreasey perform%d\n",res);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);

fclose(out_fd);
}

最佳答案

默认情况下,标题不应与正文数据一起写出。如果您在 write_data 回调中接收 header ,这可能意味着您设置了 CURLOPT_HEADER 选项或 CURLOPT_WRITEHEADER 选项。为了安全起见,您可以尝试重置这两个:

curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, 0L);

如果您仍然想要检索 header ,但只是不在 write_data 回调中,您可以像这样为 header 数据设置一个单独的回调:

curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_header);

其中 write_header 是一个回调函数,很像您的 write_data 函数。

size_t write_header(void *ptr, size_t size, size_t count, void *stream)
{
printf("HEADER: %*.*s", size * count, size * count, ptr);
return count*size;
}

请注意,返回从这些回调函数写入的字节数很重要,否则 curl 会将其视为错误。

如果这仍然不适合您,我能想到的唯一其他解释是您的服务器在 header 数据之前返回一个空行,使 header 看起来好像实际上是正文的一部分。

通过针对已知的有效 URL(例如 http://www.example.com/)进行测试,您可以很容易地看出是否属于这种情况。如果它在那里正常工作,那么您就知道错误出在您的服务器代码中,而不是客户端代码中。

虽然查看了您的完整代码,但您在输出中看到的所有额外内容都来自 CURLOPT_VERBOSE 选项(您已设置两次)和 CURLOPT_NOPROGRESS 选项。只需将它们注释掉,您应该会得到一个干净利落的响应,只有内容正文。

我还应该提到,设置 CURLOPT_FILE 没有意义,除非您要在 write_data 回调中使用 stream 参数.当您设置 CURLOPT_WRITEFUNCTION 回调时,它会替换默认输出函数,因此除非您自己这样做,否则不会将任何内容写入 CURLOPT_FILE 流。

关于CURLOPT_WRITEFUNCTION 获取xml内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17045241/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com