matlab urlread中文,如何使用Matlab urlread/urlwrite提供用户名/密码来访问Web资源?

urlwrite_auth是下一步,所以这里是......

function [output,status]=urlwrite_auth(url, user, password,location,wanted)

%URLWRITE_AUTH Like URLWRITE, with basic authentication

%

% location is where you want the file saved

% wanted is the name of the file you want

% Returns the output file which is now saved to location.

%

% Examples:

% sampleUrl = 'http://browserspy.dk/password-ok.php';

% [output,status] = urlwrite_auth(sampleUrl, 'user', 'password', location, wanted);

% Matlab's urlread() doesn't do HTTP Request params, so work directly with Java

jUrl = java.net.URL(url);

conn = jUrl.openConnection();

conn.setRequestProperty('Authorization', ['Basic ' base64encode([user ':' password])]);

conn.connect()

%note this calls the function below

% Specify the full path to the file so that getAbsolutePath will work when the

% current directory is not the startup directory and urlwrite is given a

% relative path.

file = java.io.File(location);

% the path.

try

file = file.getCanonicalFile;

catch

error('MATLAB:urlwrite:InvalidOutputLocation','Could not resolve file "%s".',char(file.getAbsolutePath));

end

% Open the output file.

pathy=strcat(location,'\',wanted);

try

fileOutputStream = java.io.FileOutputStream(pathy);

catch

error('MATLAB:urlwrite:InvalidOutputLocation','Could not open output file "%s".',char(file.getAbsolutePath));

end

% Read the data from the connection.

try

inputStream = conn.getInputStream;

import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;

% This StreamCopier is unsupported and may change at any time.

isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;

isc.copyStream(inputStream,fileOutputStream);

inputStream.close;

fileOutputStream.close;

output = char(file.getAbsolutePath);

catch

fileOutputStream.close;

delete(file);

if catchErrors, return

else error('MATLAB:urlwrite:ConnectionFailed','Error downloading URL. Your network connection may be down or your proxy settings improperly configured.');

end

end

status = 1;

function out = base64encode(str)

% Uses Sun-specific class, but we know that is the JVM Matlab ships with

encoder = sun.misc.BASE64Encoder();

out = char(encoder.encode(java.lang.String(str).getBytes()));

%this is the bit of code that makes it connect!!!!请注意,这是Andrew从http用户名和密码站点下载文件的答案的开发。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用matlab批量下载网站上的文件-MatlabUrlDownloadToFile.m 前几天美国数学建模成绩出了,我们按照网址下载了自己的证书,另外想看看学校今年的获奖情况,由于学校的参赛队伍数众多,一个一个下载证书再统计显得麻烦,于是想自己用matlab开发一个小程序以实现同类型文件的批量下载。很多网站上有介绍使用Windows API 函数URLDownloadToFile,但是这个在VC上实现容易,要把它移植到matlab上还得使用MEX,有点麻烦了。这几天一直徘徊个大小网站,没有找到相关信息。最后还是自己使用lookfor找到需要的函UrlWrite,调用这个函数轻松搞定批量下载! 以下是代码: %使用matlab下载网站上的文件 %使用urlwrite函数下载网站上的文件 %特别适用于批量下载同类型的文件 % Created by longwen36 2011-4-16 %使用urlwrite实现批量下载 %以下载2011美赛数模证书为例 clc,clear; num = 11701:11710; URLs = cell); folder_filenames = cell); filenames = cell); for idx  = 1:length;     URLs{idx} = sprintf);     filenames{idx} = sprintf); end tic; for idx = 1:length;     fprintf;     [f, status] = urlwrite;     if status == 1;         fprintf;     else         fprintf;     end     end etime = toc; fprintf,etime); 复制代码 下载速度还挺快的,才发现,matlab功能确实很强大,函数很齐全。利用这个函数,加以改进,编写GUI,就可以做个简易的批量下载器了! 感兴趣的同仁可以试试!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值