NC调用HTTPS接口请求实例

`package nc.sf.itf.http;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.x509trustmanager;

import nc.itf.plugin.hrdata.SSOloginDutil;
import nc.uap.lfw.core.util.HttpsHelper;

import org.apache.http.httpentity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import com.sun.org.apache.xml.internal.security.utils.Base64;

public class HttpUtils extends DefaultHttpClient {

public static String httpURLConnectionGet(String authName,String password,String strUrl) throws UnsupportedEncodingException{
    BufferedReader in = null;
	
    String authHeader = "Basic " + new String(Base64.encode((authName + ":" + password).getBytes("UTF-8")));

    try {
        //获取访问地址URL,注意GET请求若URL中包含中文字符的话,在高版本TOMCAT中会认为是不合法字符,可改为POST方式
        URL url = new URL(strUrl);
        //创建HttpURLConnection对象
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        HttpsHelper.initSSLCon(conn);
        //添加请求头

// conn.setRequestProperty("USER-TOKEN","eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJyc2oiLCJ1c2VySWQiOiJyZWRpczp1c2VyOnRva2VuOjI3IiwiaWF0IjoxNjAzMzUwODcyLCJleHAiOjE2MDM5NTU2NzJ9.3KOsFrzpJbz_p5vjVzL3sGTWFm1yAKAAaDpKeg4DJDw");
//请求方式,认是GET
conn.setRequestMethod("GET");
//连接主机的超时时间(单位:毫秒)
conn.setConnectTimeout(30000);
//设置认证
conn.setRequestProperty("Authorization",authHeader);
//从主机读取数据的超时时间(单位:毫秒)
conn.setReadTimeout(30000);
//设置是否输出,认是false
conn.setDoOutput(false);
//设置是否读入,认是true
conn.setDoInput(true);
String result = "";
int code = conn.getResponseCode();
if(code == 200){
//定义BufferedReader输入流读取响应,getInputStream()会自动建立连接,无需手动调用connect()连接
in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
//关闭输入流
in.close();
}
return result;
} catch (Exception e) {
e.printstacktrace();
return null;
}
}

}
`

相关文章

可以认为OpenFeign是Feign的增强版,不同的是OpenFeign支持S...
为进一步规范小程序交易生态、提升用户购物体验、满足用户在...
云原生之使用Docker部署Dashdot服务器仪表盘
本文主要描述TensorFlow之回归模型的基本原理
1.漏洞描述Apache Druid 是一个集时间序列数据库、数据仓库和...
内部类(当作类中的一个普通成员变量,只不过此成员变量是cl...