Chinaunix首页 | 论坛 | 博客
  • 博客访问: 530155
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2011-12-30 18:46:32

。。。
。。。
。。。

点击(此处)折叠或打开

  1. import java.io.InputStream;
  2. import java.net.URI;
  3. import java.security.cert.CertificateException;
  4. import java.security.cert.X509Certificate;
  5. import java.util.ArrayList;
  6. import java.util.List;

  7. import javax.net.ssl.SSLContext;
  8. import javax.net.ssl.TrustManager;
  9. import javax.net.ssl.X509TrustManager;

  10. import org.apache.commons.io.IOUtils;
  11. import org.apache.http.HttpEntity;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.NameValuePair;
  14. import org.apache.http.client.CookieStore;
  15. import org.apache.http.client.HttpClient;
  16. import org.apache.http.client.entity.UrlEncodedFormEntity;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.client.protocol.ClientContext;
  19. import org.apache.http.client.utils.URIUtils;
  20. import org.apache.http.client.utils.URLEncodedUtils;
  21. import org.apache.http.conn.ClientConnectionManager;
  22. import org.apache.http.conn.scheme.Scheme;
  23. import org.apache.http.conn.scheme.SchemeRegistry;
  24. import org.apache.http.conn.ssl.SSLSocketFactory;
  25. import org.apache.http.impl.client.BasicCookieStore;
  26. import org.apache.http.impl.client.DefaultHttpClient;
  27. import org.apache.http.impl.conn.SingleClientConnManager;
  28. import org.apache.http.message.BasicNameValuePair;
  29. import org.apache.http.params.CoreConnectionPNames;
  30. import org.apache.http.protocol.BasicHttpContext;
  31. import org.apache.http.protocol.HttpContext;

  32. public class Main {

  33.     public static void main(String[] args) throws Exception {
  34.         TrustManager easyTrustManager = new X509TrustManager() {

  35.             public void checkClientTrusted(X509Certificate[] chain,
  36.                     String authType) throws CertificateException {
  37.                 System.out.println("checkClientTrusted()...");
  38.             }

  39.             public void checkServerTrusted(X509Certificate[] chain,
  40.                     String authType) throws CertificateException {
  41.                 System.out.println("checkServerTrusted()...");
  42.             }

  43.             public X509Certificate[] getAcceptedIssuers() {
  44.                 return null;
  45.             }
  46.         };

  47.         // ================================================================
  48.         // 配置连接上下文
  49.         CookieStore cookieStore = new BasicCookieStore();
  50.         HttpContext localContext = new BasicHttpContext();
  51.         localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

  52.         SSLContext sslcontext = SSLContext.getInstance("TLS");
  53.         sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);

  54.         SSLSocketFactory sf = new SSLSocketFactory(sslcontext,
  55.                 SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

  56.         Scheme https = new Scheme("htt ps", 443, sf);
  57.         SchemeRegistry sr = new SchemeRegistry();
  58.         sr.register(https);
  59.         localContext.setAttribute(ClientContext.SCHEME_REGISTRY, sr);

  60.         // ================================================================
  61.         // 配置 HttpClient
  62.         ClientConnectionManager cm = new SingleClientConnManager();
  63.         HttpClient httpclient = new DefaultHttpClient(cm);

  64.         // 配置超时
  65.         httpclient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
  66.                 60 * 1000);
  67.         httpclient.getParams().setIntParameter(
  68.                 CoreConnectionPNames.CONNECTION_TIMEOUT, 60 * 1000);

  69.         // ================================================================
  70.         // 准备POST请求。
  71.         // URL上的参数
  72.         List<NameValuePair> getParams = new ArrayList<NameValuePair>();
  73.         getParams.add(new BasicNameValuePair("r", Long.toString(System
  74.                 .currentTimeMillis())));

  75.         URI uri = URIUtils.createURI("https", "auth.alipay.com", -1,
  76.                 "/login/index.htm", URLEncodedUtils.format(getParams, "GBK"),
  77.                 null);

  78.         HttpPost httpPost = new HttpPost(uri);

  79.         // POST提交的参数
  80.         List<NameValuePair> postParams = new ArrayList<NameValuePair>();
  81.         postParams.add(new BasicNameValuePair("logonId", "li2@2b.cn"));
  82.         postParams.add(new BasicNameValuePair("password", "91741"));
  83.         httpPost.setEntity(new UrlEncodedFormEntity(postParams, "GBK"));

  84.         // ================================================================
  85.         // 结合以上三者,执行请求并获取结果
  86.         HttpResponse response = httpclient.execute(httpPost, localContext);

  87.         // ================================================================
  88.         // 解析结果
  89.         HttpEntity entity = response.getEntity();
  90.         InputStream instream = entity.getContent();
  91.         String html = IOUtils.toString(instream);

  92.         // PS: 大家可以使用 JSoup 来解析网页饿,Very easy~
  93.         System.out.println(response);
  94.         System.out.println("---------------------------------");
  95.         System.out.println(html);
  96.     }
  97. }


阅读(1523) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~