2024年C C++最全curl常见用法_curl -xpost -d,2024年最新渣本毕业两年经验

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

curl is used in command lines or scripts to transfer data.

curl is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the internet transfer backbone for thousands of software applications affecting billions of humans daily.

1.3 curl supports

curl supports DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, Telnet and TFTP.

curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more.

2 常见用法

2.1 获取页面

**命令格式:**curl URL

示例用法如下:

curl https://www.baidu.com/

这是 curl 最简单的使用方法,运行上面的命令可以获取到 https://www.baidu.com/ 指向的页面数据,同样,如果这里的 URL 指向的是一个文件或者一幅图,也可以直接下载到本地。

2.2 发送POST请求

**命令格式:**curl -X POST -d ‘post content’ URL

-X 选项的解释如下:

-X/--request <command>
      (HTTP) Specifies a custom request method to use when communicating with the HTTP 
server.  The specified request will be used instead of the method otherwise used (which 
defaults to GET). Read the HTTP 1.1 specification for details and explanations.
      (FTP) Specifies a custom FTP command to use instead of LIST when doing file lists 
with ftp.
      If this option is used several times, the last one will be used.

-d 选项的解释如下:

-d/--data <data>
      (HTTP) Sends the specified data in a POST request to the HTTP server, in a way that 
can emulate as if a user has filled in a HTML form and pressed the submit button. Note that 
the data is sent exactly as specified with no extra processing (with all newlines cut off). 
The data is expected to be "url-encoded". This will cause curl to pass the data to the 
server using the content-type application/x-www-form-urlencoded. Compare to -F/--form. If 
this option is used more than once on the same command line, the data pieces specified will 
be merged together with a separating &-letter. Thus, using '-d name=daniel -d skill=lousy' 
would generate a post chunk that looks like 'name=daniel&skill=lousy'.

示例用法如下:

curl -X -d 'api_ver=1.0.0&merchant_id=1800257711' http://api.gcdev.tfb8.com/cgi-bin/v2.0/api_merchant_agency_qry.cgi

注意:-d 选项后的内容强烈建议用单引号’包含起来,而非双引号。

2.3 发送GET请求

**命令格式:**curl URL?param1=value1&param2=value2…

示例用法如下:

curl http://api.gcdev.tfb8.com/cgi-bin/v2.0/api_merchant_agency_qry.cgi?api_ver=1.0.0\&merchant_id=1800257711

注意:在 shell 中执行 curl 命令,发送 GET 请求时,如果要发送多个参数,则需要将参数之间的分隔符“&”修改为“&”,否则 shell 会将分隔符“&”理解为后台执行指令,导致 GET 请求参数被截断。

2.4 链接重定向/自动跳转(-L)

使用 curl 打开某些被重定向的链接时,可能无法获取我们想要的网页内容。假设有链接 http://www.A.com,使用浏览器访问该链接,会自动跳转到链接 http://www.B.com。那么如果使用 curl 直接访问 A 链接,示例命令如下:

curl http://www.A.com

会提示 A 链接不存在,因为 A 链接已经重定向到 B 链接了,但是 curl 不能识别出来(这个重定向动作)。

此时,就需要使用 -L 选项了。使用“curl -L”可以使 curl 打开的链接自动跳转,获取到最终的网页。示例命令如下:

curl -L http://www.A.com

使用上面的命令访问链接 A 时,就会自动跳转到其重定向的链接 B 中,最终获取到链接 B 的内容。

2.5 将页面响应写入文件(-o)

使用 curl 将网页的响应消息写入到一个文件中,可以“-o”选项,如下:

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo


![img](https://img-blog.csdnimg.cn/img_convert/110e5bec4f46375c0af5e479da7a060c.png)
![img](https://img-blog.csdnimg.cn/img_convert/aec855156a6571032e91ebbb8058b0b2.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**

开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**

  • 28
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值