Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have written this code to make a file local before start working on it. But when i execute it is not copying the file to local. I am getting 401 error. could you please let me know whats wrong with this code. thaks in advance.
#!/usr/bin/perl -w use LWP::Simple; my $filename = "http://sample.com/secure/sample.xls"; MakeFileLocal($filename); sub MakeFileLocal { my $filename = shift; chomp($filename); $filename =~ m/.*\.(.*)$/; my $suffix = $1; print "Suffix : $suffix\n"; my $newname = "d:/test/mohan/temp.$suffix"; unlink("$newname"); if ($filename =~ m/^http/i) { print "Name matches starts with http://\n"; my $retval = getstore($filename, $newname); die "Could not store it!!\n" unless is_success($retval); } else { my $cmd = qq~copy "$filename" "$newname"~; print "$cmd\n"; my @a = `$cmd`; } if (-e "$newname") { return $newname; } return 0; }

Replies are listed 'Best First'.
Re: HTTP file download
by Anonymous Monk on Nov 07, 2008 at 07:29 UTC
    I am getting 401 error. could you please let me know whats wrong with this code. thaks in advance.
    Learn what 401 means
      Hello, I had a look at the HTTP error code 401. Its related to authentication failure. I have read information about it. After that i have modified my code as follows.
      #!/usr/bin/perl use LWP::UserAgent; my $url = 'http://sample.com/sites/projcentral/test.xls'; my $ua = new LWP::UserAgent(keep_alive=>1); $ua->no_proxy; #$ua->agent('MSIE/6.0'); $ua->credentials('sample.com','/sites/projcentral/','mohan' => 'mohan1 +23'); $ua->timeout(15); my $request = HTTP::Request->new('GET'); $request->url($url); my $response = $ua->request($request); my $code = $response->code; print "Return Code ====>\n $code\n"; my $headers = $response->headers_as_string; #If the Error is 401.2 then, #The authentication methods that were tried are either disabled, or yo +u are attempting to use NTLM through a proxy server. print "Headers ======>\n $headers\n"; # HTML body: my $body = $response->content; print "================\n$body====================\n";
      When i have executed this code. I am getting the following output.
      Return Code ====> 401 Headers ======> Date: Mon, 10 Nov 2008 04:53:08 GMT Server: Microsoft-IIS/6.0 WWW-Authenticate: NTLM Content-Length: 1656 Content-Type: text/html Content-Type: text/html; charset=Windows-1252 Client-Date: Mon, 10 Nov 2008 04:53:08 GMT Client-Peer: 10.24.250.27:80 Client-Response-Num: 1 Client-Warning: Unsupported authentication scheme 'ntlm' MicrosoftSharePointTeamServices: 12.0.0.6219 Set-Cookie: RBNA_Boschbiz_07=R731053916; path=/ Title: You are not authorized to view this page X-Powered-By: ASP.NET ================ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/T +R/html4/strict.dtd"> <HTML><HEAD><TITLE>You are not authorized to view this page</TITLE> <META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-12 +52"> <STYLE type="text/css"> BODY { font: 8pt/12pt verdana } H1 { font: 13pt/15pt verdana } H2 { font: 8pt/12pt verdana } A:link { color: red } A:visited { color: maroon } </STYLE> </HEAD><BODY><TABLE width=500 border=0 cellspacing=10><TR><TD> <h1>You are not authorized to view this page</h1> You do not have permission to view this directory or page using the cr +edentials that you supplied because your Web browser is sending a WWW +-Authenticate header field that the Web server is not configured to a +ccept. <hr> <p>Please try the following:</p> <ul> <li>Contact the Web site administrator if you believe you should be ab +le to view this directory or page.</li> <li>Click the <a href="javascript:location.reload()">Refresh</a> butto +n to try again with different credentials.</li> </ul> <h2>HTTP Error 401.2 - Unauthorized: Access is denied due to server co +nfiguration.<br>Internet Information Services (IIS)</h2> <hr> <p>Technical Information (for support personnel)</p> <ul> <li>Go to <a href="http://go.microsoft.com/fwlink/?linkid=8180">Micros +oft Product Support Services</a> and perform a title search for the w +ords <b>HTTP</b> and <b>401</b>.</li> <li>Open <b>IIS Help</b>, which is accessible in IIS Manager (inetmgr) +, and search for topics titled <b>About Security</b>, <b>Authentication +</b>, and <b>About Custom Error Messages</b>.</li> </ul> </TD></TR></TABLE></BODY></HTML> ====================
      could you please let me know where should i am doing wrong in this code.