说明
模拟http请求,用于在服务端调用第三方api,抓取网页数据等等
主要方法
HttpUtil.Request(String method, String url,ZmHash query,ZmHash methodData,ZmHash header,ZmHash config);
Request 发起http请求
返回值:ZmReturnMsg ZmReturnMsg对象 msgStatus为ok时候代表请求成功,否则请求失败
method 请求方式 GET POST String,
url 请求的url地址 String,
query 请求url参数 键值对 执行时会拼接到url?后面 ZmHash,
methodData 请求方式数据 主要包含以下内容 type:请求子类型,可以传入 json、xml、plain、x-www-form-urlencoded、form-data、fileDown(文件下载)
也可以为空或者none data:post传递的内容 对于x-www-form-urlencoded、form-data传入ZmHash键值对
其它为字符串 files:上传文件时候传递文件数据的键值对列表ZmHashList filePath:下载文件保存的路径 ZmHash,
header 请求头部数据 ZmHash,
config 请求配置数据 ZmHash
示例
ZmScript
[RequestMapping("doHttpGet")]
@ResponseBody
public ZmReturnMsg doHttpGet() {
ZmReturnMsg mReturnMsg = ZmReturnMsg.ErrorMsg("doHttpGet");
return mReturnMsg;
}
/**
*演示Get方式的http请求
*/
[RequestMapping("httpGet")]
@ResponseBody
public ZmReturnMsg httpGet ()
{
ZmActiveRequest zmRequest = base.getActiveRequest();
String serverUrl =zmRequest.getRequestServerUrl();
String url = serverUrl+ "/demo/design/httputil/doHttpGet";
ZmHash query = new ZmHash();
query.Add("ajax",1);
ZmHash methodData = new ZmHash();
ZmHash header = new ZmHash();
ZmHash config = new ZmHash();
//Get请求调用doHttpGet
ZmReturnMsg mReturnMsg = HttpUtil.Request("GET", url, query, methodData, header, config);
return mReturnMsg;
}
[RequestMapping("doPostJson")]
[HttpPost]
@ResponseBody
public ZmReturnMsg doPostJson([RequestBody] DemoStudent student)
{
String StudentName=student.StudentName;
ZmReturnMsg mReturnMsg = ZmReturnMsg.ErrorMsg("doPostJson:"+StudentName);
return mReturnMsg;
}
/**
* 演示Get Json方式的http请求
*/
[RequestMapping("postJson")]
@ResponseBody
public ZmReturnMsg postJson ()
{
ZmActiveRequest zmRequest = base.getActiveRequest();
String serverUrl =zmRequest.getRequestServerUrl();
String url = serverUrl+ "/demo/design/httputil/doPostJson";
ZmHash query = new ZmHash();
query.Add("ajax",1);
ZmHash methodData = new ZmHash();
methodData.Add("type","json");
ZmHash student=new ZmHash();
student.Add("studentName","testDemoStudent");
student.Add("studentId",5);
student.Add("studentAge",18);
String data=JsonHelper.HashToJson(student);
methodData.Add("data",data);
ZmHash header = new ZmHash();
ZmHash config = new ZmHash();
////Post Json方式提交数据
ZmReturnMsg mReturnMsg = HttpUtil.Request("POST", url, query, methodData, header, config);
return mReturnMsg;
}
[RequestMapping("doPostFormUrlencoded")]
[HttpPost]
@ResponseBody
public ZmReturnMsg doPostFormUrlencoded()
{
ZmActiveRequest zmRequest = base.getActiveRequest();
String studentName=zmRequest.QueryString("studentName");
ZmReturnMsg mReturnMsg = ZmReturnMsg.ErrorMsg("doPostFormUrlencoded:"+studentName);
return mReturnMsg;
}
/**
*演示Post x-www-form-urlencoded方式的http请求
*/
[RequestMapping("postFormUrlencoded")]
@ResponseBody
public ZmReturnMsg postFormUrlencoded ()
{
ZmActiveRequest zmRequest = base.getActiveRequest();
String serverUrl =zmRequest.getRequestServerUrl();
String url = serverUrl+ "/demo/design/httputil/doPostFormUrlencoded";
ZmHash query = new ZmHash();
query.Add("ajax",1);
ZmHash methodData = new ZmHash();
methodData.Add("type","x-www-form-urlencoded");
methodData.Add("data","studentName=testDemoStudent&studentAge=18");
ZmHash header = new ZmHash();
ZmHash config = new ZmHash();
//Post x-www-form-urlencoded方式的http请求
ZmReturnMsg mReturnMsg = HttpUtil.Request("POST", url, query, methodData, header, config);
return mReturnMsg;
}
/**
*演示下载文件
*/
[RequestMapping("downFile")]
@ResponseBody
public ZmReturnMsg downFile ()
{
String url ="http://www.qiafa.com/hlt.rar";
ZmHash query = new ZmHash();
query.Add("ajax",1);
ZmHash methodData = new ZmHash();
methodData.Add("type","fileDown");
String filePath =ZmFile.Combine("C:","Users","zjx","Desktop","tmp","test","hlt.rar");
methodData.Add("filePath",filePath);
ZmHash header = new ZmHash();
ZmHash config = new ZmHash();
//下载文件
ZmReturnMsg mReturnMsg = HttpUtil.Request("GET", url, query, methodData, header, config);
return mReturnMsg;
}
/**
*上传文件
*/
[RequestMapping("uploadFile")]
@ResponseBody
public ZmReturnMsg uploadFile()
{
ZmActiveRequest zmRequest = base.getActiveRequest();
String serverUrl =zmRequest.getRequestServerUrl();
String url = serverUrl+ "/SysRepository/upload";
ZmHash query = new ZmHash();
query.Add("ajax",1);
query.Add("FileType","file");
query.Add("FormSNO","rich");
query.Add("sno","202212");
query.Add("FieldSNO","17");
query.Add("store","file");
query.Add("access_token","123456");
ZmHash methodData = new ZmHash();
methodData.Add("type","form-data");
ZmHashList files=new ZmHashList();
ZmHash file = new ZmHash();
String filePath =ZmFile.Combine("C:","icon.png");
file.Add("filePath",filePath);
file.Add("fileName","icon.png");
files.Add(file);
methodData.Add("files",files);
ZmHash header = new ZmHash();
ZmHash config = new ZmHash();
//上传文件
ZmReturnMsg mReturnMsg = HttpUtil.Request("POST", url, query, methodData, header, config);
return mReturnMsg;
}
[RequestMapping("doWithHttpHeader")]
@ResponseBody
public ZmReturnMsg doWithHttpHeader() {
ZmActiveRequest zmRequest = base.getActiveRequest();
String ua=zmRequest.GetUserAgent();
String referer = zmRequest.GetReferer();
ZmReturnMsg mReturnMsg=ZmReturnMsg.ErrorMsg("doWithHttpHeader:"+referer+" "+ ua);
return mReturnMsg;
}
/**
*添加请求的 http头
*/
[RequestMapping("withHttpHeader")]
@ResponseBody
public ZmReturnMsg withHttpHeader ()
{
ZmActiveRequest zmRequest = base.getActiveRequest();
String serverUrl =zmRequest.getRequestServerUrl();
String url = serverUrl+ "/demo/design/httputil/doWithHttpHeader";
ZmHash query = new ZmHash();
query.Add("ajax",1);
ZmHash methodData = new ZmHash();
ZmHash header = new ZmHash();
header.Add("Referer", "http://www.xxx.com");
header.Add("UserAgent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.138 Safari/537.36");
ZmHash config = new ZmHash();
//添加了 Referer 和ua 并发起http请求
ZmReturnMsg mReturnMsg = HttpUtil.Request("GET", url, query, methodData, header, config);
return mReturnMsg;
}
[RequestMapping("doWithCookia")]
@ResponseBody
public ZmReturnMsg doWithCookia() {
ZmActiveRequest zmRequest = base.getActiveRequest();
String myCookieVal= zmRequest.GetCookie("myCookie");
ZmReturnMsg mReturnMsg=ZmReturnMsg.ErrorMsg("doWithCookia:"+ myCookieVal);
//同时设置了 http请求cookie
zmRequest.SetCookie("responseCookia", "888");
zmRequest.SetCookie("nextCookia", "999");
return mReturnMsg;
}
/**
*添加cookie的 http请求
*/
[RequestMapping("withCookia")]
@ResponseBody
public ZmReturnMsg withCookia ()
{
ZmActiveRequest zmRequest = base.getActiveRequest();
String serverUrl =zmRequest.getRequestServerUrl();
String url = serverUrl+ "/demo/design/httputil/doWithCookia";
ZmHash query = new ZmHash();
query.Add("ajax",1);
ZmHash methodData = new ZmHash();
ZmHash header = new ZmHash();
Boolean useCookieObj=false;
//添加 cookie的两种方式 一种是使用对象 另外一种是用key=val的形式
if(useCookieObj){
ZmHashList CookieObj=new ZmHashList();
ZmHash myCookie = new ZmHash();
myCookie.Add("name","myCookie");
myCookie.Add("value","123");
CookieObj.Add(myCookie);
header.Add("CookieObj",CookieObj);
}
else{
header.Add("Cookie", "myCookie=123");
}
ZmHash config = new ZmHash();
//请求配置里面接收服务端响应过来的cookie
config.Add("GetResponseCookie",true);
//发起有cookie的请求
ZmReturnMsg mReturnMsg = HttpUtil.Request("GET", url, query, methodData, header, config);
return mReturnMsg;
}
模拟http请求的基本操作
调用第三方api
普通的get请求
post json请求,post x-www-form-urlencoded 请求
请求添加 http头 refer ua等信息
上传和下载文件
请求添加cookie信息,获取服务端返回的cookie信息