开发网站,少不了测试。现在的网站为了加强安全性,都启用了HTTPS协议。所谓HTTPS,也就是HTTP文本在SSL协议中传输。用curl命令行来测试HTTPS站点是个很有用的功能,写点脚本,就可以做功能测试。

假定Ubuntu系统运行着一个HTTPS站点,用CppCMS编写,Nginx配置了SSL证书,通过FastCGI和CppCMS编写的后台进程连接在一起。

第一步,安装:

apt-get install curl

我的Ubuntu是13.04, 因此安装的curl版本很新,下面的命令检查版本号和其他信息:

curl -V
curl 7.29.0 (x86_64-pc-linux-gnu) libcurl/7.29.0 OpenSSL/1.0.1c zlib/1.2.7 libidn/1.25 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smtp smtps telnet tftp
Features: GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP

我们可以看到启用了SSL, 并且openssl版本是1.0.1c。

第二步,访问HTTP站点:

curl http://www.baidu.com
<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><title>百度一下,你就知道<unction(){var _t=new Date().getTime();document.cookie = "WWW_ST=" + _t +";expires=" + new Date(_t + 10000).toGMTString()})}catch(e){}</script></html><!--b5d54ba904675fbf-->

返回了百度的网页内容。内容太多,裁剪了。

第三步,查看详细信息,用-v参数。

 curl -v http://www.baidu.com
* About to connect() to www.baidu.com port 80 (#0)
*   Trying 61.135.169.125...
* Connected to www.baidu.com (61.135.169.125) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Wed, 03 Jul 2013 13:55:45 GMT
< Server: BWS/1.0
< Content-Length: 10437
< Content-Type: text/html;charset=utf-8
< Cache-Control: private
< Set-Cookie: BDSVRTM=24; path=/
< Set-Cookie: H_PS_PSSID=2757_1457_2704_2726_1788_2249_2702; path=/; domain=.baidu.com
< Set-Cookie: BAIDUID=5E81F8E70C5DE6EDB5C24088E3E56359:FG=1; expires=Wed, 03-Jul-43 13:55:45 GMT; path=/; domain=.baidu.com
< Expires: Wed, 03 Jul 2013 13:55:45 GMT
< P3P: CP=" OTI DSP COR IVA OUR IND COM "
< Connection: Keep-Alive
<
<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><title>百度一下,你就知道</title><style >html,body{height:100%}html{overflow-y:auto}#wrapper{position:relative;_position:;min-height:100%}#content{padding-bottom:100px;text-align:center}#ftCon{height:100px;position:absolute;bottom:44px;text-align:center;width:100%;margin:0 auto;z-index:0;overflow:hidden}#ftConw{width:720px;margin:0 auto}body{font:12px arial;text-align:;background:#fff}body,p,form,ul,li{margin:0;padding:0;list-style:none}body,form,#fm{position:relative}td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}#u{color:#999;padding:4px 10px 5px 0;text-align:right}#u a{margin:0 5px}#u .reg{margin:0}#m{width:720px;margin:0 auto}#nv a,#nv b,.btn,#lk{font-size:14px}#fm{padding-left:110px;text-align:left;z-index:1}input{border:0;padding:0}#nv{height:19px;font-size:16px;margin:0 0 4px;text-alig

这样详细的信息都显示出来了。-v参数很有用,一般调试时都打开。

如果只想查看头部信息,用-i代替-v.

第四步,访问本地HTTPS站点

curl --insecure https://localhost/your_site/login_page<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="content-type" content="text/html; charset=utf-8"><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache">

---insecure表示忽略校验步骤。

我试过用--cacert选项指定server.crt文件,也就是我的nginx使用的那个文件。但是报错。所以直接忽略算了。

第五步,调用HTTPS的login API登录

curl -v --insecure -d "email=gche@yousite.com&pwd=123456&language=en" https://localhost/your_site/login
* About to connect() to localhost port 443 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: noneCApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using ECDHE-RSA-AES256-SHA
* Server certificate:
*    subject: C=AU; ST=Some-State; O=Internet Widgits Pty Ltd
*    start date: 2013-06-02 07:24:53 GMT
*    expire date: 2014-06-02 07:24:53 GMT
*    issuer: C=AU; ST=Some-State; O=Internet Widgits Pty Ltd
*    SSL certificate verify result: self signed certificate (18), continuing anyway.
> POST /your_site/login HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
> Content-Length: 51
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 51 out of 51 bytes
< HTTP/1.1 200 OK
< Server: nginx/1.5.1
< Date: Wed, 03 Jul 2013 14:02:38 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: CppCMS/1.0.3
< Set-Cookie: cml_session=518b7fc5117e87bce28f2444; Max-Age=36000; Path=/; Version=1
<
* Connection #0 to host localhost left intact
{"message":"Login succeeded!","status":0,"value":""}

-d "...&..." 的参数是通过POST方法发送参数。服务端最终回复一个JSON格式的字符串,表示登录成功。并且拿到了cml_session的值,也就是cookie.

第六步,用cookie访问HTTP网页。后面的网页只需要HTTP访问,提供正确的cookie即可。

curl -v --cookie "cml_session=518b7fc5117e87bce28f2444" http://localhost/your_site/home
* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /your_site/home HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
> Cookie: cml_session=518b7fc5117e87bce28f2444
>
< HTTP/1.1 200 OK
< Server: nginx/1.5.1
< Date: Wed, 03 Jul 2013 14:06:43 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: CppCMS/1.0.3
< <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="content-type" content="text/html; charset=utf-8"><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><title>CML Cloud</title><link type="text/css" href="../style/reset.css" rel="stylesheet"/><link type="text/css" href="../style/style.css" rel="stylesheet"/>

用curl访问HTTPS站点并登录相关推荐

  1. Spring RestTemplate 访问https站点

    RestTemplate 是Spring对HttpClient的封装, API的调用和结果的解析更简单了.举例来说,访问一个站点并获取结果的代码只需要两行代码就可以了,类似: String url = ...

  2. HttpClient 忽略证书直接访问https站点

    使用HttpClient访问https 站点时,如果Java没有导入该站点的证书的话,则会报如下错误: javax.net.ssl.SSLHandshakeException: sun.securit ...

  3. HttpClient 使用证书访问https站点

    使用HttpClient访问https 站点时,如果JRE或者JDK没有导入某个站点的证书,则会报如下错误: javax.net.ssl.SSLHandshakeException: sun.secu ...

  4. curl证书过期_centos7内核升级及curl访问https证书过期处理

    centos7内核升级及curl访问https证书过期处理 先看下当前系统的linux内核版本 uname -r 3.10.0-229.el7.x86_64 升级步骤 1.rpm --import h ...

  5. php curl访问HTTPS页面502

    今天网站更换https和服务器,用php curl访问https页面,出现502(通过nginx):用php直接执行这个php文件,会报错Segmentation fault,导致直接退出: 1.Se ...

  6. curl+个人证书(又叫客户端证书)访问https站点

    目前,大公司的OA管理系统(俗称内网),安全性要求较高,通常采用https的双向 认证模式. 首先,什么是https,简单的说就是在SSL协议之上实现的http协议(get.post等操作).更多的介 ...

  7. curl+个人证书访问https站点

    目前,大公司的OA管理系统(俗称内网),安全性要求较高,通常采用https的双向 认证模式. 首先,什么是https,简单的说就是在SSL协议之上实现的http协议(get.post等操作) 什么是双 ...

  8. IIS 使用OpenSSL 生成的自签名证书,然后使用SingalR 客户端访问Https 站点通信

    使用SignalR 的客户端去发送消息给使用 https 部署的站点,官方文档目前并没有详细的教程,所以在此记录下步骤: 使用管理员身份打开cmd 窗口,选择一个整数保存文件夹的地址,切换到对应的文件 ...

  9. curl 访问https接口_微信/QQ域名检测-最新腾讯域名检测官方接口

    背景 最近手里有个项目需要检测域名在微信里是否可以打开,如果被微信拦截,则需要进行下一步操作,所以需要判断域名的状态,但是微信官方并没有提供相关查询的方法,最后在网上找到了这个接口地址,分享给有需要的 ...

最新文章

  1. 使用 HTML5、CSS3 和 MathML 在 EPUB 3 中制作版式丰富的出版物
  2. 请问生产成本收集器与标准成本评估有什么关联?
  3. ompl_interface/OMPLPlanner
  4. VTK:截锥体用法实战
  5. 本地文件上传到ecs_将本地代码上传到GitHub
  6. css3中变形与动画(三)
  7. mysql语句二级查询_mysql_2 基本查询语句
  8. python web框架【补充】自定义web框架
  9. 利用ichart绘制网页图表
  10. h3c防火墙服务器ip修改,H3C防火墙常用配置命令
  11. NFT 项目的 7 种市场营销策略
  12. vue 多个组件动态加载(动态组件的使用)
  13. aras innovator: 分类筛选如何做?
  14. 忘记了PPT文件打开密码怎么办?
  15. 「AI Timer 说」我只是没有行动而已,我笃定。
  16. 软件测试多次点击按钮叫什么方法,常见控件的测试点 - 爱昵容儿 的个人空间 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
  17. 50个经典的增长黑客策略高效实现增长
  18. 【Linux】ARM篇七--WDT看门狗实验
  19. C语言,判断一个数是否是质数(素数)。质数:只能被1和它本身整除的数,称为质数.
  20. 云徙科技CEO包志刚:数字化锻造企业韧性

热门文章

  1. 房地产企业的客户细分战略
  2. 第14题 计算机网络故障排除的第二步是,计算机网络故障诊断与排除第1章网络故障和网络诊断测试工具(习题)(ok)...
  3. jquery ajax和servlet,浅谈ajax在jquery中的请求和servlet中的响应
  4. 织梦支持html5吗,自动更新HTML FOR DedeCMS V5.3(支持首页,列表页,文章页)
  5. 和php结合实现分页js代码,无JS,完全php面向过程数据分页实现代码
  6. php 当前时间转换,php时间转换
  7. java里frame颜色_如何为Java中的窗口(JFrame)添加颜色
  8. python:接口自动化测试框架优化版
  9. @GeneratedValue不能使属性自增长
  10. ai怎么画路线_AI换脸的本质是把颜值和表情分开