`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;
}
}
}
`
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。