Matlab网络请求操作

作者:matrix 被围观: 10,376 次 发布时间:2019-06-22 分类:零零星星 | 6 条评论 »

这是一个创建于 1762 天前的主题,其中的信息可能已经有所发展或是发生改变。

matlab这个学术界的编程语言我是几个月前听说,有些基本操作是真的麻烦,当然在其他方面matlab是相当🐂🍺

使用Matlab进行GET/POST请求调用接口,提交参数。

之前苦于没有将get请求的query参数进行变量提取,好在www.mathworks.com找到有文档说明

GET请求操作函数封装

get网络请求封装成函数get_request

%GET网络请求
function [result]=get_request(uri,query)

    import matlab.net.* %导入Matlab网络请求库
    import matlab.net.http.*

    uri = URI(uri);%请求地址;
    uri.Query = matlab.net.QueryParameter(query);%get  附加请求参数 

    r = RequestMessage;
    r.Method = 'GET';%使用GET请求类型

    response = send(r,uri);%发送请求
    status = response.StatusCode%获取服务器响应的状态码
    if (status==200) %一般成功响应的状态码为200 表示ok 即成功
        content = response.Body.Data; %获取服务器响应的内容
        result = content;
    else
        disp('请求失败')
    end

说明:

参数1 uri: 请求url, 如:http://hhtjim.com
参数2 query: get请求参数 传入struct类型数据
还有就是接口返回json数据(response响应头有指定type json),matlab会自动解析json数据,最后用response.Body.Data调用就好,不方便查看的那就debug。

如果要手动处理json字符串的话使用jsonencode, jsondecode进行编解操作。

调用举例:
请求:http://hhtjim.com?arg=123

get_request('http://hhtjim.com',struct('arg', '123'))

或者

query.arg = '123'
get_request('http://hhtjim.com',query)

上面两种方式的调用都可以完成操作。matlab内部会自己封装拼接为http://hhtjim.com?arg=123进行请求。

如果想要更简单的ge请求可以使用webread进行操作。

POST请求

这里测试的POST的请求更为复杂,会添加自定义请求头,post body表单内容,cookie的涉及。所以没有封装成统一调用的函数,需要的自行修改咯~

import matlab.net.* %倒入Matlab网络请求库
import matlab.net.http.*

uri = URI('http://localhost:8080/testOrder/double_param/');%请求地址

%添加请求参数
qStruct.K=1;
qStruct.d2=2;
qStruct.d1=3;
qStruct.L=4;
qStruct.order_switch=1;%1开 0关
qStruct.order_size=2;
qStruct.diff_earnings=1.00009;



r = RequestMessage;
r.Method = 'POST'%使用POST请求类型
csrf = 'YYC0e8GICcEroZGDuL8THJ4ZQdwQpDqNtkBsfnaBP0XpH3rqYVNXADJGpWdo53o0'%Djabgo框架表单中防止跨站请求的参数,其实这是服务器生成的伪随机字符
qStruct.csrfmiddlewaretoken=csrf;
r.Body = matlab.net.QueryParameter(qStruct)%放入请求参数

%添加请求头
r = addFields(r,'Content-Type','application/x-www-form-urlencoded');
r = addFields(r,'Cookie',sprintf('csrftoken=%s',csrf));


response = send(r,uri);%发送请求
status = response.StatusCode%获取服务器响应的状态码
if (status==200) %一般成功响应的状态码为200 表示ok 即成功
    content = response.Body.Data %获取服务器响应的内容
    disp('请求成功:')
    disp(content)
else
    disp('请求失败')
end

%参考:
%https://ww2.mathworks.cn/help/matlab/http-interface.html
%https://ww2.mathworks.cn/help/matlab/ref/matlab.net.http.requestmessage.send.html#bu199af-6
%https://www.mathworks.com/help/matlab/ref/matlab.net.http.message.addfields.html
%https://ww2.mathworks.cn/help/matlab/ref/matlab.net.queryparameter-class.html

最后我真想说术业有专攻,用matlab进行网络请求是的自讨苦吃。

参考:

https://ww2.mathworks.cn/help/matlab/ref/matlab.net.uri-class.html#bvflp65-2

https://ww2.mathworks.cn/help/matlab/http-interface.html
https://ww2.mathworks.cn/help/matlab/ref/matlab.net.http.requestmessage.send.html#bu199af-6
https://www.mathworks.com/help/matlab/ref/matlab.net.http.message.addfields.html
https://ww2.mathworks.cn/help/matlab/ref/matlab.net.queryparameter-class.html

其他文章:
本文固定链接:https://www.hhtjim.com/matlab-network-request-operation.html
matrix
本文章由 matrix 于2019年06月22日发布在零零星星分类下,目前没有通告,你可以至底部留下评论。
转载请注明:Matlab网络请求操作-HHTjim'S 部落格
关键字:

有6 条评论 »

  1. 子午物联网 子午物联网 2019-6-25 14:59:36 +0800#3

    这个我能看懂

  2. 1900 1900 2019-6-23 16:00:20 +0800#2

    解析貌似又出问题了,能解决么?
    http://m8c.music.126.net/20190623161840/1af3c59da269ccdd11a24a2c449113b6/ymusic/055d/065f/515d/272b5d606115f57d5942f79561148769.mp3?bitrate=128000 403 (Forbidden)

    • matrix matrix Moderator 2019-6-24 0:29:32 +0800

      测试几个id都可以的呀~
      发个url我测试下。

      • 1900 1900 2019-6-24 9:13:13 +0800

        我单独解析是可以的,但是用到网站利会提示403的错误,但是有时候有可以解析,就很奇怪。

        • matrix matrix Moderator 2019-6-24 12:58:25 +0800

          原因在于网易有些域名有referer来源的检测 像m8c.music.163.net这种,已经处理掉了(直接删掉c)
          其他办法也有很多

          • 用官方接口music.163.com/song/media/outer/url?id=28713832.mp3,
          • 用link站的?proxy反向代理操作
          • 设置网页全局的<meta name="referrer" content="no-referrer" />,
  3. antior antior 2019-6-22 19:45:02 +0800#1

    Mark 了

添加新评论 »

 🙈 😱 😂 😛 😭 😳 😀 😆 👿 😉 😯 😮 😕 😎 😐 😥 😡 😈 💡

插入图片

NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!