By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,220 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

HTTP file download

P: 33
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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use LWP::Simple;
  3.  
  4. my $filename = "http://sample.com/secure/sample.xls";
  5.  
  6. MakeFileLocal($filename);
  7.  
  8. sub MakeFileLocal {
  9.   my $filename = shift;
  10.  
  11.   chomp($filename);
  12.  
  13.   $filename =~ m/.*\.(.*)$/;
  14.   my $suffix = $1;
  15.   print "Suffix : $suffix\n";
  16.   my $newname = "d:/test/mohan/temp.$suffix";
  17.  
  18.   unlink("$newname");
  19.  
  20.   if ($filename =~ m/^http/i) {
  21.     print "Name matches starts with http://\n";
  22.     my $retval = getstore($filename, $newname);
  23.     die "Could not store it!!\n" unless is_success($retval);
  24.   } else {
  25.     my $cmd = qq~copy "$filename" "$newname"~;
  26.     print "$cmd\n";
  27.     my @a = `$cmd`;
  28.     }
  29.  
  30.   if (-e "$newname") {
  31.     return $newname;
  32.   }
  33.  
  34.   return 0;
  35.   }
  36.  
Nov 7 '08 #1
Share this Question
Share on Google+
8 Replies


KevinADC
Expert 2.5K+
P: 4,059
401 is document not found error. Are you sure the URI to the remote file is correct?

http://sample.com/secure/sample.xls
Nov 7 '08 #2

P: 33
401 is document not found error. Are you sure the URI to the remote file is correct?

http://sample.com/secure/sample.xls
hello Kevin,
Document is available at the same url.
when i use the same url in browser it is asking me to save or open it through file download alert message box.
Nov 7 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Hard to say, is there a firewall or proxy server maybe causing the error?
Nov 7 '08 #4

P: 33
the server is a microsoft biztalk server.
So i am not sure about whether any firewall behind it.
As it is a corporate machine i believe definitely there will be firewall.
If a firewall is available does it block such type of requests.
I dont have much knowledge on firewalls working.
thanks in advance.
Nov 7 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
I don't know enough to be able to help you with firewall problems. I just tested your code and it works for me:


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use LWP::Simple;
  3. my $filename = "http://www.e-pixs.com/smilies/smiley10.gif";
  4. MakeFileLocal($filename);
  5. sub MakeFileLocal {
  6.   my $filename = shift;
  7.   chomp($filename);
  8.   $filename =~ m/.*\.(.*)$/;
  9.   my $suffix = $1;
  10.   print "Suffix : $suffix\n";
  11.   my $newname = "c:/perl_test/smiley10.$suffix";
  12. #  unlink("$newname");
  13.   if ($filename =~ m/^http/i) {
  14.     print "Name matches starts with http://\n";
  15.     my $retval = getstore($filename, $newname);
  16.     die "Could not store it!!\n" unless is_success($retval);
  17.   } else {
  18.     my $cmd = qq~copy "$filename" "$newname"~;
  19.     print "$cmd\n";
  20.     my @a = `$cmd`;
  21.     }
  22.   if (-e "$newname") {
  23.     return $newname;
  24.   }
  25.   return 0;
  26.   }
  27.  
the script got the gif from the site and stored it on my server.
Nov 7 '08 #6

P: 33
I don't know enough to be able to help you with firewall problems. I just tested your code and it works for me:


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use LWP::Simple;
  3. my $filename = "http://www.e-pixs.com/smilies/smiley10.gif";
  4. MakeFileLocal($filename);
  5. sub MakeFileLocal {
  6.   my $filename = shift;
  7.   chomp($filename);
  8.   $filename =~ m/.*\.(.*)$/;
  9.   my $suffix = $1;
  10.   print "Suffix : $suffix\n";
  11.   my $newname = "c:/perl_test/smiley10.$suffix";
  12. #  unlink("$newname");
  13.   if ($filename =~ m/^http/i) {
  14.     print "Name matches starts with http://\n";
  15.     my $retval = getstore($filename, $newname);
  16.     die "Could not store it!!\n" unless is_success($retval);
  17.   } else {
  18.     my $cmd = qq~copy "$filename" "$newname"~;
  19.     print "$cmd\n";
  20.     my @a = `$cmd`;
  21.     }
  22.   if (-e "$newname") {
  23.     return $newname;
  24.   }
  25.   return 0;
  26.   }
  27.  
the script got the gif from the site and stored it on my server.
Hello Kevin,
Thank you very for your response.
From this discussion i got to know nothing wrong in the code i have written.
The only problem is may be due to firewall or proxy server.
I will discuss this thing further with that server administrator.
Nov 7 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
OK, you're welcome, good luck.

Kevin
Nov 7 '08 #8

P: 33
OK, you're welcome, good luck.

Kevin
Hello,
As i am getting 401 error, i tried to understand the reason behind this error.
i found it will come if we don't have access to the particular links

[HTML]<a href="http://en.wikipedia.org/wiki/HTTP_401"> 401 error </a>[/HTML]

So later i modified my code so that it will send authentication details as well.
and when i requested my administrator he was telling i am having read access to the directory from where i am going to download the file.
so my new script is as follows.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2. use LWP::UserAgent;
  3. #my $url = 'http://si-airbag-www.de.bosch.com/cpc/Default.aspx'; 
  4. my $url = 'http://boschbiz.us.bosch.com/sites/projcentral/1810/Secured%20Engineering/60.Software/A.SW_Project_Management/00.%20Schedule/DC10_60_A_00_SW_Schedule_Full.mpp'; 
  5. my $ua = new LWP::UserAgent(keep_alive=>1);
  6. $ua->no_proxy;
  7. #$ua->agent('MSIE/6.0');
  8. $ua->credentials('sample.com:80','sites','mohan' => 'mohan123');
  9. $ua->timeout(15);
  10. my $request = HTTP::Request->new('GET');
  11. $request->url($url); 
  12. my $response = $ua->request($request);  
  13. my $code = $response->code; 
  14. print "Return Code ====>\n $code\n";
  15. my $headers = $response->headers_as_string;
  16. #If the Error is 401.2 then, 
  17. #The authentication methods that were tried are either disabled, or you are attempting to use NTLM through a proxy server.
  18. print "Headers ======>\n $headers\n";
  19. # HTML body:
  20. my $body =  $response->content;
  21. print "================\n$body====================\n";
i am getting the following output after executing the script.

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/TR/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-1252">

<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 credentials that you supplied because your Web browser is sending a WWW-Authenticate header field that the Web server is not configured to accept.

<hr>

<p>Please try the following:</p>

<ul>

<li>Contact the Web site administrator if you believe you should be able to view this directory or page.</li>

<li>Click the <a href="javascript:location.reload()">Refresh</a> button to try again with different credentials.</li>

</ul>

<h2>HTTP Error 401.2 - Unauthorized: Access is denied due to server configuration.<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">Microsoft Product Support Services</a> and perform a title search for the words <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>
====================

and anybody could let me know whether the new code is correct, i mean the way i am sending the authentication details.

And one more thing i want to know is how can we make sure the http request we are making is going through proxy?

thanks a lot in advance.

Best Regards,
Mohan
Nov 11 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.