将命令行cURL转换为C cURL

3

我以前没有使用过curl,所以需要一些帮助。我尝试从示例中学习,但无法理解!

我有一个curl命令,可以在Windows命令行上成功运行,将pdf文件索引到Solr中。

我需要将此curl命令合并到C客户端中。

如何将这个curl命令翻译成可以在C cURL客户端中运行的命令呢?

curl "http://localhost:8983/solr/update/extract?literal.id=doc2&uprefix=attr_&fmap.content=attr_content&commit=true"" -F "myfile=@doc.pdf"


你打算从你的C客户端调用cURL可执行文件,还是使用libcurl? - Daniel Gehriger
是的,我打算从我的C客户端调用cURL可执行文件。 - prasad
2个回答

12

在2015年FOSDEM上,curl的作者-Daniel Stenberg-向我展示了一个使用libcurl选项的很好的例子:

curl http://example.com --libcurl example.c

正如你所猜测的那样,您可以在example.c中找到完整的代码(包括处理程序和清理)。


0
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi;
TCHAR tempCmdLine[MAX_PATH];
_tcscpy_s(tempCmdLine, MAX_PATH, _T("curl.exe http://localhost:8983/solr/update/extract?literal.id=doc2&uprefix=attr_&fmap.content=attr_content&commit=true -F myfile=@doc.pdf"));
CreateProcess(NULL, tempCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

您可能需要检查CreateProcess的返回值。


感谢您的快速回复。 如果我想使用cURL API curl_easy_init、curl_easy_setopt、curl_easy_perform和curl_easy_cleanup来执行相同的操作,那么我该如何实现呢? - prasad
我尝试使用httpput.c。它给了我以下错误消息,请问您能帮忙解决吗?<html><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/> <title>Error 400 </title> </head> <body><h2>HTTP ERROR: 400</h2><pre>Unsupported method: PUT</pre> <p>RequestURI=/solr/update/extract/</p><p><i><small><a href="http://jetty.mortbay.org/">Powered by Jetty://</a></small></i></p><br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> </body> </html> - prasad
curl "http://localhost:8983/solr/update/extract?literal.id=doc2&uprefix=attr_&fmap.content=attr_content&commit=true"" -F "myfile=@doc.pdf" 当我通过命令提示符运行它时,它可以正常工作。 - prasad
hd = open("doc1.pdf", O_RDONLY) ; fstat(hd, &file_info); close(hd) ; hd_src = fopen("doc1.pdf", "rb");curl_global_init(CURL_GLOBAL_ALL);curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl, CURLOPT_PUT, 1L); - prasad
1
谢谢你,Daniel。curl.exe有--libcurl <File>命令行选项,可以生成用于cURl命令的c CURL代码。通过参考生成的代码,我能够在Solr中索引文件。 - prasad
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接