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;
}
}

}
`

上一篇:Flink之state processor api读取checkpoint文件


下一篇:spark中生成RDD时分区规则是怎样的?(只需要看getPartitions方法的逻辑就可以了)