1#!/usr/bin/perl-w
2useLWP;
3usevars'@ISA';
4@ISA='LWP::UserAgent';
5
6subget_file{
7my$url='https//www.google.com';
8my$agent=__PACKAGE__->new;
9$response=$agent->request(POST$url,
10'User-Agent'=>'Mozilla/4.76[en](Win98;U)',
11
12Content_Type=>'form-data',
13Content=>[fileName=>[$fname],
14waspaction=>"FILEUPLOAD",
15swrFile=>[$fname]
16]
17);
18#$response->is_successordie"$url:",$response->message,"\n";
19my$content=$response->content;
20}
21
22这段代码,我用ruby改写,如下:
23defget_rac
24url=URI.parse('https:/www.google.com');
25#agent=__PACKAGE__.new;
26
27 req = Net::HTTP::Post.new(url.path)
req.basic_auth $uid, $pwd
28req.set_form_data({:fileName=>[$fname],:waspaction=>"FILEUPLOAD",:swrFile=>
29[$fname]},';')
30
31
32res=Net::HTTP.new(url.host,url.port).start{|http|http.request(req)}
33
34content=res;
35putsres.body
36end
37报错:endoffilereached
38C:/Ruby/lib/ruby/1.8/net/protocol.rb:133:in`sysread'
39C:/Ruby/lib/ruby/1.8/net/protocol.rb:133:in`rbuf_fill'
40C:/Ruby/lib/ruby/1.8/timeout.rb:62:in`timeout'
41C:/Ruby/lib/ruby/1.8/timeout.rb:93:in`timeout'
42C:/Ruby/lib/ruby/1.8/net/protocol.rb:132:in`rbuf_fill'
43C:/Ruby/lib/ruby/1.8/net/protocol.rb:116:in`readuntil'
44C:/Ruby/lib/ruby/1.8/net/protocol.rb:126:in`readline'
45C:/Ruby/lib/ruby/1.8/net/http.rb:2020:in`read_status_line'
46C:/Ruby/lib/ruby/1.8/net/http.rb:2009:in`read_new'
47C:/Ruby/lib/ruby/1.8/net/http.rb:1050:in`request'
48C:/WORKSPACE/get_rac.rb:27:in`get_rac'
49C:/Ruby/lib/ruby/1.8/net/http.rb:543:in`start'
50C:/WORKSPACE/get_rac.rb:27:in`get_rac'
51C:/WORKSPACE/get_rac.rb:39
52C:/Ruby/lib/ruby/1.8/net/protocol.rb:133:in`sysread':endoffilereached(EOFError)
53fromC:/Ruby/lib/ruby/1.8/net/protocol.rb:133:in`rbuf_fill'
54fromC:/Ruby/lib/ruby/1.8/timeout.rb:62:in`timeout'
55fromC:/Ruby/lib/ruby/1.8/timeout.rb:93:in`timeout'
56fromC:/Ruby/lib/ruby/1.8/net/protocol.rb:132:in`rbuf_fill'
57fromC:/Ruby/lib/ruby/1.8/net/protocol.rb:116:in`readuntil'
58fromC:/Ruby/lib/ruby/1.8/net/protocol.rb:126:in`readline'
59fromC:/Ruby/lib/ruby/1.8/net/http.rb:2020:in`read_status_line'
60fromC:/Ruby/lib/ruby/1.8/net/http.rb:2009:in`read_new'
61fromC:/Ruby/lib/ruby/1.8/net/http.rb:1050:in`request'
62fromC:/WORKSPACE/get_rac.rb:27:in`get_rac'
63fromC:/Ruby/lib/ruby/1.8/net/http.rb:543:in`start'
64fromC:/WORKSPACE/get_rac.rb:27:in`get_rac'
65fromC:/WORKSPACE/get_rac.rb:39
66不知道是什么问题。
现在貌似有点头绪了,问题就出在:
req.set_form_data({:fileName => [$fname],:waspaction=>"FILEUPLOAD",:swrFile=>
[$fname]}, ';')
这里。ruby中的意义与perl是不同的,这个地方;
Ruby posted about 1 year ago by christian
You have at least 5 options:
1 require 'rest_client'
2 RestClient.post 'http://localhost:3000/foo', fields_hash.merge(:file => File.new('/path/to/file'))
Tagged
post,
multipart,
curl,
ruby,
restclient,
upload 我用了第一种方法,奇怪的是发到一半,会有报错:
Code
classMultipart
definitialize(file_names)
@file_names=file_names
end
defpost(url,user,password)
boundary='---------------------------7d9b914b082e'
parts=[]
streams=[]
@file_names.eachdo|param_name,filepath|
pos=filepath.rindex('/')
filename=filepath[pos+1,filepath.length-pos]
#putsparam_name.to_s
#putsfilepath
parts<<StringPart.new("--"+boundary+"\r\n"+
"Content-Disposition:form-data;name=\""+param_name.to_s+"\";filename=\""+filename+"\"\r\n"+
"Content-Type:text/plain\r\n\r\n")
stream=File.open(filepath,"rb")
streams<<stream
parts<<StreamPart.new(stream,File.size(filepath))
end
parts<<StringPart.new("\r\n--"+boundary+"--\r\n")
post_stream=MultipartStream.new(parts)
#url=URI.parse(to_url)
http=Net::HTTP.new(url.host,url.port)
http.use_ssl=true
req=Net::HTTP::Post.new(url.path)
req.basic_auth"admin","admin"
req.content_length=post_stream.size
req.content_type='multipart/form-data;boundary='+boundary
req.body_stream=post_stream
res=Net::HTTP.new(url.host,url.port).start{|http|http.request(req)}
streams.eachdo|stream|
stream.close();
end
res
end
end
classStreamPart
definitialize(stream,size)
@stream,@size=stream,size
end
defsize
@size
end
defread(offset,how_much)
@stream.read(how_much)
end
end
classStringPart
definitialize(str)
@str=str
end
defsize
@str.length
end
defread(offset,how_much)
@str[offset,how_much]
end
end
An established connection was aborted by the software in your host machine.
C:/Ruby/lib/ruby/1.8/net/protocol.rb:175:in `write'
C:/Ruby/lib/ruby/1.8/net/protocol.rb:175:in `write0'
C:/Ruby/lib/ruby/1.8/net/protocol.rb:151:in `write'
C:/Ruby/lib/ruby/1.8/net/protocol.rb:166:in `writing'
C:/Ruby/lib/ruby/1.8/net/protocol.rb:150:in `write'
C:/Ruby/lib/ruby/1.8/net/http.rb:1557:in `send_request_with_body_stream'
C:/Ruby/lib/ruby/1.8/net/http.rb:1527:in `exec'
C:/Ruby/lib/ruby/1.8/net/http.rb:1048:in `request'
C:/WORKSPACE/get_rac.rb:48:in `post'
C:/Ruby/lib/ruby/1.8/net/http.rb:543:in `start'
C:/WORKSPACE/get_rac.rb:48:in `post'
C:/WORKSPACE/get_rac.rb:137:in `get_rac'
C:/WORKSPACE/get_rac.rb:167
C:/Ruby/lib/ruby/1.8/net/protocol.rb:175:in `write': An established connection was aborted by the software in your host machine. (Errno::ECONNABORTED)
from C:/Ruby/lib/ruby/1.8/net/protocol.rb:175:in `write0'
from C:/Ruby/lib/ruby/1.8/net/protocol.rb:151:in `write'
from C:/Ruby/lib/ruby/1.8/net/protocol.rb:166:in `writing'
from C:/Ruby/lib/ruby/1.8/net/protocol.rb:150:in `write'
from C:/Ruby/lib/ruby/1.8/net/http.rb:1557:in `send_request_with_body_stream'
from C:/Ruby/lib/ruby/1.8/net/http.rb:1527:in `exec'
from C:/Ruby/lib/ruby/1.8/net/http.rb:1048:in `request'
from C:/WORKSPACE/get_rac.rb:48:in `post'
from C:/Ruby/lib/ruby/1.8/net/http.rb:543:in `start'
from C:/WORKSPACE/get_rac.rb:48:in `post'
from C:/WORKSPACE/get_rac.rb:137:in `get_rac'
from C:/WORKSPACE/get_rac.rb:167
截包分析:发小点的文件是可以发全的。大一点的文件发了一半就不能继续上传文件了。实在找不到错误的地方。是
不是少设置了什么参数
优质内容筛选与推荐>>
1、有关LinkedList常用方法的源码解析2、MySQL Server8.0版本时出现Client does not support authentication protocol requested by server3、jqGrid的封装4、Ubuntu下编译SqlCipher以及解密微信数据库EnMicroMsg.db过程和坑5、AES加密与解密(秘钥)
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。
阅读
好看
已推荐到看一看
你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
取消
分享想法到看一看
确定
最多200字,当前共字
微信扫一扫
关注该公众号