。。。
。。。
。。。
- import java.io.InputStream;
- import java.net.URI;
- import java.security.cert.CertificateException;
- import java.security.cert.X509Certificate;
- import java.util.ArrayList;
- import java.util.List;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import org.apache.commons.io.IOUtils;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.CookieStore;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.protocol.ClientContext;
- import org.apache.http.client.utils.URIUtils;
- import org.apache.http.client.utils.URLEncodedUtils;
- 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.BasicCookieStore;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.impl.conn.SingleClientConnManager;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.params.CoreConnectionPNames;
- import org.apache.http.protocol.BasicHttpContext;
- import org.apache.http.protocol.HttpContext;
- public class Main {
- public static void main(String[] args) throws Exception {
- TrustManager easyTrustManager = new X509TrustManager() {
- public void checkClientTrusted(X509Certificate[] chain,
- String authType) throws CertificateException {
- System.out.println("checkClientTrusted()...");
- }
- public void checkServerTrusted(X509Certificate[] chain,
- String authType) throws CertificateException {
- System.out.println("checkServerTrusted()...");
- }
- public X509Certificate[] getAcceptedIssuers() {
- return null;
- }
- };
- // ================================================================
- // 配置连接上下文
- CookieStore cookieStore = new BasicCookieStore();
- HttpContext localContext = new BasicHttpContext();
- localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
- SSLContext sslcontext = SSLContext.getInstance("TLS");
- sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);
- SSLSocketFactory sf = new SSLSocketFactory(sslcontext,
- SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- Scheme https = new Scheme("htt ps", 443, sf);
- SchemeRegistry sr = new SchemeRegistry();
- sr.register(https);
- localContext.setAttribute(ClientContext.SCHEME_REGISTRY, sr);
- // ================================================================
- // 配置 HttpClient
- ClientConnectionManager cm = new SingleClientConnManager();
- HttpClient httpclient = new DefaultHttpClient(cm);
- // 配置超时
- httpclient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
- 60 * 1000);
- httpclient.getParams().setIntParameter(
- CoreConnectionPNames.CONNECTION_TIMEOUT, 60 * 1000);
- // ================================================================
- // 准备POST请求。
- // URL上的参数
- List<NameValuePair> getParams = new ArrayList<NameValuePair>();
- getParams.add(new BasicNameValuePair("r", Long.toString(System
- .currentTimeMillis())));
- URI uri = URIUtils.createURI("https", "auth.alipay.com", -1,
- "/login/index.htm", URLEncodedUtils.format(getParams, "GBK"),
- null);
- HttpPost httpPost = new HttpPost(uri);
- // POST提交的参数
- List<NameValuePair> postParams = new ArrayList<NameValuePair>();
- postParams.add(new BasicNameValuePair("logonId", "li2@2b.cn"));
- postParams.add(new BasicNameValuePair("password", "91741"));
- httpPost.setEntity(new UrlEncodedFormEntity(postParams, "GBK"));
- // ================================================================
- // 结合以上三者,执行请求并获取结果
- HttpResponse response = httpclient.execute(httpPost, localContext);
- // ================================================================
- // 解析结果
- HttpEntity entity = response.getEntity();
- InputStream instream = entity.getContent();
- String html = IOUtils.toString(instream);
- // PS: 大家可以使用 JSoup 来解析网页饿,Very easy~
- System.out.println(response);
- System.out.println("---------------------------------");
- System.out.println(html);
- }
- }
阅读(1523) | 评论(0) | 转发(0) |