栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HTTP之NTML认证

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HTTP之NTML认证

package com.byd.mes2.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


public class HttpNtmlRequest {
	
	public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException {
		test();
	}
 
 
	public static void test() throws KeyManagementException, NoSuchAlgorithmException {
 
		String urlStr = "https://test.test.com";
 
		String formData = "";
 
		try {
			urlStr += URLEncoder.encode(formData, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		System.out.println(urlStr);
 
		String domain = ""; // May also be referred as realm
		String userName = "user";
		String password = "password";
		String responseText = null;
		try {
			
//			responseText = getAuthenticatedResponse(urlStr, domain, userName, password);
			
			responseText = postAuthenticatedResponse(urlStr, domain, userName, password);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("response: " + responseText);
 
	}
 
	private static String getAuthenticatedResponse(final String urlStr, final String domain, final String userName,
			final String password) throws IOException {
 
		StringBuilder response = new StringBuilder();
 
		Authenticator.setDefault(new Authenticator() {
			@Override
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(domain + "\" + userName, password.toCharArray());
			}
		});
		URL urlRequest = new URL(urlStr);
		HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setRequestMethod("GET");
 
		System.out.println(conn.getContentLengthLong());
		System.out.println(conn.getResponseMessage());
		InputStream stream = conn.getInputStream();
		BufferedReader in = new BufferedReader(new InputStreamReader(stream));
		String str = "";
		while ((str = in.readLine()) != null) {
			response.append(str);
		}
		in.close();
 
		return response.toString();
	}
 
	private static String postAuthenticatedResponse(final String urlStr, final String domain, final String userName,
			final String password) throws IOException, KeyManagementException, NoSuchAlgorithmException {
 
		StringBuilder response = new StringBuilder();
 
		Authenticator.setDefault(new Authenticator() {
			@Override
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(domain + "\" + userName, password.toCharArray());
			}
		});
 
		URL urlRequest = new URL(urlStr);
		
		//创建SSLContext对象,并使用我们指定的信任管理器初始化
		X509TrustManager tm = new X509TrustManager() {
			public void checkClientTrusted(X509Certificate[] xcs, String string)
					throws CertificateException {
			}
 
			public void checkServerTrusted(X509Certificate[] xcs, String string)
					throws CertificateException {
			}
 
			public X509Certificate[] getAcceptedIssuers() {
				return null;
			}
		};
		//jdk1.7默认为TLSv1.0,需要修改为TLSv1.2
        SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); 
        sslContext.init(null, new TrustManager[] { tm }, null);

        //从上述SSLContext对象中得到SSLSocketFactory对象
        SSLSocketFactory ssf = sslContext.getSocketFactory();

        // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
        HttpsURLConnection conn = (HttpsURLConnection) urlRequest.openConnection();
        
        //创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
        conn.setSSLSocketFactory(ssf);
		
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setRequestMethod("POST");
 
		OutputStream out = conn.getOutputStream();
		// 写入请求的字符串
		String obj = "";
        out.write((obj.toString()).getBytes("UTF-8"));
		out.flush();
		InputStream in = conn.getInputStream();
		// read .....
		System.out.println("Responce Code:    " + conn.getResponseCode());
		System.out.println("Responce Message: " + conn.getResponseMessage());
 
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String str = "";
		while ((str = br.readLine()) != null) {
			response.append(str);
		}
		out.close();
		in.close();
 
		return response.toString();
	}
	
 
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/331710.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号