免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2704 | 回复: 2
打印 上一主题 下一主题

求用perl做的程序模拟post一个文件的方法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-06 06:03 |只看该作者 |倒序浏览
求用perl做的程序模拟post一个文件的方法
$ua->cookie_jar(HTTP::Cookies->new(file => "D:\\bin\\posttools\\dingtie\\date\\feier\\cookie.txt", autosave => 1));
$ua->timeout(240);
#开始提交文件
$response = $ua->post( 'http://www1.5hxy.com/bbs/postupfile.asp','Content-Type'=>'multipart/form-data',$torrent,'referer'=>'http://www1.5hxy.com/bbs/');

跟踪网络报文发现这样POST出去的和正常用浏览器post出去的不一样,导致失败,哪位高手有代码让我借鉴一下?是post一个文件不是post一个表单,谢谢了

论坛徽章:
0
2 [报告]
发表于 2007-02-06 20:55 |只看该作者
..

论坛徽章:
0
3 [报告]
发表于 2007-02-06 22:23 |只看该作者
等你们算是没希望了,我自己找到答案了:
Sending my ~/.profile to the survey used as example above can be achieved by this:

  POST 'http://www.perl.org/survey.cgi',
       Content_Type => 'form-data',
       Content      => [ name  => 'Gisle Aas',
                         email => 'gisle@aas.no',
                         gender => 'M',
                         born   => '1964',
                         init   => ["$ENV{HOME}/.profile"],
                       ]

This will create a HTTP::Request object that almost looks this (the boundary and the content of your ~/.profile is likely to be different):

  POST http://www.perl.org/survey.cgi
  Content-Length: 388
  Content-Type: multipart/form-data; boundary="6G+f"

  --6G+f
  Content-Disposition: form-data; name="name"

  Gisle Aas
  --6G+f
  Content-Disposition: form-data; name="email"

  gisle@aas.no
  --6G+f
  Content-Disposition: form-data; name="gender"

  M
  --6G+f
  Content-Disposition: form-data; name="born"

  1964
  --6G+f
  Content-Disposition: form-data; name="init"; filename=".profile"
  Content-Type: text/plain

  PATH=/local/perl/binPATH
  export PATH
  --6G+f--
If you set the $DYNAMIC_FILE_UPLOAD variable (exportable) to some TRUE value, then you get back a request object with a subroutine closure as the content attribute. This subroutine will read the content of any files on demand and return it in suitable chunks. This allow you to upload arbitrary big files without using lots of memory. You can even upload infinite files like /dev/audio if you wish; however, if the file is not a plain file, there will be no Content-Length header defined for the request. Not all servers (or server applications) like this. Also, if the file(s) change in size between the time the Content-Length is calculated and the time that the last chunk is delivered, the subroutine will Croak.

The post(...) method of "LWP::UserAgent" exists as a shortcut for $ua->request(POST ...).
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP