用curl访问HTTPS站点并登录(对HTTP返回的结果特别清楚)
开发网站,少不了测试。现在的网站为了加强安全性,都启用了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: none
- CApath: /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"/>
http://blog.csdn.net/csfreebird/article/details/9237925
用curl访问HTTPS站点并登录(对HTTP返回的结果特别清楚)的更多相关文章
- 用curl访问HTTPS站点并登录
开发网站,少不了测试.现在的网站为了加强安全性,都启用了HTTPS协议.所谓HTTPS,也就是HTTP文本在SSL协议中传输.用curl命令行来测试HTTPS站点是个很有用的功能,写点脚本,就可以做功 ...
- php curl 访问 https站点
$uri = "https://your_website"; $ch = curl_init (); $data=I('post.'); curl_setopt ( $ch, CU ...
- curl+个人证书(又叫客户端证书)访问https站点
摘自http://blog.csdn.net/chary8088/article/details/22990741 curl+个人证书(又叫客户端证书)访问https站点 目前,大公司的OA管理系统( ...
- ACME[free https] Linux中使用curl命令访问https站点4种常见错误和解决方法
free https certification generator https://github.com/Neilpang/acme.sh/wiki/%E8%AF%B4%E6%98%8E 每一种客户 ...
- centos7内核升级及curl访问https证书过期处理
centos7内核升级及curl访问https证书过期处理 先看下当前系统的linux内核版本 uname -r 3.10.0-229.el7.x86_64 升级步骤 1.rpm --import h ...
- C#检测并安装https站点的数字证书,CefSharp和HttpWebRequest通过会话Cookie实现自动登录访问https站点
HttpUtil工具类: using System; using System.Collections.Generic; using System.IO; using System.Linq; usi ...
- requests访问https站点证书告警问题
背景 想使用api的方式去访问公司内部azkaban平台,https站点,azkaban的官方api文档使用的curl语句,如下: curl -k -X POST --data "actio ...
- iOS 安全:UIWebView访问Https站点防止中间人攻击
尽管Https协议能够提供数据的加密.身份的认证等安全服务,但并不是没有漏洞.HTTPS协议安全隐患的存在可能使用户受到各种极具破坏力的网络攻击.其中中间人攻击(Man In The Middle, ...
- [PHP] curl访问https与CA证书问题
CA证书,用来在调用HTTPS资源的时候,验证对方网站是否是CA颁布的证书,而不是自己随便生成的 curl命令1.需要下载CA证书 文件地址是 http://curl.haxx.se/ca/cacer ...
随机推荐
- 【u125】最大子树和
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课 ...
- php实现 合唱队形(算法想清楚在动)
php实现 合唱队形(算法想清楚在动) 一.总结 一句话总结:写一个最长递增子序列的函数,正反两遍扫一下就好.写函数这样不容易错.这个好像可以用二分来优化. 1.算法题怎么提高正确率和节约时间? 算 ...
- windows server 2012 AD 活动目录部署加入域并创建域用户(寻找视频课程)(计算机加入域其实是本计算机的管理员账号(本机名)加入域,关联账号即可在已经加入域的计算机上面登录)
windows server 2012 AD 活动目录部署加入域并创建域用户(寻找视频课程)(计算机加入域其实是本计算机的管理员账号(本机名)加入域,关联账号即可在已经加入域的计算机上面登录) 一.总 ...
- eclipse jdt
http://www.cnblogs.com/hoojo/p/use_eclipse_ant_javac_JDT_compiler_class.html
- web报表工具FineReport经常使用函数的使用方法总结(文本函数)
文本函数 CHAR CHAR(number):依据指定数字返回相应的字符.CHAR函数可将计算机其它类型的数字代码转换为字符. Number:用于指定字符的数字,介于1Number:用于指定字符的数字 ...
- [tmux] Reuse terminal workspaces using tmux sessions
In this lesson, we'll learn how to detach from a running tmux session and leave it running in the ba ...
- CentOS 7安装fcitx中文输入法
安装过程例如以下: 1.增加EPEL源 EPEL7差点儿是CentOS必备的源: sudo yum install epel-release 2.加入mosquito-myrepo源 mosquito ...
- 如何在使Xcode打包iOS应用时自动增加编译号
在红框标注的输入框中输入:真机调试编译成功增加 echo $CONFIGURATION if [ "Release" == "${CONFIGURATION}" ...
- CodeForces 659E New Reform (图的遍历判环)
Description Berland has n cities connected by m bidirectional roads. No road connects a city to itse ...
- WPF 通过位处理合并图片
原文:WPF 通过位处理合并图片 本文告诉大家,在使用 WPF 合并两张图片的处理,可以使用像素之间的与或和异或的方式,对三个颜色的通道进行处理. 先给大家看一下软件的界面 这就是通过将左边的两张图片 ...