5

我用过

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);
}
4

1 回答 1

3

默认情况下,标题不应与正文数据一起写出。如果您在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, size_t size, size_t count, void *stream)
{
    printf("HEADER: %*.*s", size * count, size * count, ptr);
    return count*size;
}

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

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

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

但是,在查看了您的完整代码后,您在输出中看到的所有额外内容都来自CURLOPT_VERBOSEoptions(您已设置两次)和CURLOPT_NOPROGRESSoption. 只需将它们注释掉,您应该会得到一个很好的干净响应,除了内容主体之外什么都没有。

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

于 2013-06-14T16:31:01.270 回答