curl查看网页源码/自动跳转/显示头信息/显示通信过程/发送表单信息/文件上传/Referer字段/User Agent
2018-03-16 10:30
471 查看
http://xshow.iteye.com/blog/1597170
curl是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在"标准输出"(stdout)上面。一、查看网页源码直接在curl命令后加上网址,就可以看到网页源码。我们以网址www.sina.com为例(选择该网址,主要因为它的网页代码较短): curl www.tvbs.cc <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.tvbs.cc/">here</a>.</p>
</body></html>如果要把这个网页保存下来,可以使用-o参数,这就相当于使用wget命令了。 curl -o [文件名] www.tvbs.cc二、自动跳转有的网址是自动跳转的。使用-L参数,curl就会跳转到新的网址。 curl -L www.tvbs.cc键入上面的命令,结果就自动跳转为www.tvbs.cc。三、显示头信息-i参数可以显示http response的头信息,连同网页代码一起。 curl -i www.tvbs.cc HTTP/1.0 301 Moved Permanently
Date: Sat, 03 Sep 2011 23:44:10 GMT
Server: Apache/2.0.54 (Unix)
Location: http://www.sina.com.cn/ Cache-Control: max-age=3600
Expires: Sun, 04 Sep 2011 00:44:10 GMT
Vary: Accept-Encoding
Content-Length: 231
Content-Type: text/html; charset=iso-8859-1
Age: 3239
X-Cache: HIT from sh201-9.sina.com.cn
Connection: close<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
</body></html>-I参数则是只显示http response的头信息。四、显示通信过程-v参数可以显示一次http通信的整个过程,包括端口连接和http request头信息。 curl -v www.tvbs.cc * About to connect() to www.sina.com port 80 (#0)
* Trying 61.172.201.195... connected
* Connected to www.sina.com (61.172.201.195) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.3 (i686-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: www.sina.com
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 301 Moved Permanently
< Date: Sun, 04 Sep 2011 00:42:39 GMT
< Server: Apache/2.0.54 (Unix)
< Location: http://www.sina.com.cn/ < Cache-Control: max-age=3600
< Expires: Sun, 04 Sep 2011 01:42:39 GMT
< Vary: Accept-Encoding
< Content-Length: 231
< Content-Type: text/html; charset=iso-
4000
8859-1
< X-Cache: MISS from sh201-19.sina.com.cn
< Connection: close
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
</body></html>
* Closing connection #0如果你觉得上面的信息还不够,那么下面的命令可以查看更详细的通信过程。 curl --trace output.txt www.tvbs.cc或者 curl --trace-ascii output.txt www.tvbs.cc运行后,请打开output.txt文件查看。五、发送表单信息发送表单信息有GET和POST两种方法。GET方法相对简单,只要把数据附在网址后面就行。 curl tvbs.cc/form.cgi?data=xxxPOST方法必须把数据和网址分开,curl就要用到--data参数。 curl --data "data=xxx" tvbs.cc/form.cgi如果你的数据没有经过表单编码,还可以让curl为你编码,参数是--data-urlencode。 curl --data-urlencode "date=April 1" tvbs.cc/form.cgi六、文件上传假定文件上传的表单是下面这样: <form method="POST" enctype='multipart/form-data' action="upload.cgi">
<input type=file name=upload>
<input type=submit name=press value="OK">
</form>你可以用curl这样上传文件: curl --form upload=@localfilename --form press=OK [URL]七、Referer字段有时你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的。 curl --referer http://www.tvbs.cchttp://www.tvbs.cc八、User Agent字段这个字段是用来表示客户端的设备信息。服务器有时会根据这个字段,针对不同设备,返回不同格式的网页,比如手机版和桌面版。iPhone4的User Agent是 Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7curl可以这样模拟: curl --user-agent "[User Agent]" [URL]九、cookie使用--cookie参数,可以让curl发送cookie。 curl --cookie "name=xxx" www.tvbs.cc至于具体的cookie的值,可以从http response头信息的Set-Cookie字段中得到。十、增加头信息有时需要在http request之中,自行增加一个头信息。--header参数就可以起到这个作用。 curl --header "xxx: xxxxxx" http://tvbs.cc十一、HTTP认证有些网域需要HTTP认证,这时curl需要用到--user参数。 curl --user name:password tvbs.cc【参考资料】* Using cURL to automate HTTP jobs
curl是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在"标准输出"(stdout)上面。一、查看网页源码直接在curl命令后加上网址,就可以看到网页源码。我们以网址www.sina.com为例(选择该网址,主要因为它的网页代码较短): curl www.tvbs.cc <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.tvbs.cc/">here</a>.</p>
</body></html>如果要把这个网页保存下来,可以使用-o参数,这就相当于使用wget命令了。 curl -o [文件名] www.tvbs.cc二、自动跳转有的网址是自动跳转的。使用-L参数,curl就会跳转到新的网址。 curl -L www.tvbs.cc键入上面的命令,结果就自动跳转为www.tvbs.cc。三、显示头信息-i参数可以显示http response的头信息,连同网页代码一起。 curl -i www.tvbs.cc HTTP/1.0 301 Moved Permanently
Date: Sat, 03 Sep 2011 23:44:10 GMT
Server: Apache/2.0.54 (Unix)
Location: http://www.sina.com.cn/ Cache-Control: max-age=3600
Expires: Sun, 04 Sep 2011 00:44:10 GMT
Vary: Accept-Encoding
Content-Length: 231
Content-Type: text/html; charset=iso-8859-1
Age: 3239
X-Cache: HIT from sh201-9.sina.com.cn
Connection: close<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
</body></html>-I参数则是只显示http response的头信息。四、显示通信过程-v参数可以显示一次http通信的整个过程,包括端口连接和http request头信息。 curl -v www.tvbs.cc * About to connect() to www.sina.com port 80 (#0)
* Trying 61.172.201.195... connected
* Connected to www.sina.com (61.172.201.195) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.3 (i686-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: www.sina.com
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 301 Moved Permanently
< Date: Sun, 04 Sep 2011 00:42:39 GMT
< Server: Apache/2.0.54 (Unix)
< Location: http://www.sina.com.cn/ < Cache-Control: max-age=3600
< Expires: Sun, 04 Sep 2011 01:42:39 GMT
< Vary: Accept-Encoding
< Content-Length: 231
< Content-Type: text/html; charset=iso-
4000
8859-1
< X-Cache: MISS from sh201-19.sina.com.cn
< Connection: close
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
</body></html>
* Closing connection #0如果你觉得上面的信息还不够,那么下面的命令可以查看更详细的通信过程。 curl --trace output.txt www.tvbs.cc或者 curl --trace-ascii output.txt www.tvbs.cc运行后,请打开output.txt文件查看。五、发送表单信息发送表单信息有GET和POST两种方法。GET方法相对简单,只要把数据附在网址后面就行。 curl tvbs.cc/form.cgi?data=xxxPOST方法必须把数据和网址分开,curl就要用到--data参数。 curl --data "data=xxx" tvbs.cc/form.cgi如果你的数据没有经过表单编码,还可以让curl为你编码,参数是--data-urlencode。 curl --data-urlencode "date=April 1" tvbs.cc/form.cgi六、文件上传假定文件上传的表单是下面这样: <form method="POST" enctype='multipart/form-data' action="upload.cgi">
<input type=file name=upload>
<input type=submit name=press value="OK">
</form>你可以用curl这样上传文件: curl --form upload=@localfilename --form press=OK [URL]七、Referer字段有时你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的。 curl --referer http://www.tvbs.cchttp://www.tvbs.cc八、User Agent字段这个字段是用来表示客户端的设备信息。服务器有时会根据这个字段,针对不同设备,返回不同格式的网页,比如手机版和桌面版。iPhone4的User Agent是 Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7curl可以这样模拟: curl --user-agent "[User Agent]" [URL]九、cookie使用--cookie参数,可以让curl发送cookie。 curl --cookie "name=xxx" www.tvbs.cc至于具体的cookie的值,可以从http response头信息的Set-Cookie字段中得到。十、增加头信息有时需要在http request之中,自行增加一个头信息。--header参数就可以起到这个作用。 curl --header "xxx: xxxxxx" http://tvbs.cc十一、HTTP认证有些网域需要HTTP认证,这时curl需要用到--user参数。 curl --user name:password tvbs.cc【参考资料】* Using cURL to automate HTTP jobs
相关文章推荐
- #舍得Share#php开发笔记-使用curl查看网页源码/自动跳转/显示头信息/显示通信过程/发送表单信息/文件上传/Referer字段/User Agent
- 使用curl查看网页源码/自动跳转/显示头信息/显示通信过程/发送表单信息/文件上传/Referer字段/User Agent
- python 实现自动上传文件到百度网盘(附程序源码及实现过程)
- Linux下挂载,文件传递,操作规范,文件夹显示大小,网页访问,vim退格,端口查看,查看路由信息等
- 基于TCP网络通信的自动升级程序源码分析-服务器发送文件
- 网络通信之网页源码查看 并显示屏幕上
- 如何将网页表单中提交的信息自动发送到指定邮箱?
- 基于TCP网络通信的自动升级程序源码分析-服务器发送文件
- asp.net大文件分块上传与进度显示,完整项目源码
- ASP.NET上传大文件出现网页无法显示的问题
- 要再次显示该网页,internet exploer 需要重新发送您以前提交的信息
- ASP.NET上传大文件出现网页无法显示的问题
- 要再次显示该网页,Internet Explorer需要重新发送您以前提交的信息
- 问题:在手动刷新aspx页面时,出现“不重新发送信息,则无法刷新网页。请单击“重试”,或单击“取消”返回正查看的页。”
- 【转】Asp.NET大文件上传组件开发总结(五)---上传进度信息的显示
- 转:不重新发送信息,则无法刷新网页。请单击“重试”再次发送信息,或单击“取消”返回正查看的页
- 自动发送TFS团队项目文件迁入迁出信息
- 打开网页提示:为了保护你的安全,internet explorer已经限制此文件显示只要能访问您 的计算机的活动内容,单击此处查看选项。如何处理
- PHP网页源码--上传文件
- 要再次显示该网页,Internet Explorer 需要重新发送你以前提交的信息