首页 技术 正文
技术 2022年11月16日
0 收藏 626 点赞 2,861 浏览 2585 个字

前言

近期研究如何利用java代码如何获取其他系统中所需的数据,自己总结的方法如下:

1.工具类代码

 /**
* <pre>
* 方法体说明:向远程接口发起请求,返回字符串类型结果
* @param url 接口地址
* @param requestMethod 请求类型
* @param params 传递参数
* @return String 返回结果
* </pre>
*/
public static String httpRequestToString(String url, String requestMethod,
Map<String, String> params, String ...auth){
//接口返回结果
String requestResult = null;
try {
String parameters = "";
boolean hasParams = false;
//将参数集合拼接成特定格式,如name=zhangsan&age=24
for(String key : params.keySet()){
String value = URLEncoder.encode(params.get(key), "UTF-8");
parameters += key +"="+ value +"&";
hasParams = true;
}
if(hasParams){
parameters = parameters.substring(0, parameters.length()-1);
}
//是否为GET方式请求
boolean isGet = "get".equalsIgnoreCase(requestMethod);
boolean isPost = "post".equalsIgnoreCase(requestMethod);
boolean isPut = "put".equalsIgnoreCase(requestMethod);
boolean isDelete = "delete".equalsIgnoreCase(requestMethod); //创建HttpClient连接对象
DefaultHttpClient client = new DefaultHttpClient();
HttpRequestBase method = null;
if(isGet){
url += "?" + parameters;
method = new HttpGet(url);
}else if(isPost){
method = new HttpPost(url);
HttpPost postMethod = (HttpPost) method;
StringEntity entity = new StringEntity(parameters);
postMethod.setEntity(entity);
}else if(isPut){
method = new HttpPut(url);
HttpPut putMethod = (HttpPut) method;
StringEntity entity = new StringEntity(parameters);
putMethod.setEntity(entity);
}else if(isDelete){
url += "?" + parameters;
method = new HttpDelete(url);
}
method.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
//设置参数内容类型
method.addHeader("Content-Type","application/x-www-form-urlencoded");
//httpClient本地上下文
HttpClientContext context = null;
if(!(auth==null || auth.length==0)){
String username = auth[0];
String password = auth[1];
UsernamePasswordCredentials credt = new UsernamePasswordCredentials(username,password);
//凭据提供器
CredentialsProvider provider = new BasicCredentialsProvider();
//凭据的匹配范围
provider.setCredentials(AuthScope.ANY, credt);
context = HttpClientContext.create();
context.setCredentialsProvider(provider);
}
//访问接口,返回状态码
HttpResponse response = client.execute(method, context);
//返回状态码200,则访问接口成功
if(response.getStatusLine().getStatusCode()==200){
requestResult = EntityUtils.toString(response.getEntity());
}
client.close();
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return requestResult;
}

2.测试代码

public static void main(String[] args) {
//url中获取的登录的接口
String url ="https://●●●●●●●●●●●●/login/validate.do";
Map<String, String> params = new HashMap<>();
// 用户名
params.put("username","LZZHXF");
// 密码
params.put("password","●●●●●●●");
// 是否记住密码
params.put("isRemenber","true");
// 根据用户名、密码调用登录接口返回token
String string = httpRequestToString(url, "post", params);
System.out.println(string);
}

返回如下图所示:

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,022
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,773
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,851