httpclient3.1发送https 的post请求,带参

package com.dahantc.fcchannel.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;

public class LogInTest {

	public static void main(String[] args) throws Exception, IOException {

		String userName = "userName";
		String passWord = "passWord";
		String loginPath = "https://111.111.111.111/login";
		String code = "123";
		String cookie = "JSESSIONID=97BC4FCDDAA7AE490AED5EF307505CAE";
		logIn(loginPath, cookie, userName, passWord, code);

	}

	/**
	 * 登录
	 * 
	 * @param url
	 * @param cookie
	 * @param userName
	 * @param passWord
	 * @param code
	 * @return
	 */
	public static boolean logIn(String url, String cookie, String userName, String passWord,
			String code) {
		boolean flag = false;
		System.out.println("===============登录开始===============");
		HttpClient httpclient = new HttpClient();
		PostMethod method = new PostMethod(url);
		Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
		Protocol.registerProtocol("https", myhttps);
		//
		method.setRequestHeader(new Header("Cookie", cookie));
		method.setRequestHeader(new Header("Host", "111.111.111.111"));
		method.setRequestHeader(new Header("Origin", "https://111.111.111.111"));
		method.setRequestHeader(new Header("Referer",
				"https://111.111.111.111/loginIndex.do"));
		method.setRequestHeader(new Header(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36"));

		NameValuePair[] param = { new NameValuePair("userName", userName),
				new NameValuePair("passWord", passWord), new NameValuePair("code", code) };
		method.setRequestBody(param);

		try {
			httpclient.executeMethod(method);
			String result = method.getResponseBodyAsString();
			System.out.println(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("=================登录结束-==============");
		return flag;
	}

}

/**
 * 
 */
class MySSLProtocolSocketFactory implements ProtocolSocketFactory {

	private SSLContext sslcontext = null;

	private SSLContext createSSLContext() {
		SSLContext sslcontext = null;
		try {
			sslcontext = SSLContext.getInstance("SSL");
			sslcontext.init(null, new TrustManager[] { new TrustAnyTrustManager() },
					new java.security.SecureRandom());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		}
		return sslcontext;
	}

	private SSLContext getSSLContext() {
		if (this.sslcontext == null) {
			this.sslcontext = createSSLContext();
		}
		return this.sslcontext;
	}

	public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
			throws IOException, UnknownHostException {
		return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
	}

	public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
		return getSSLContext().getSocketFactory().createSocket(host, port);
	}

	public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
			throws IOException, UnknownHostException {
		return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
	}

	public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
			HttpConnectionParams params) throws IOException, UnknownHostException,
			ConnectTimeoutException {
		if (params == null) {
			throw new IllegalArgumentException("Parameters may not be null");
		}
		int timeout = params.getConnectionTimeout();
		SocketFactory socketfactory = getSSLContext().getSocketFactory();
		if (timeout == 0) {
			return socketfactory.createSocket(host, port, localAddress, localPort);
		} else {
			Socket socket = socketfactory.createSocket();
			SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
			SocketAddress remoteaddr = new InetSocketAddress(host, port);
			socket.bind(localaddr);
			socket.connect(remoteaddr, timeout);
			return socket;
		}
	}

	// 自定义私有类
	private static class TrustAnyTrustManager implements X509TrustManager {

		public void checkClientTrusted(X509Certificate[] chain, String authType)
				throws CertificateException {
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType)
				throws CertificateException {
		}

		public X509Certificate[] getAcceptedIssuers() {
			return new X509Certificate[] {};
		}
	}

}

猜你喜欢

转载自278653219.iteye.com/blog/2327413