博客园java_模拟博客园登录java实现

1 importjava.io.BufferedReader;2 importjava.io.InputStreamReader;3 importjava.io.OutputStreamWriter;4 importjava.net.URL;5 importjava.security.GeneralSecurityException;6 importjava.util.List;7

8 importjavax.net.ssl.HostnameVerifier;9 importjavax.net.ssl.HttpsURLConnection;10 importjavax.net.ssl.SSLContext;11 importjavax.net.ssl.SSLSession;12 importjavax.net.ssl.X509TrustManager;13

14 importcom.dyf.test.LoginDemo;15

16

17 public classMyBlong {18

19 private staticString url;20 public staticString realCookie;21

22

23 private X509TrustManager xtm = newTrustVer();24

25 //创建一个类去信任所有的(主机(个人觉得翻译成主机))

26 private HostnameVerifier hnv = newHostnameVerifier() {27 public booleanverify(String hostname, SSLSession session) {28 return false;29 }30 };31

32 //这个方法中我们假定我们的系统是不太严格的校验和信任管理的。代码一旦执行,就会调用静态方法HttpsURLConnection();

33 publicMyBlong(String url) {34 this.url =url;35 SSLContext sslContext = null;36

37 try{38 sslContext = SSLContext.getInstance("TLS");39 X509TrustManager[] xtmArray = newX509TrustManager[] { xtm };40 sslContext.init(null, xtmArray, newjava.security.SecureRandom());41 } catch(GeneralSecurityException gse) {42 gse.printStackTrace();43 }44

45 if(sslContext != null) {46 HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());47 }48 HttpsURLConnection.setDefaultHostnameVerifier(hnv);49

50 }51

52 publicMyBlong() {53     //这个不写好像有个程序里面会报错,所以就加上了

54 }55

56 public voidgetHtml(String url,String data) {57 newLoginDemo(url);58 //主要获取信息参考http://www.jianshu.com/p/2cd5ba0bd7b3

59 try{60

61 //首先通过get方法获取返回的cookie 通过firebug看到的有两次请求 返回两个cookie的是有用的,62 //个人看了一下只是多了AspxAutoDetectCookieSupport=1,于是手动加上了

63 HttpsURLConnection urlCon = (HttpsURLConnection) (newURL(url)).openConnection();64

65 String cookie = urlCon.getHeaderField("Set-Cookie");66 cookie = "AspxAutoDetectCookieSupport=1; "+cookie.substring(0,cookie.indexOf(";"));67

68 BufferedReader br = new BufferedReader(new InputStreamReader(urlCon.getInputStream(),"utf-8"));69 String str = "";70 String verificationToken = "";71 while((str=br.readLine())!=null){72 //获取VerificationToken

73 if(str.contains("VerificationToken")){74 verificationToken = str.substring(str.indexOf(":")+3,str.lastIndexOf("'"));75 }76 }77 br.close();78 urlCon.disconnect();79

80 //查看请求头信息81 //Map> headMap = urlCon.getHeaderFields();82

83 //Set>> headSet = headMap.entrySet();84 //Iterator>> it = headSet.iterator();85 //while (it.hasNext()) {86 //System.out.println(it.next());87 //}88 //post请求吧用户名和密码加密后和之前获取的cookie还有VerificationToken以及X-Requested-With(经证明这个也是必须的)一同发送给服务器

89 HttpsURLConnection _urlCon = (HttpsURLConnection) (newURL(url)).openConnection();90

91 _urlCon.setDoInput(true);92 _urlCon.setDoOutput(true);93 _urlCon.setRequestProperty("VerificationToken", verificationToken);94 _urlCon.setRequestProperty("Cookie", cookie);95 _urlCon.setRequestProperty("X-Requested-With", "XMLHttpRequest");96 _urlCon.setRequestProperty("Content-Type", "application/json; charset=utf-8");97 OutputStreamWriter osw = newOutputStreamWriter(_urlCon.getOutputStream());98 osw.write(data);99 osw.flush();100 osw.close();101

102 //System.out.println("==================这段只是查看响应的东西==========");103 //

104 //

105 //BufferedReader _br = new BufferedReader(new InputStreamReader(_urlCon.getInputStream(),"utf-8"));106 //String line = "";107 //StringBuilder sb = new StringBuilder();108 //while ((line = _br.readLine()) != null) {109 //sb.append(line);110 //sb.append("\n");111 //}112 //_br.close();113 //

114 //System.out.println(sb.toString());

115

116 List temp_List = (_urlCon.getHeaderFields()).get("Set-Cookie");117

118 for(String s : temp_List) {119

120 //.CNBlogsCookie这个才是关键的cookie

121 if(s.contains(".CNBlogsCookie")){122 realCookie = s.substring(s.indexOf("=")+1,s.indexOf(";"));123 System.out.println(realCookie);124 }125 }126

127 //只是查看请求头信息128 //Map> headMap = _urlCon.getHeaderFields();129 //Set>> headSet = headMap.entrySet();130 //Iterator>> it = headSet.iterator();131 //while (it.hasNext()) {132 //System.out.println(it.next());133 //}

134

135 _urlCon.disconnect();136 } catch(Exception mue) {137 mue.printStackTrace();138 }139 }140

141 //just a kidding142 //public static void main(String[] args) {143 //LoginDemo lg = new LoginDemo("https://passport.cnblogs.com/user/signin");144 //String userName = KeyDemo.myencode("风清扬dyf");145 //String password = KeyDemo.myencode("ssss");//我会告诉你我的密码吗146 //String jsonTest = "{\"input1\":\"" + userName + "\",\"input2\":\"" + password + "\",\"remember\":false}";147 //lg.getHtml("https://passport.cnblogs.com/user/signin",jsonTest);148 //}

149 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值