开发者社区> 问答> 正文

https 用 HttpsURLConnectio如何登陆 post方式:报错

不是进入登陆页面,而是通过用户 名称和密码,进入到系统欢迎页面。

展开
收起
kun坤 2020-06-06 13:47:35 470 0
1 条回答
写回答
取消 提交回答
  • 不知这个工具类可否解决你的问题。

    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.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.NoSuchProviderException;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    import java.util.Map;
    import java.util.Map.Entry;
    
    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSession;
    import javax.net.ssl.SSLSocketFactory;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    
    /**
     * http util
     * @author biezhi
     * @since 1.0
     */
    public class HttpUtil {
    
    	private static final String DEFAULT_CHARSET = "UTF-8"; // 默认字符集
    
    	private static final String _GET = "GET"; // GET
    	private static final String _POST = "POST";// POST
    
    	/**
    	 * 初始化http请求参数
    	 */
    	private static HttpURLConnection initHttp(String url, String method, Map<String, String> headers)
    			throws IOException {
    		URL _url = new URL(url);
    		HttpURLConnection http = (HttpURLConnection) _url.openConnection();
    		// 连接超时
    		http.setConnectTimeout(25000);
    		// 读取超时 --服务器响应比较慢,增大时间
    		http.setReadTimeout(25000);
    		http.setRequestMethod(method);
    		http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    		http.setRequestProperty("User-Agent",
    				"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
    		if (null != headers && !headers.isEmpty()) {
    			for (Entry<String, String> entry : headers.entrySet()) {
    				http.setRequestProperty(entry.getKey(), entry.getValue());
    			}
    		}
    		http.setDoOutput(true);
    		http.setDoInput(true);
    		http.connect();
    		return http;
    	}
    
    	/**
    	 * 初始化http请求参数
    	 */
    	private static HttpsURLConnection initHttps(String url, String method, Map<String, String> headers)
    			throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
    		TrustManager[] tm = { new MyX509TrustManager() };
    		SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
    		sslContext.init(null, tm, new java.security.SecureRandom());
    		// 从上述SSLContext对象中得到SSLSocketFactory对象  
    		SSLSocketFactory ssf = sslContext.getSocketFactory();
    		URL _url = new URL(url);
    		HttpsURLConnection http = (HttpsURLConnection) _url.openConnection();
    		// 设置域名校验
    		http.setHostnameVerifier(new HttpUtil().new TrustAnyHostnameVerifier());
    		// 连接超时
    		http.setConnectTimeout(25000);
    		// 读取超时 --服务器响应比较慢,增大时间
    		http.setReadTimeout(25000);
    		http.setRequestMethod(method);
    		http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    		http.setRequestProperty("User-Agent",
    				"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
    		if (null != headers && !headers.isEmpty()) {
    			for (Entry<String, String> entry : headers.entrySet()) {
    				http.setRequestProperty(entry.getKey(), entry.getValue());
    			}
    		}
    		http.setSSLSocketFactory(ssf);
    		http.setDoOutput(true);
    		http.setDoInput(true);
    		http.connect();
    		return http;
    	}
    
    	/**
    	 * get请求
    	 */
    	public static String get(String url, Map<String, String> params, Map<String, String> headers) {
    		StringBuffer bufferRes = null;
    		try {
    			HttpURLConnection http = null;
    			if (isHttps(url)) {
    				http = initHttps(initParams(url, params), _GET, headers);
    			} else {
    				http = initHttp(initParams(url, params), _GET, headers);
    			}
    			InputStream in = http.getInputStream();
    			BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
    			String valueString = null;
    			bufferRes = new StringBuffer();
    			while ((valueString = read.readLine()) != null) {
    				bufferRes.append(valueString);
    			}
    			in.close();
    			if (http != null) {
    				http.disconnect();// 关闭连接
    			}
    			return bufferRes.toString();
    		} catch (Exception e) {
    			e.printStackTrace();
    			return null;
    		}
    	}
    
    	/**
    	 * get请求
    	 */
    	public static String get(String url) {
    		return get(url, null);
    	}
    
    	/**
    	 * get请求
    	 */
    	public static String get(String url, Map<String, String> params) {
    		return get(url, params, null);
    	}
    
    	/**
    	 * post请求
    	 */
    	public static String post(String url, String params, Map<String, String> headers) {
    		StringBuffer bufferRes = null;
    		try {
    			HttpURLConnection http = null;
    			if (isHttps(url)) {
    				http = initHttps(url, _POST, headers);
    			} else {
    				http = initHttp(url, _POST, headers);
    			}
    			OutputStream out = http.getOutputStream();
    			out.write(params.getBytes(DEFAULT_CHARSET));
    			out.flush();
    			out.close();
    
    			InputStream in = http.getInputStream();
    			BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
    			String valueString = null;
    			bufferRes = new StringBuffer();
    			while ((valueString = read.readLine()) != null) {
    				bufferRes.append(valueString);
    			}
    			in.close();
    			if (http != null) {
    				http.disconnect();// 关闭连接
    			}
    			return bufferRes.toString();
    		} catch (Exception e) {
    			e.printStackTrace();
    			return null;
    		}
    	}
    
    	/**
    	 * post请求
    	 */
    	public static String post(String url, Map<String, String> params) {
    		return post(url, map2Url(params), null);
    	}
    
    	/**
    	 * post请求
    	 */
    	public static String post(String url, Map<String, String> params, Map<String, String> headers) {
    		return post(url, map2Url(params), headers);
    	}
    
    	/**
    	 * 初始化参数
    	 */
    	public static String initParams(String url, Map<String, String> params) {
    		if (null == params || params.isEmpty()) {
    			return url;
    		}
    		StringBuilder sb = new StringBuilder(url);
    		if (url.indexOf("?") == -1) {
    			sb.append("?");
    		}
    		sb.append(map2Url(params));
    		return sb.toString();
    	}
    
    	/**
    	 * map转url参数
    	 */
    	public static String map2Url(Map<String, String> paramToMap) {
    		if (null == paramToMap || paramToMap.isEmpty()) {
    			return null;
    		}
    		StringBuffer url = new StringBuffer();
    		boolean isfist = true;
    		for (Entry<String, String> entry : paramToMap.entrySet()) {
    			if (isfist) {
    				isfist = false;
    			} else {
    				url.append("&");
    			}
    			url.append(entry.getKey()).append("=");
    			String value = entry.getValue();
    			if (StringUtils.isNotEmpty(value)) {
    				try {
    					url.append(URLEncoder.encode(value, DEFAULT_CHARSET));
    				} catch (UnsupportedEncodingException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    		return url.toString();
    	}
    
    	/**
    	 * 检测是否https
    	 */
    	private static boolean isHttps(String url) {
    		return url.startsWith("https");
    	}
    
    	/**
    	 * https 域名校验
    	 * @author biezhi
    	 * @since 1.0
    	 */
    	public class TrustAnyHostnameVerifier implements HostnameVerifier {
    		public boolean verify(String hostname, SSLSession session) {
    			return true;// 直接返回true
    		}
    	}
    }
    
    // 证书管理
    class MyX509TrustManager implements X509TrustManager {
    
    	public X509Certificate[] getAcceptedIssuers() {
    		return null;
    	}
    
    	@Override
    	public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    	}
    
    	@Override
    	public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    	}
    }



    ######设置Method为Post不就行了,你是想问什么?######不登录如何到欢迎界面去?######

    你用fiddler 或firebug 抓下请求 然后 在按照请求的顺序 逐个发送请求 


    ###### ######

    GET与POST安全性几乎是相同的,你不用考虑这个,用fireBug一下就看到传的内容了

    你的问题主要是https的连接方式与传参方法是问题的重点

    https Request(加密通道传参数)

    ######其实post和get很多时候用起来差不多,,,
    2020-06-06 13:47:45
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
CDN助力企业网站进入HTTPS时代 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载