加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

CURLOPT_WRITEFUNCTION获取xml内容

发布时间:2020-12-15 23:52:36 所属栏目:百科 来源:网络整理
导读:我用过 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,ptr);} 获得以下输出,但我只需要获取正文内容 * About to connect() to 10.10.1.1
我用过
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,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,write_data);


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

    curl_easy_setopt(curl,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%dn",res);
    curl_slist_free_all(headers);
    curl_easy_cleanup(curl);

    fclose(out_fd);
}
默认情况下,不应使用正文数据写出标题.如果您在write_data回调中接收标头,则可能意味着您已设置CURLOPT_HEADER选项或CURLOPT_WRITEHEADER选项.你可以尝试重置这两个是安全的:
curl_easy_setopt(curl,CURLOPT_HEADER,0L);
curl_easy_setopt(curl,CURLOPT_WRITEHEADER,0L);

如果您仍然想要检索标题,但只是不在write_data回调中,则可以为标题数据设置单独的回调,如下所示:

curl_easy_setopt(curl,CURLOPT_HEADERFUNCTION,write_header);

write_header是一个回调函数,就像你的write_data函数一样.

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

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

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

您可以通过针对已知的有效网址(例如http://www.example.com/)进行测试来轻松查看是否属于这种情况.如果它在那里正常工作,那么你知道故障在你的服务器代码中,而不是客户端代码.

但是,看了你的完整代码之后,你在输出中看到的所有额外的东西都来自CURLOPT_VERBOSE选项(你已经设置了两次)和CURLOPT_NOPROGRESS选项.只是评论出来,你应该得到一个很好的干净的回应除了内容正文.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读