in reply to how to upload a file to a secured website?

Here a 2 scripts using lwp. The first does the auth and upload, the second shows proxy use. Just combine the two.
#!/usr/bin/perl use warnings; use strict; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request::Common qw(POST); my $https_url = 'https://zentara.zentara.net/~zentara/cgi-bin/uploads/ +up1.cgi'; my $https_user = 'zentara'; my $https_pass = 'foobar'; my $file = 'testout.tgz'; &postHTTPS(); sub postHTTPS { my $ua = new LWP::UserAgent; $ua->protocols_allowed( [ 'https'] ); $ua->cookie_jar(HTTP::Cookies->new(file =>".cookies.txt",autos +ave => 1)); #setup request my $req = POST($https_url, Content_Type => 'multipart/form-data', Content =>[ file =>[ $file ], ], ); #setup auth $req->authorization_basic($https_user, $https_pass); #do post my $response = $ua->request($req); if ($response->is_error()) { printf " %s\n", $response->status_line; print "https request error!\n"; } else { my $content = $response->content(); print "$content\n"; } if ( $response->is_success ) { print $response->as_string; }else { print $response->status_line; } }
#!/usr/bin/perl use warnings; use strict; use LWP::UserAgent; my $href = "http://kompas.com/kompas-cetak/0505/09/metro/index.htm +"; my $ua = new LWP::UserAgent; my $request = new HTTP::Request ("GET" => $href); $ua->env_proxy, $request->proxy_authorization_basic($ENV{HTTP_proxy_user}, $ENV{HTTP_p +roxy_pass}) if defined $ENV{HTTP_proxy}; my $response = $ua->request($request); if ($response && $response->is_success) { print "Success!\n", $response->content; } else { print "Failed!\n", $response->as_string; }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: how to upload a file to a secured website?
by KarthikK (Sexton) on Oct 17, 2007 at 07:29 UTC
    Thanks zentara. I will give it a try today. Best Regards Karthik
Re^2: how to upload a file to a secured website?
by KarthikK (Sexton) on Oct 17, 2007 at 13:08 UTC
    I tried your code and i did not get any errors but i am getting this message: <HTML></HTML> HTTP/1.0 200 OK Content-Type: text/html Client-Date: Wed, 17 Oct 2007 13:00:23 GMT Client-Peer: ip address Refresh: 0; URL=https://www.somesite.com/import.do <HTML></HTML> The file is not uploaded. I am not sure what could be the problem. Other thing is there is no login page but the login is via the integrated login where IE asks for Username and pwd when this site is invoked. any idea if i am doing anything wrong?
      It's hard to say what the problem is, since I don't have your site to play with. The authorization_basic part of the upload script, is the integrated login put out by the server, it's not for a separate login form/page.

      It looks as if the url you are entering, is redirecting you to another site. Maybe the proxy server is sending the message, have you added the proxy handling? You didn't post the code you are using.

      Lwp works in a very straight forward manner, when the upload site redirects, or something else devious, you may need to use WWW::Mechanize.

      The first thing I would try, is to use the URL you show above, as the upload URL, if you are not already. You can also try adding

      $ua->requests_redirectable ( ['GET', 'HEAD', 'POST'] ) # or push @{ $ua->requests_redirectable }, 'POST';
      You may need WWW::Mechanize

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum
        Thanks again! below is the code
        #!/usr/bin/perl use warnings; use strict; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request::Common qw(POST); my $upload_url = 'https://mysite.com/import.do'; my $login_url = 'https://mysite.com/login.do '; my $https_user = 'ABC'; my $https_pass = 'ABC'; my $file = 'File_2007_10_15_14_21_02.csv'; &postHTTPS(); sub postHTTPS { my $ua = new LWP::UserAgent(keep_alive=>1); $ua->proxy(https => 'http://ourproxyserver'); my $request = new HTTP::Request ("GET" => $login_url ); $request->authorization_basic($https_user, $https_pass); #$ua->protocols_allowed( [ 'https'] ); $ua->cookie_jar(HTTP::Cookies->new(file =>".cookies.txt",autos +ave => 1)); # $request->proxy_authorization_basic($https_user, $https_pass); #setup request print "before posting\n"; my $req=POST ($upload_url,Content_Type => 'multipart/form-data +', Content_Type => [ submit => 1, Content => [ $file ] ]); print "after posting\n"; my $response = $ua->request($req); if ($response->is_error()) { printf " %s\n", $response->status_line; print "https request error!\n"; } else { my $content = $response->content(); #print "$content\n"; } print ""; if ( $response->is_success ) { print $response->as_string; }else { print $response->status_line; } }
        As you might know I cannot give my exact site name due to some reasons. I think when i call the upload page it goes to the login page whre we have to provide the username and password (not in the form but in the IE authentication box) when i execute the above code I dont get any error! Have I missed anything in the code?
        no luck ;-( same problem as what is mentioned here http://lists.evolt.org/archive/Week-of-Mon-20001204/021219.html i have posted the code in my message. awaiting for your response thanks in advance regards Karthik