java通过url调用远程接口返回json数据,有用户名和密码验证
2017-12-15 18:41
1071 查看
最近有需要调用url远程接口来获取返回值,还有用户名和密码验证
使用http请求来获取接口返回值,代码如下
package com.kp.module;
import com.kp.constant.ServerConstant;
import java.util.Base64;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Tesalkt {
public static void main(String[] args) throws Exception {
StringBuffer buffer = new StringBuffer();
HttpURLConnection httpConn = null;
BufferedReader reader = null;
try {
//url远程接口
String strURL = ServerConstant.SERVER_RUNTIME_INTERFACE+"118.31.11.213";
//用户名
String username = ServerConstant.SERVER_RUNTIME_USRENAME;
//密码
String password = ServerConstant.SERVER_RUNTIME_PASSWORD;
//原先使用的时com.sun.org.apache.xml.internal.security.utils.Base64,这个包虽然在jdk中,
//但并不是标准的包,所以在gradle编译打包时总是无法引入改包,所以不用这个包
// String author = "Basic " + Base64.encode((username+":"+ password).getBytes());
//使用jdk1.8中的java.util.Base64来对字符串加密
String author = "Basic " + Base64.getEncoder().encodeToString((username+":"+ password).getBytes());
URL url = new URL(strURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Authorization", author);
httpConn.connect();
reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpConn != null) {
httpConn.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("接口返回值:"+buffer.toString());
}
}
使用http请求来获取接口返回值,代码如下
package com.kp.module;
import com.kp.constant.ServerConstant;
import java.util.Base64;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Tesalkt {
public static void main(String[] args) throws Exception {
StringBuffer buffer = new StringBuffer();
HttpURLConnection httpConn = null;
BufferedReader reader = null;
try {
//url远程接口
String strURL = ServerConstant.SERVER_RUNTIME_INTERFACE+"118.31.11.213";
//用户名
String username = ServerConstant.SERVER_RUNTIME_USRENAME;
//密码
String password = ServerConstant.SERVER_RUNTIME_PASSWORD;
//原先使用的时com.sun.org.apache.xml.internal.security.utils.Base64,这个包虽然在jdk中,
//但并不是标准的包,所以在gradle编译打包时总是无法引入改包,所以不用这个包
// String author = "Basic " + Base64.encode((username+":"+ password).getBytes());
//使用jdk1.8中的java.util.Base64来对字符串加密
String author = "Basic " + Base64.getEncoder().encodeToString((username+":"+ password).getBytes());
URL url = new URL(strURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Authorization", author);
httpConn.connect();
reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpConn != null) {
httpConn.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("接口返回值:"+buffer.toString());
}
}
相关文章推荐
- Java如何通过URL调用远程接口并读取返回信息?
- Java如何通过URL调用远程接口并读取返回信息?
- asp通过json调用webservice接口,并获取返回的xml数据及解析
- Java调用天气接口(百度天气)解析返回的JSON数据
- Java调用天气接口(百度天气)解析返回的JSON数据
- c# 通过访问URL,获取并解析远程服务端返回的复杂Json数据
- Java 通过HttpURLConnection Post方式提交json,并从服务端返回json数据
- Java 通过HttpURLConnection Post方式提交json,并从服务端返回json数据
- java 调用wsdl接口同时将返回数据解析成json
- Java调用天气接口(百度天气)解析返回的JSON数据
- Jquery Easy UI 中的datagrid通过url调用webservice返回json数据
- Java 通过HttpURLConnection Post方式提交json,并从服务端返回json数据
- Java 通过HttpURLConnection Post方式提交json,并从服务端返回json数据
- 微信相关 -- 可以通过调用下面的方法 请求对应的url 获取微信服务器返回的json数据
- java rest接口返回不完整的json数据
- JSON 数据传输 与调用远程接口
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- jQuery通过调用webservice返回json数据的问题
- 通过SSH协议访问远程Shell脚本-免密码登陆与返回数据丢失解决
- jboss-as-7.1.1.Final 使用client远程调用,用户名密码验证出错