├── ghost-login ├── .gitignore ├── seleunim-driver │ ├── chromedriver.exe │ ├── chromedriver_win32.zip │ └── geckodriver-v0.8.0-win32.zip ├── captcha │ └── captcha-20160915-073359.png ├── src │ ├── test │ │ ├── resources │ │ │ ├── dirver-config.xml │ │ │ └── log4j.properties │ │ └── java │ │ │ └── zz │ │ │ └── pseas │ │ │ └── ghost │ │ │ └── login │ │ │ ├── DriverInitterTest.java │ │ │ ├── ClientTest.java │ │ │ └── RSATest.java │ └── main │ │ ├── resources │ │ ├── dirver-config.xml │ │ └── log4j.properties │ │ └── java │ │ └── zz │ │ └── pseas │ │ └── ghost │ │ ├── login │ │ ├── sina │ │ │ └── package-info.java │ │ ├── douban │ │ │ └── package-info.java │ │ ├── zhihu │ │ │ └── package-info.java │ │ ├── core │ │ │ └── net │ │ │ │ └── package-info.java │ │ ├── tencent │ │ │ └── package-info.java │ │ ├── GhostLogin.java │ │ ├── weibo │ │ │ ├── WeiboLoginTest.java │ │ │ ├── WeiboLogin.java │ │ │ ├── WeibocnLogin.java │ │ │ └── SinaWeiboLogin.java │ │ ├── baidu │ │ │ ├── BaiduLogInTest.java │ │ │ └── BaiduLogin.java │ │ ├── zhifubao │ │ │ ├── HtmlUnitTry.java │ │ │ └── SeleniumTest.java │ │ ├── taobao │ │ │ ├── LogInTaobaoWebDriver.java │ │ │ ├── LogInTaobao.java │ │ │ ├── RsaUtil.java │ │ │ └── MTaobaoLogin.java │ │ └── jd │ │ │ ├── SimpleJDLogin.java │ │ │ ├── JDLogIn.java │ │ │ └── JDCookie.java │ │ ├── anticaptcha │ │ ├── package-info.java │ │ └── svm │ │ │ └── package-info.java │ │ ├── client │ │ ├── CommonsPage.java │ │ ├── ClientFactory.java │ │ └── GhostClient.java │ │ ├── browser │ │ ├── DriverInitter.java │ │ └── BrowserFactory.java │ │ └── utils │ │ ├── FileUtil.java │ │ ├── CodeUtils.java │ │ ├── DownloadUtil.java │ │ ├── ImgUtil.java │ │ ├── MD5Util.java │ │ ├── LoginUtils.java │ │ ├── StringUtil.java │ │ ├── BrowserUtil.java │ │ └── HttpClinetUtil.java ├── .project ├── .classpath └── pom.xml ├── .gitignore └── README.md /ghost-login/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /ghost-login/seleunim-driver/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hubs/ghost-login/master/ghost-login/seleunim-driver/chromedriver.exe -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Package Files # 4 | *.jar 5 | *.war 6 | *.ear 7 | *.bak 8 | 9 | 10 | .settings/ 11 | target/ 12 | 13 | -------------------------------------------------------------------------------- /ghost-login/captcha/captcha-20160915-073359.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hubs/ghost-login/master/ghost-login/captcha/captcha-20160915-073359.png -------------------------------------------------------------------------------- /ghost-login/seleunim-driver/chromedriver_win32.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hubs/ghost-login/master/ghost-login/seleunim-driver/chromedriver_win32.zip -------------------------------------------------------------------------------- /ghost-login/seleunim-driver/geckodriver-v0.8.0-win32.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hubs/ghost-login/master/ghost-login/seleunim-driver/geckodriver-v0.8.0-win32.zip -------------------------------------------------------------------------------- /ghost-login/src/test/resources/dirver-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | webdriver.chrome.driver 6 | ./seleunim-driver/chromedriver.exe 7 | 8 | -------------------------------------------------------------------------------- /ghost-login/src/test/java/zz/pseas/ghost/login/DriverInitterTest.java: -------------------------------------------------------------------------------- 1 | package zz.pseas.ghost.login; 2 | 3 | import java.io.IOException; 4 | 5 | import zz.pseas.ghost.browser.DriverInitter; 6 | 7 | public class DriverInitterTest { 8 | public static void main(String[] args) throws IOException { 9 | DriverInitter.init(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /ghost-login/src/test/java/zz/pseas/ghost/login/ClientTest.java: -------------------------------------------------------------------------------- 1 | package zz.pseas.ghost.login; 2 | 3 | import zz.pseas.ghost.client.GhostClient; 4 | 5 | public class ClientTest { 6 | public static void main(String[] args) { 7 | GhostClient client = new GhostClient(); 8 | String s = client.get("http://www.baidu.com"); 9 | System.out.println(s); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /ghost-login/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | login 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /ghost-login/src/main/resources/dirver-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | webdriver.chrome.driver 7 | ./seleunim-driver/chromedriver.exe 8 | 9 | 10 | 26 | -------------------------------------------------------------------------------- /ghost-login/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger = debug,D,I 2 | 3 | ### print info to console 4 | log4j.appender.D = org.apache.log4j.ConsoleAppender 5 | log4j.appender.D.Target = System.out 6 | log4j.appender.D.Threshold = info 7 | log4j.appender.D.layout = org.apache.log4j.PatternLayout 8 | log4j.appender.D.layout.ConversionPattern = [%-5p] [%d{yy-MM-dd HH:mm:ss,sss}] %m %c %l %t%n 9 | 10 | ### print info to file 11 | log4j.appender.I = org.apache.log4j.DailyRollingFileAppender 12 | log4j.appender.I.File = logs/ghost-login.log 13 | log4j.appender.I.Append = TRUE 14 | log4j.appender.I.Threshold = INFO 15 | log4j.appender.I.layout = org.apache.log4j.PatternLayout 16 | log4j.appender.I.layout.ConversionPattern = [%-5p] [%d{yyyy-MM-dd HH:mm:ss,sss}] %m %c %l %t%n 17 | 18 | -------------------------------------------------------------------------------- /ghost-login/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger = debug,D,I 2 | 3 | ### print info to console 4 | log4j.appender.D = org.apache.log4j.ConsoleAppender 5 | log4j.appender.D.Target = System.out 6 | log4j.appender.D.Threshold = info 7 | log4j.appender.D.layout = org.apache.log4j.PatternLayout 8 | log4j.appender.D.layout.ConversionPattern = [%-5p] [%d{yy-MM-dd HH:mm:ss,sss}] %m %c %l %t%n 9 | 10 | ### print info to file 11 | log4j.appender.I = org.apache.log4j.DailyRollingFileAppender 12 | log4j.appender.I.File = logs/ghost-login.log 13 | log4j.appender.I.Append = TRUE 14 | log4j.appender.I.Threshold = INFO 15 | log4j.appender.I.layout = org.apache.log4j.PatternLayout 16 | log4j.appender.I.layout.ConversionPattern = [%-5p] [%d{yyyy-MM-dd HH:mm:ss,sss}] %m %c %l %t%n 17 | 18 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/sina/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * 18 | */ 19 | /** 20 | * @date 2016年9月14日 下午9:26:00 21 | * @version 22 | * @since JDK 1.8 23 | */ 24 | package zz.pseas.ghost.login.sina; -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/anticaptcha/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * 18 | */ 19 | /** 20 | * @date 2016年9月26日 下午11:46:58 21 | * @version 22 | * @since JDK 1.8 23 | */ 24 | package zz.pseas.ghost.anticaptcha; -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/douban/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * 18 | */ 19 | /** 20 | * @date 2016年9月14日 下午9:26:00 21 | * @version 22 | * @since JDK 1.8 23 | */ 24 | package zz.pseas.ghost.login.douban; -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/zhihu/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * 18 | */ 19 | /** 20 | * @date 2016年9月14日 下午9:26:00 21 | * @version 22 | * @since JDK 1.8 23 | */ 24 | package zz.pseas.ghost.login.zhihu; -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/core/net/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * 18 | */ 19 | /** 20 | * @date 2016年9月14日 下午9:26:00 21 | * @version 22 | * @since JDK 1.8 23 | */ 24 | package zz.pseas.ghost.login.core.net; -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/tencent/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * 18 | */ 19 | /** 20 | * @date 2016年9月14日 下午9:26:00 21 | * @version 22 | * @since JDK 1.8 23 | */ 24 | package zz.pseas.ghost.login.tencent; -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/anticaptcha/svm/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * 18 | */ 19 | /** 20 | * @date 2016年9月26日 下午11:52:30 21 | * @version 22 | * @since JDK 1.8 23 | */ 24 | package zz.pseas.ghost.anticaptcha.svm; -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/GhostLogin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login; 20 | 21 | /** 22 | * Hello world! 23 | * 24 | */ 25 | public class GhostLogin 26 | { 27 | public static void main( String[] args ) 28 | { 29 | System.out.println( "Hello World!" ); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/weibo/WeiboLoginTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * 18 | */ 19 | package zz.pseas.ghost.login.weibo; 20 | 21 | /** 22 | * @date 2016年9月15日 下午7:30:57 23 | * @version 24 | * @since JDK 1.8 25 | */ 26 | public class WeiboLoginTest { 27 | 28 | /** 29 | * @param args 30 | * @since JDK 1.8 31 | */ 32 | public static void main(String[] args) { 33 | // TODO Auto-generated method stub 34 | 35 | try { 36 | String cookie=SinaWeiboLogin.getSinaCookie("test", "test"); 37 | System.out.println(cookie); 38 | } catch (Exception e) { 39 | // TODO Auto-generated catch block 40 | e.printStackTrace(); 41 | } 42 | 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /ghost-login/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/baidu/BaiduLogInTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.baidu; 20 | 21 | import java.io.IOException; 22 | 23 | import javax.swing.JOptionPane; 24 | /** 25 | * @date 2016年9月14日 下午9:26:00 26 | * @version 27 | * @since JDK 1.8 28 | */ 29 | public class BaiduLogInTest { 30 | 31 | public static void main(String[] args) { 32 | String username = JOptionPane.showInputDialog("请输入用户名"); 33 | String password = JOptionPane.showInputDialog("请输入密码"); 34 | try { 35 | String bdCookies = BaiduLogin.getBaiduCookie(username, password); 36 | System.out.println("baidu-cookies:"); 37 | System.out.println(bdCookies); 38 | } catch (IOException e) { 39 | // TODO Auto-generated catch block 40 | e.printStackTrace(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/zhifubao/HtmlUnitTry.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.zhifubao; 20 | 21 | import java.io.IOException; 22 | import java.net.MalformedURLException; 23 | 24 | import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; 25 | import com.gargoylesoftware.htmlunit.Page; 26 | import com.gargoylesoftware.htmlunit.WebClient; 27 | /** 28 | * @date 2016年9月14日 下午9:26:00 29 | * @version 30 | * @since JDK 1.8 31 | */ 32 | public class HtmlUnitTry { 33 | public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException { 34 | WebClient client = new WebClient(); 35 | client.getOptions().setJavaScriptEnabled(false); 36 | client.getOptions().setCssEnabled(false); 37 | Page page = client.getPage("https://auth.alipay.com/login/index.htm"); 38 | System.out.println(page.getWebResponse().getContentAsString()); 39 | client.close(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/client/CommonsPage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.client; 20 | 21 | import java.util.Map; 22 | import java.util.TreeMap; 23 | 24 | /** 25 | * @date 2016年9月14日 下午9:26:00 26 | * @version 27 | * @since JDK 1.8 28 | */ 29 | public class CommonsPage { 30 | private String errMsg = "OK"; 31 | private Map headers = new TreeMap(); 32 | private byte[] contents; 33 | 34 | public Map getHeaders() { 35 | return headers; 36 | } 37 | 38 | public void setHeaders(Map headers) { 39 | this.headers = headers; 40 | } 41 | 42 | public byte[] getContents() { 43 | return contents; 44 | } 45 | 46 | public void setContents(byte[] contents) { 47 | this.contents = contents; 48 | } 49 | 50 | public String getHeader(String k) { 51 | String ans = headers.get(k); 52 | return ans; 53 | } 54 | 55 | public String getErrMsg() { 56 | return errMsg; 57 | } 58 | 59 | public void setErrMsg(String errMsg) { 60 | this.errMsg = errMsg; 61 | } 62 | 63 | public void addHeader(String k, String v) { 64 | headers.put(k, v); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/browser/DriverInitter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.browser; 20 | 21 | import java.io.FileInputStream; 22 | import java.io.IOException; 23 | import java.io.InputStream; 24 | 25 | import org.apache.commons.io.IOUtils; 26 | import org.jsoup.Jsoup; 27 | import org.jsoup.nodes.Element; 28 | import org.jsoup.select.Elements; 29 | 30 | import zz.pseas.ghost.utils.StringUtil; 31 | 32 | /** 33 | * @date 2016年9月14日 下午9:26:00 34 | * @version 35 | * @since JDK 1.8 36 | */ 37 | public class DriverInitter { 38 | public static void init() throws IOException { 39 | InputStream in = new FileInputStream("dirver-config.xml"); 40 | byte[] bytes = IOUtils.toByteArray(in); 41 | String xml = new String(bytes, "utf-8"); 42 | Elements drivers = Jsoup.parse(xml).select("driver"); 43 | in.close(); 44 | 45 | for (Element driver : drivers) { 46 | String k = driver.getElementsByTag("name").first().ownText(); 47 | String v = driver.getElementsByTag("value").first().ownText(); 48 | if (StringUtil.isNotEmpty(k) && StringUtil.isNotEmpty(v)) { 49 | System.setProperty(k, v); 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/utils/FileUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.utils; 20 | 21 | import java.io.BufferedReader; 22 | import java.io.File; 23 | import java.io.FileInputStream; 24 | import java.io.FileNotFoundException; 25 | import java.io.FileOutputStream; 26 | import java.io.IOException; 27 | import java.io.InputStreamReader; 28 | /** 29 | * @date 2016年9月14日 下午9:26:00 30 | * @version 31 | * @since JDK 1.8 32 | */ 33 | public class FileUtil { 34 | public static void writeFile(byte[] bytes, String path) { 35 | int n = path.lastIndexOf("/"); 36 | if (n > 0) { 37 | String dir = path.substring(0, n); 38 | File f = new File(dir); 39 | f.mkdir(); 40 | } 41 | 42 | try { 43 | FileOutputStream out = new FileOutputStream(new File(path)); 44 | out.write(bytes); 45 | out.flush(); 46 | out.close(); 47 | } catch (FileNotFoundException e) { 48 | } catch (IOException e) { 49 | } 50 | } 51 | 52 | public static String readFile(String file, String charset) throws Exception { 53 | FileInputStream in = new FileInputStream(file); 54 | InputStreamReader r1 = new InputStreamReader(in, charset); 55 | BufferedReader r2 = new BufferedReader(r1); 56 | StringBuffer sb = new StringBuffer(); 57 | while (true) { 58 | String s = r2.readLine(); 59 | if (s == null) 60 | break; 61 | sb.append(s); 62 | } 63 | r2.close(); 64 | return sb.toString(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/zhifubao/SeleniumTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.zhifubao; 20 | 21 | import org.apache.http.client.CookieStore; 22 | import org.openqa.selenium.By; 23 | import org.openqa.selenium.WebDriver; 24 | 25 | import zz.pseas.ghost.browser.BrowserFactory; 26 | import zz.pseas.ghost.client.GhostClient; 27 | /** 28 | * @date 2016年9月14日 下午9:26:00 29 | * @version 30 | * @since JDK 1.8 31 | */ 32 | public class SeleniumTest { 33 | public static void main(String[] args) throws Exception { 34 | 35 | String zfbuserNmae="ZFBname"; 36 | String zfbpassWord="ZFBpasssword"; 37 | 38 | WebDriver driver = BrowserFactory.getPhantomJS(); 39 | 40 | driver.get("https://auth.alipay.com/login/index.htm"); 41 | 42 | driver.findElement(By.id("J-input-user")).clear(); 43 | driver.findElement(By.id("J-input-user")).sendKeys(zfbuserNmae); 44 | driver.findElement(By.id("password_rsainput")).clear(); 45 | driver.findElement(By.id("password_rsainput")).sendKeys(zfbpassWord); 46 | driver.findElement(By.id("J-login-btn")) 47 | .click(); 48 | 49 | CookieStore store = BrowserFactory.getCookieStore(driver); 50 | 51 | GhostClient client = new GhostClient("utf-8", store); 52 | String url = "https://my.alipay.com/portal/i.htm?referer=https%3A%2F%2Fauth.alipay.com%2Flogin%2Findex.htm"; 53 | String ans = client.get(url); 54 | System.out.println(ans); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/utils/CodeUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.utils; 20 | 21 | import java.util.regex.Matcher; 22 | import java.util.regex.Pattern; 23 | /** 24 | * @date 2016年9月14日 下午9:26:00 25 | * @version 26 | * @since JDK 1.8 27 | */ 28 | public class CodeUtils { 29 | 30 | public static String getcode(String string,String name){ 31 | String code = null; 32 | Pattern p = Pattern.compile(name+"=\"([\\s\\S]*?)\""); 33 | Matcher m = p.matcher(string); 34 | if(m.find()){ 35 | code = m.group(1); 36 | } 37 | return code; 38 | } 39 | 40 | public static String getcodeWithoutQua(String string,String name){ 41 | String code = null; 42 | Pattern p = Pattern.compile(name+"=([\\s\\S]*?);"); 43 | Matcher m = p.matcher(string); 44 | if(m.find()){ 45 | code = m.group(1); 46 | } 47 | return code; 48 | } 49 | 50 | public static String getcodeWithAnd(String string,String name){ 51 | String code = null; 52 | Pattern p = Pattern.compile(name+"=([\\s\\S]*?)&"); 53 | Matcher m = p.matcher(string); 54 | if(m.find()){ 55 | code = m.group(1); 56 | } 57 | return code; 58 | } 59 | 60 | public static String getcodeWithSingleQua(String string,String name){ 61 | String code = null; 62 | Pattern p = Pattern.compile(name+"='([\\s\\S]*?)'"); 63 | Matcher m = p.matcher(string); 64 | if(m.find()){ 65 | code = m.group(1); 66 | } 67 | return code; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/taobao/LogInTaobaoWebDriver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.taobao; 20 | 21 | import org.openqa.selenium.By; 22 | import org.openqa.selenium.WebDriver; 23 | import org.openqa.selenium.WebElement; 24 | 25 | import zz.pseas.ghost.browser.BrowserFactory; 26 | /** 27 | * @date 2016年9月14日 下午9:26:00 28 | * @version 29 | * @since JDK 1.8 30 | */ 31 | public class LogInTaobaoWebDriver { 32 | public static void main(String[] args) throws InterruptedException { 33 | WebDriver ie = BrowserFactory.getIE(); 34 | ie.get("https://login.taobao.com/member/login.jhtml"); 35 | 36 | Thread.sleep(5000); 37 | 38 | String tbuserNmae="TBname"; 39 | String tbpassWord="TBpasssword"; 40 | 41 | WebElement btn = ie.findElement(By.id("J_Quick2Static")); 42 | if(btn!=null && btn.isDisplayed()){ 43 | btn.click(); 44 | } 45 | 46 | ie.findElement(By.id("TPL_username_1")).clear(); 47 | ie.findElement(By.id("TPL_username_1")).sendKeys(tbuserNmae); 48 | 49 | ie.findElement(By.id("TPL_password_1")).clear(); 50 | ie.findElement(By.id("TPL_password_1")).sendKeys(tbpassWord); 51 | 52 | ie.findElement(By.id("J_SubmitStatic")).click(); 53 | 54 | Thread.sleep(10000L); 55 | 56 | String html = ie.getPageSource(); 57 | 58 | System.out.println(html); 59 | String url = ie.getCurrentUrl(); 60 | System.out.println(url); 61 | /*WebElement phoneCheck = ie.findElement(By.id("J_GetCode")); 62 | if(phoneCheck!=null && phoneCheck.isDisplayed()){ 63 | WebElement btn2 = ie.findElement(By.id("J_GetCode")); 64 | btn2.click(); 65 | }*/ 66 | 67 | //Thread.sleep(5000); 68 | 69 | 70 | 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/weibo/WeiboLogin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.weibo; 20 | 21 | import java.util.Date; 22 | import java.util.Set; 23 | 24 | import org.apache.http.impl.client.BasicCookieStore; 25 | import org.apache.http.impl.cookie.BasicClientCookie; 26 | import org.openqa.selenium.Cookie; 27 | import org.openqa.selenium.WebDriver; 28 | 29 | import zz.pseas.ghost.browser.BrowserFactory; 30 | import zz.pseas.ghost.client.GhostClient; 31 | import zz.pseas.ghost.utils.StringUtil; 32 | /** 33 | * @date 2016年9月14日 下午9:26:00 34 | * @version 35 | * @since JDK 1.8 36 | */ 37 | public class WeiboLogin { 38 | public static void main(String[] args) { 39 | WebDriver driver = BrowserFactory.getIE(); 40 | 41 | driver.get("http://weibo.com/"); 42 | 43 | Set cookies = driver.manage().getCookies(); 44 | BasicCookieStore cookieStore = new BasicCookieStore(); 45 | for (Cookie c : cookies) { 46 | BasicClientCookie c1 = new BasicClientCookie(c.getName(), c.getValue()); 47 | c1.setDomain(c.getDomain() == null ? "weibo" : c.getDomain()); 48 | c1.setPath(c.getPath()); 49 | Date d = c.getExpiry(); 50 | if (d != null) { 51 | c1.setExpiryDate(d); 52 | } 53 | cookieStore.addCookie(c1); 54 | } 55 | driver.quit(); 56 | 57 | GhostClient client = new GhostClient("utf-8", cookieStore); 58 | String url = "http://weibo.com/p/10080813dc27e2acb1441006674c8aa2ef07d4/followlist?page=1#Pl_Core_F4RightUserList__38"; 59 | //HttpGet get = new HttpGet(url); 60 | String s = client.get(url); 61 | System.out.println(s); 62 | String next = StringUtil.regex(s, "(?<=replace\\(\").*?(?=\")"); 63 | String html = client.get(next); 64 | System.out.println(html); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/jd/SimpleJDLogin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.jd; 20 | 21 | import java.awt.image.BufferedImage; 22 | import java.io.File; 23 | import java.io.IOException; 24 | 25 | import javax.imageio.ImageIO; 26 | 27 | import org.openqa.selenium.By; 28 | import org.openqa.selenium.WebDriver; 29 | import org.openqa.selenium.WebElement; 30 | 31 | import zz.pseas.ghost.utils.BrowserUtil; 32 | import zz.pseas.ghost.utils.ImgUtil; 33 | /** 34 | * @date 2016年9月14日 下午9:26:00 35 | * @version 36 | * @since JDK 1.8 37 | */ 38 | public class SimpleJDLogin { 39 | public static void main(String[] args) throws IOException { 40 | 41 | String jduserNmae="youname"; 42 | String jdpassWord="youpasssword"; 43 | 44 | JDCookie cookieParamSupplier = new JDCookie(); 45 | cookieParamSupplier.init(); 46 | 47 | WebDriver browser = cookieParamSupplier.getBrowser(); 48 | 49 | browser.findElement(By.id("loginname")).clear(); 50 | browser.findElement(By.id("loginname")) 51 | .sendKeys(jduserNmae); 52 | 53 | browser.findElement(By.id("nloginpwd")).clear(); 54 | browser.findElement(By.id("nloginpwd")) 55 | .sendKeys(jdpassWord); 56 | 57 | 58 | WebElement authCode = browser.findElement(By.id("authcode")); 59 | if(authCode.isDisplayed()){ 60 | byte[] bytes = BrowserUtil.captureScreenShotById(browser, "JD_Verification1"); 61 | 62 | BufferedImage im = ImgUtil.bytesToBImage(bytes); 63 | ImageIO.write(im, "jpg", new File("d:/jd.jpg")); 64 | 65 | browser.findElement(By.id("authcode")).clear(); 66 | browser.findElement(By.id("authcode")) 67 | .sendKeys("YYEU"); 68 | } 69 | 70 | browser.findElement(By.id("loginsubmit")) 71 | .click(); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #Ghost-login 2 | 3 | #使用帮助 4 | [EliteQing](http://www.cnblogs.com/liinux) 5 | [liinnux](https://liinnux.github.io/ghost-login/) 6 | ## 模拟登录一些常见的网站 7 | 8 | 9 | 主要基于以下的 Java的第三 library 10 | 1. [httpclient](http://hc.apache.org/downloads.cgi) HTTP请求以及响应 11 | 2. [Selenium](http://docs.seleniumhq.org/download/) 模拟自动登录 12 | 3. [tesseract-ocr](https://github.com/tesseract-ocr) 验证码识别 13 | 4. [bouncycastle](http://www.bouncycastle.org/) 加密解密 14 | 15 | ## Done 16 | 1. [百度](https://www.baidu.com)(已经实现) 17 | 2. [豆瓣](https://accounts.douban.com/login?redir=https://m.douban.com/)(待实现) 18 | 3. [京东](http://www.jd.com/)(已经实现) 19 | 4. [淘宝](https://www.taobao.com/)(还有点问题) 20 | 5. [支付宝](https://www.alipay.com/)(还有点问题) 21 | 6. [新浪微博](https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F%3Fjumpfrom%3Dwapv4%26tip%3D1) (已经实现) 22 | 5. [新浪邮箱](http://mail.sina.com.cn/)(待实现) 23 | 5. [QQ邮箱](https://mail.qq.com/cgi-bin/loginpage)(待实现) 24 | 7. [QQ微博](http://w.t.qq.com/touch) (已经实现) 25 | 8. [知乎](https://www.zhihu.com/#signin)(还有点问题) 26 | 27 | 28 | ##Todolist 29 | 0. **重构代码,增加可扩展性** 30 | 1. 增加新浪微博网页版的登录 (已解决) 31 | 2. 增加QQ空间和QQ邮箱的登录 32 | 3. 重新组织文件结构和代码风格,make it esay to read; 33 | 4. 增加可扩展性,方便添加新的功能, 现在开发新功能的例子还很不优雅。 34 | 35 | ## tips of pull request 36 | 37 | 欢迎大家一起来 pull request 38 | 39 | 0. pull request 尽量做到JDK1.8版本的兼容。 40 | 1. 增加新的网站登录 41 | 2. 改进错误, JDK1.8版本的兼容 42 | 3. 基于模拟登录增加新的功能。 43 | 44 | ## something to add 45 | 46 | 0. 这个项目开始于 2016.9.11,有些网站改了规则,可能模拟登录不能使用了,授人以鱼不如授人以渔,后面会维护几个典型的模拟登录,并且会给出每个模拟登录的教程,这样对于刚刚接触爬虫,对于抓包分析技术一脸懵逼的初学者来说比较友好,后面可能会更新图文的教程。 47 | 1. 项目写了一段时间后,发现代码的风格和程序的易用性,可扩展性,代码的可读性,都存在一定的问题,所以接下来最重要的是重构代码,让大家可以更容易的做出一些自己的小功能。 48 | 2. 如果你觉得某个网站的登录很有代表性,欢迎在 issue 中提出, 49 | 如果网站的登录很有意思,我会在后面的更新中加入 50 | 3. 网站的登录机制有可能经常的变动,所以当现在的模拟的登录的规则不能使用的时候,请在 issue 中提出 51 | 如果时间允许的话,我会更新。 52 | 53 | ## 除责申明 54 | 55 | 1. 本开源项目仅为技术交流,严禁用于其他任何违法犯罪行为; 56 | 2. 若本项目侵犯相关网站权益,请用QQ群联系,我会及时更正解决; 57 | 3. 若第三者用此项目侵犯相关网站权益,一切责任自负; 58 | 59 | ## 交流讨论 60 | 61 | 1. 开源Java爬虫QQ交流群:322937592 62 | 2. 欢迎讨论交流数据分析&网络爬虫 63 | ![alt text][id] 64 | 65 | [id]: https://git.oschina.net/liinux/ghost-login/attach_files/download?i=68688&u=http%3A%2F%2Ffiles.git.oschina.net%2Fgroup1%2FM00%2F00%2F90%2FPaAvDFgd3LGAOr-HAAAhOWgubTM458.png%3Ftoken%3Dea0a1349cf91f60aea8042a3d876a64c%26ts%3D1478352018%26attname%3Dqq1-group-322937592.png "Title" -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/jd/JDLogIn.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.jd; 20 | 21 | import java.util.Map; 22 | 23 | import org.apache.http.client.CookieStore; 24 | import org.apache.http.cookie.Cookie; 25 | 26 | import net.sf.json.JSONObject; 27 | import zz.pseas.ghost.client.GhostClient; 28 | 29 | /** 30 | * @date 2016年9月14日 下午9:26:00 31 | * @version 32 | * @since JDK 1.8 33 | */ 34 | public class JDLogIn { 35 | public static void main(String[] args) { 36 | 37 | String jduserNmae="JDname"; 38 | String jdpassWord="JDpasssword"; 39 | 40 | 41 | JDCookie cookieParamSupplier = new JDCookie(); 42 | cookieParamSupplier.init(); 43 | CookieStore cookies = cookieParamSupplier.supplyCookies(); 44 | for (Cookie c : cookies.getCookies()) { 45 | System.out.println(c.getName() + " -> " + c.getValue()); 46 | } 47 | Map map = cookieParamSupplier.supplyParams(); 48 | 49 | // 关闭浏览器 50 | cookieParamSupplier.close(); 51 | 52 | map.put("loginname", jduserNmae); 53 | map.put("loginpwd", jdpassWord); 54 | map.put("nloginpwd", jdpassWord); 55 | 56 | GhostClient client = new GhostClient("GBK", cookies); 57 | 58 | String url = "https://passport.jd.com/uc/loginService?"; 59 | url = url + "&uuid=" + map.get("uuid") + "&&r=" + Math.random() + "version=2015"; 60 | 61 | String ans = client.post(url, map); 62 | 63 | JSONObject jso = JSONObject.fromObject(ans.substring(1, ans.length() - 1)); 64 | System.out.println(jso.get("username")); 65 | 66 | System.out.println("=================="); 67 | System.out.println(url); 68 | for (Map.Entry entry : map.entrySet()) { 69 | System.out.println(entry.getKey() + " -> " + entry.getValue()); 70 | } 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/utils/DownloadUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.utils; 20 | 21 | import java.util.Set; 22 | 23 | import org.apache.http.client.CookieStore; 24 | import org.apache.http.client.methods.HttpUriRequest; 25 | import org.apache.http.impl.client.BasicCookieStore; 26 | import org.apache.http.impl.cookie.BasicClientCookie; 27 | import org.openqa.selenium.Cookie; 28 | /** 29 | * @date 2016年9月14日 下午9:26:00 30 | * @version 31 | * @since JDK 1.8 32 | */ 33 | public class DownloadUtil { 34 | public static void setUserAgent(HttpUriRequest request) { 35 | request.addHeader("User-Agent", 36 | "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"); 37 | 38 | //request.addHeader("Host","gd.189.cn"); 39 | //request.addHeader("Content-Type","text/plain"); 40 | //request.addHeader("Referer","http://gd.189.cn/common/login.jsp?UATicket=-1&loginOldUri=null"); 41 | //request.addHeader("host","my.alipay.com"); 42 | //request.addHeader("User-Agent","Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"); 43 | //request.addHeader("Host","api.m.taobao.com"); 44 | } 45 | 46 | public static CookieStore convertToCookieStore(Set cookies) { 47 | BasicCookieStore store = new BasicCookieStore(); 48 | for (Cookie c : cookies) { 49 | BasicClientCookie c1 = new BasicClientCookie(c.getName(), c.getValue()); 50 | c1.setDomain(c.getDomain()); 51 | c1.setPath(c.getPath()); 52 | c1.setExpiryDate(c.getExpiry()); 53 | store.addCookie(c1); 54 | } 55 | return store; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/utils/ImgUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.utils; 20 | 21 | import java.awt.Graphics; 22 | import java.awt.GraphicsConfiguration; 23 | import java.awt.GraphicsDevice; 24 | import java.awt.GraphicsEnvironment; 25 | import java.awt.HeadlessException; 26 | import java.awt.Image; 27 | import java.awt.Toolkit; 28 | import java.awt.Transparency; 29 | import java.awt.image.BufferedImage; 30 | 31 | import javax.swing.ImageIcon; 32 | /** 33 | * @date 2016年9月14日 下午9:26:00 34 | * @version 35 | * @since JDK 1.8 36 | */ 37 | public class ImgUtil { 38 | public static BufferedImage bytesToBImage(byte[] bytes){ 39 | Image image = Toolkit.getDefaultToolkit().createImage(bytes); 40 | if (image instanceof BufferedImage) { 41 | return (BufferedImage) image; 42 | } 43 | // This code ensures that all the pixels in the image are loaded 44 | image = new ImageIcon(image).getImage(); 45 | BufferedImage bimage = null; 46 | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 47 | try { 48 | int transparency = Transparency.OPAQUE; 49 | GraphicsDevice gs = ge.getDefaultScreenDevice(); 50 | GraphicsConfiguration gc = gs.getDefaultConfiguration(); 51 | bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); 52 | } catch (HeadlessException e) { 53 | // The system does not have a screen 54 | } 55 | if (bimage == null) { 56 | // Create a buffered image using the default color model 57 | int type = BufferedImage.TYPE_INT_RGB; 58 | bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); 59 | } 60 | // Copy image to buffered image 61 | Graphics g = bimage.createGraphics(); 62 | // Paint the image onto the buffered image 63 | g.drawImage(image, 0, 0, null); 64 | g.dispose(); 65 | return bimage; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/utils/MD5Util.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.utils; 20 | /** 21 | * @date 2016年9月14日 下午9:26:00 22 | * @version 23 | * @since JDK 1.8 24 | */ 25 | import java.security.MessageDigest; 26 | 27 | /** 28 | * 采用MD5加密解密 29 | * 30 | * @author tfq 31 | * @datetime 2011-10-13 32 | */ 33 | public class MD5Util { 34 | 35 | /*** 36 | * MD5加码 生成32位md5码 37 | */ 38 | public static String string2MD5(String inStr) { 39 | MessageDigest md5 = null; 40 | try { 41 | md5 = MessageDigest.getInstance("MD5"); 42 | } catch (Exception e) { 43 | e.printStackTrace(); 44 | return null; 45 | } 46 | char[] charArray = inStr.toCharArray(); 47 | byte[] byteArray = new byte[charArray.length]; 48 | 49 | for (int i = 0; i < charArray.length; i++) 50 | byteArray[i] = (byte) charArray[i]; 51 | byte[] md5Bytes = md5.digest(byteArray); 52 | StringBuffer hexValue = new StringBuffer(); 53 | for (int i = 0; i < md5Bytes.length; i++) { 54 | int val = ((int) md5Bytes[i]) & 0xff; 55 | if (val < 16) 56 | hexValue.append("0"); 57 | hexValue.append(Integer.toHexString(val)); 58 | } 59 | return hexValue.toString(); 60 | } 61 | 62 | /** 63 | * 加密解密算法 执行一次加密,两次解密 64 | */ 65 | public static String xorAchar(String inStr) { 66 | 67 | char[] a = inStr.toCharArray(); 68 | for (int i = 0; i < 8; i++) { 69 | a[i] = (char) (a[i] ^ 't'); 70 | } 71 | String s = new String(a); 72 | return s; 73 | 74 | } 75 | 76 | // 测试主函数 77 | public static void main(String args[]) { 78 | 79 | String tk = "57c0d3cb4fe5761a6e9cb093487ffce2"; 80 | String t = "1468373720305"; 81 | String appKey = "12574478"; 82 | String data = "{\"spm\":\"a2141.7756461.2.6\",\"page\":1,\"tabCode\":\"all\",\"appVersion\":\"1.0\",\"appName\":\"tborder\"}"; 83 | 84 | String s = tk + "&" + t + "&" + appKey + "&" + data; 85 | String a = MD5Util.string2MD5(s); 86 | System.out.println(a); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/client/ClientFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.client; 20 | 21 | import java.security.cert.CertificateException; 22 | import java.security.cert.X509Certificate; 23 | 24 | import javax.net.ssl.SSLContext; 25 | 26 | import org.apache.http.client.config.RequestConfig; 27 | import org.apache.http.config.Registry; 28 | import org.apache.http.config.RegistryBuilder; 29 | import org.apache.http.conn.socket.ConnectionSocketFactory; 30 | import org.apache.http.conn.socket.PlainConnectionSocketFactory; 31 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; 32 | import org.apache.http.conn.ssl.SSLContexts; 33 | import org.apache.http.conn.ssl.TrustStrategy; 34 | import org.apache.http.impl.client.CloseableHttpClient; 35 | import org.apache.http.impl.client.HttpClients; 36 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 37 | /** 38 | * @date 2016年9月14日 下午9:26:00 39 | * @version 40 | * @since JDK 1.8 41 | */ 42 | @SuppressWarnings("deprecation") 43 | public class ClientFactory { 44 | public static CloseableHttpClient getNewClient() { 45 | try { 46 | SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { 47 | public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { 48 | return true; 49 | } 50 | }).build(); 51 | 52 | SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); 53 | Registry reg = RegistryBuilder. create() 54 | .register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf).build(); 55 | PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(reg); 56 | 57 | RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10 * 60 * 1000) 58 | .setConnectionRequestTimeout(10 * 60 * 1000).setConnectionRequestTimeout(10 * 60 * 1000).build(); 59 | 60 | CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig) 61 | .setConnectionManager(connMgr).build(); 62 | return client; 63 | } catch (Exception e) { 64 | return null; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /ghost-login/src/test/java/zz/pseas/ghost/login/RSATest.java: -------------------------------------------------------------------------------- 1 | package zz.pseas.ghost.login; 2 | 3 | import java.math.BigInteger; 4 | import java.security.KeyFactory; 5 | import java.security.interfaces.RSAPublicKey; 6 | import java.security.spec.RSAPublicKeySpec; 7 | 8 | import javax.crypto.Cipher; 9 | 10 | import org.apache.http.client.HttpClient; 11 | import org.apache.http.impl.client.CloseableHttpClient; 12 | 13 | public class RSATest { 14 | public static void main(String[] args) throws Exception { 15 | BigInteger pbk = new BigInteger( 16 | "9a39c3fefeadf3d194850ef3a1d707dfa7bec0609a60bfcc7fe4ce2c615908b9599c8911e800aff684f804413324dc6d9f982f437e95ad60327d221a00a2575324263477e4f6a15e3b56a315e0434266e092b2dd5a496d109cb15875256c73a2f0237c5332de28388693c643c8764f137e28e8220437f05b7659f58c4df94685", 17 | 16); 18 | BigInteger exponent = new BigInteger("10001"); 19 | 20 | KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 21 | RSAPublicKeySpec keySpec = new RSAPublicKeySpec(pbk, exponent); 22 | 23 | RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec); 24 | System.out.println(publicKey); 25 | 26 | String ans = encryptByPublicKey("123456",publicKey); 27 | 28 | System.out.println(ans.length()); 29 | 30 | System.out.println(ans); 31 | String a = "5ac48b840c5112f3d6da1d0a30880" 32 | + "72410bd1ad1f3a517b70e4ac41d143208ae4941e" 33 | + "5da0b7f92209ac48b6d7c47dfad43783ecbda" 34 | + "5adea357e590dc9d31a6a2f835252c19e80d63" 35 | + "6333b8d1d9ea7e59a773467b39161faf822fdf" 36 | + "c2e14439ae065cfd30377e532cc5d3ae0fb617" 37 | + "cfa3e12e75daeb6624dd5a335d1bb1ca6626"; 38 | 39 | System.out.println(a.length()); 40 | } 41 | 42 | @SuppressWarnings("unused") 43 | public static String encryptByPublicKey(String data, RSAPublicKey publicKey) throws Exception { 44 | Cipher cipher = Cipher.getInstance("RSA"); 45 | 46 | cipher.init(Cipher.ENCRYPT_MODE, publicKey); 47 | // 模长 48 | int key_len = publicKey.getModulus().bitLength() / 8; 49 | // 加密数据长度 <= 模长-11 50 | String[] datas = splitString(data, key_len - 11); 51 | String mi = ""; 52 | // 如果明文长度大于模长-11则要分组加密 53 | for (String s : datas) { 54 | mi += bcd2Str(cipher.doFinal(s.getBytes())); 55 | } 56 | 57 | HttpClient client = null; 58 | 59 | CloseableHttpClient client1; 60 | 61 | 62 | 63 | 64 | return mi; 65 | } 66 | 67 | public static String bcd2Str(byte[] bytes) { 68 | char temp[] = new char[bytes.length * 2], val; 69 | 70 | for (int i = 0; i < bytes.length; i++) { 71 | val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f); 72 | temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); 73 | 74 | val = (char) (bytes[i] & 0x0f); 75 | temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); 76 | } 77 | return new String(temp); 78 | } 79 | 80 | public static String[] splitString(String string, int len) { 81 | int x = string.length() / len; 82 | int y = string.length() % len; 83 | int z = 0; 84 | if (y != 0) { 85 | z = 1; 86 | } 87 | String[] strings = new String[x + z]; 88 | String str = ""; 89 | for (int i = 0; i < x + z; i++) { 90 | if (i == x + z - 1 && y != 0) { 91 | str = string.substring(i * len, i * len + y); 92 | } else { 93 | str = string.substring(i * len, i * len + len); 94 | } 95 | strings[i] = str; 96 | } 97 | return strings; 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/taobao/LogInTaobao.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.taobao; 20 | 21 | import java.util.HashMap; 22 | import java.util.Set; 23 | 24 | import org.apache.http.client.CookieStore; 25 | import org.jsoup.Jsoup; 26 | import org.openqa.selenium.Cookie; 27 | import org.openqa.selenium.WebDriver; 28 | 29 | import zz.pseas.ghost.browser.BrowserFactory; 30 | import zz.pseas.ghost.client.GhostClient; 31 | import zz.pseas.ghost.utils.DownloadUtil; 32 | /** 33 | * @date 2016年9月14日 下午9:26:00 34 | * @version 35 | * @since JDK 1.8 36 | */ 37 | public class LogInTaobao { 38 | @SuppressWarnings("unused") 39 | public static void main(String[] args) throws Exception { 40 | 41 | String tbuserNmae="TBname"; 42 | String tbpassWord="TBpasssword"; 43 | 44 | String url = "https://login.taobao.com/member/login.jhtml"; 45 | WebDriver ie = BrowserFactory.getIE(); 46 | ie.get(url); 47 | 48 | Thread.sleep(5000L); 49 | 50 | Set cookies = ie.manage().getCookies(); 51 | CookieStore store = DownloadUtil.convertToCookieStore(cookies); 52 | 53 | GhostClient client = new GhostClient("utf-8", store); 54 | 55 | String html = ie.getPageSource(); 56 | String pbk = Jsoup.parse(html).select("input#J_PBK").attr("value"); 57 | String pwd1 = RsaUtil.enCode(pbk, "10001", tbuserNmae); 58 | 59 | HashMap map = new HashMap(); 60 | map.put("TPL_username", tbuserNmae); 61 | map.put("TPL_password", tbpassWord); 62 | map.put("fc", "default"); 63 | map.put("from", "tb"); 64 | map.put("gvfdcname", "10"); 65 | map.put("keyLogin", "false"); 66 | map.put("loginASR", "1"); 67 | map.put("loginASRSuc", "1"); 68 | map.put("loginType", "3"); 69 | map.put("loginsite", "0"); 70 | map.put("naviVer", "firefox|47"); 71 | 72 | String ncoToken = Jsoup.parse(html).select("input#J_NcoToken").attr("value"); 73 | map.put("ncoToken", ncoToken); 74 | 75 | map.put("newMini", "false"); 76 | map.put("newMini2", "false"); 77 | map.put("newlogin", "0"); 78 | map.put("osVer", "windows|6.1"); 79 | map.put("oslanguage", "zh-cn"); 80 | map.put("qrLogin", "true"); 81 | map.put("slideCodeShow", "false"); 82 | map.put("sr", "1920*1080"); 83 | 84 | String ua = Jsoup.parse(html).select("input#UA_InputId").attr("value"); 85 | //map.put("ua", ua); 86 | 87 | String umToken = Jsoup.parse(html).select("input[name=um_token]") 88 | .attr("value"); 89 | map.put("um_token", umToken); // TODO get um_token 90 | 91 | String ans = client.post("https://login.taobao.com/member/login.jhtml", map); 92 | System.out.println(ans); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/browser/BrowserFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.browser; 20 | 21 | import java.io.IOException; 22 | import java.util.Set; 23 | 24 | import org.apache.http.client.CookieStore; 25 | import org.apache.http.impl.client.BasicCookieStore; 26 | import org.apache.http.impl.cookie.BasicClientCookie; 27 | import org.openqa.selenium.Cookie; 28 | import org.openqa.selenium.WebDriver; 29 | import org.openqa.selenium.chrome.ChromeDriver; 30 | import org.openqa.selenium.firefox.FirefoxDriver; 31 | import org.openqa.selenium.ie.InternetExplorerDriver; 32 | import org.openqa.selenium.phantomjs.PhantomJSDriver; 33 | /** 34 | * @date 2016年9月14日 下午9:26:00 35 | * @version 36 | * @since JDK 1.8 37 | */ 38 | public class BrowserFactory { 39 | 40 | public static void main(String[] args) { 41 | WebDriver fireFox = BrowserFactory.getChrome(); 42 | fireFox.close(); 43 | } 44 | 45 | public static WebDriver getIE() { 46 | try { 47 | DriverInitter.init(); 48 | } catch (IOException e) { 49 | return null; 50 | } 51 | WebDriver browser = new InternetExplorerDriver(); 52 | return browser; 53 | } 54 | 55 | public static WebDriver getPhantomJS() { 56 | try { 57 | DriverInitter.init(); 58 | } catch (IOException e) { 59 | return null; 60 | } 61 | WebDriver browser = new PhantomJSDriver(); 62 | return browser; 63 | } 64 | 65 | public static WebDriver getFireFox() { 66 | try { 67 | DriverInitter.init(); 68 | } catch (IOException e) { 69 | return null; 70 | } 71 | WebDriver browser = new FirefoxDriver(); 72 | return browser; 73 | } 74 | 75 | public static WebDriver getChrome() { 76 | try { 77 | DriverInitter.init(); 78 | } catch (IOException e) { 79 | return null; 80 | } 81 | WebDriver browser = new ChromeDriver(); 82 | return browser; 83 | } 84 | 85 | 86 | 87 | public static CookieStore getCookieStore(WebDriver driver) { 88 | CookieStore store = new BasicCookieStore(); 89 | Set cs = driver.manage().getCookies(); 90 | System.out.println(cs.size()); 91 | for (Cookie c : cs) { 92 | BasicClientCookie c1 = new BasicClientCookie(c.getName(), c.getValue()); 93 | if (c.getDomain() == null) { 94 | System.out.println(c.getName() + "->" + c.getValue()); 95 | } else { 96 | System.out.println(c.getDomain()); 97 | } 98 | 99 | c1.setDomain(c.getDomain() == null ? "my.alipay.com" : c.getDomain()); 100 | c1.setPath(c.getPath()); 101 | c1.setExpiryDate(c.getExpiry()); 102 | store.addCookie(c1); 103 | } 104 | System.out.println(store.getCookies().size()); 105 | return store; 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/utils/LoginUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | 19 | package zz.pseas.ghost.utils; 20 | 21 | import java.io.File; 22 | import java.sql.Timestamp; 23 | import java.text.SimpleDateFormat; 24 | import java.util.Calendar; 25 | import java.util.concurrent.ThreadLocalRandom; 26 | 27 | import org.apache.commons.lang3.SystemUtils; 28 | 29 | 30 | public class LoginUtils { 31 | 32 | private static final long COUNT = 10000000; 33 | private static final int THREADS = 2; 34 | 35 | // 36 | protected int sleeps; 37 | 38 | public int getSleeps() { 39 | return sleeps; 40 | } 41 | 42 | public void setSleeps(int sleeps) { 43 | this.sleeps = sleeps; 44 | } 45 | 46 | // 停顿固定的时间=0.3秒// 47 | public static void minSleep() { 48 | try { 49 | Thread.sleep(3 * 100); 50 | } catch (InterruptedException e) { 51 | // throw e; 52 | } 53 | } 54 | 55 | // 设置线程停顿时间,1秒-millis之间的任意时间停顿 // 56 | public static void sleep(long millis) { 57 | //Random rand = new Random(System.currentTimeMillis()); 58 | ThreadLocalRandom tlr = ThreadLocalRandom.current(); 59 | // 平均模拟停顿时间30秒// 60 | int minInternalTime = 1 * 1000; 61 | int maxInternalTime = (int) millis; 62 | int randnum = 0; 63 | 64 | if (maxInternalTime < minInternalTime) { 65 | maxInternalTime = 3 * minInternalTime + 1; 66 | } 67 | randnum = tlr.nextInt(maxInternalTime) % (maxInternalTime - minInternalTime + 1) + minInternalTime; 68 | // 69 | if (randnum <= 0) 70 | return; 71 | try { 72 | Thread.sleep(randnum); 73 | //Thread.currentThread().sleep(randnum); 74 | } catch (InterruptedException e) { 75 | } 76 | } 77 | 78 | // 以标准格式返回此刻时间// 79 | public static String getCurrentTime() { 80 | Timestamp ts = new Timestamp(System.currentTimeMillis()); 81 | return ts.toString(); 82 | } 83 | 84 | // 85 | public static String getCurrentTime(String format){ 86 | 87 | Calendar calendar = Calendar.getInstance(); 88 | //format=yyyyMMdd-hhmmss// 89 | SimpleDateFormat formats = new SimpleDateFormat(format); 90 | String time = formats.format(calendar.getTime()); 91 | //System.out.println(time); 92 | return time; 93 | } 94 | //////////// 95 | // runner for all tests 96 | 97 | /** 98 | * getCaptchaDir:.
99 | * 新建Captcha目录// 100 | */ 101 | public static void getCaptchaDir(){ 102 | 103 | File tmpath=SystemUtils.getUserDir(); 104 | String path=tmpath.toString()+"\\captcha"; 105 | File workdir=new File(path); 106 | if(!workdir.exists()){ 107 | workdir.mkdirs(); 108 | } 109 | } 110 | 111 | /* 112 | * 任何情况下都不要在多个线程间共享一个java.util.Random实例,而该把它放入ThreadLocal之中。 113 | * Java7在所有情形下都更推荐使用java.util.concurrent.ThreadLocalRandom——它向下兼容已有的代码且运营成本更低。 114 | */ 115 | public static void main(String[] args) { 116 | 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/utils/StringUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.utils; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | import java.util.regex.Matcher; 24 | import java.util.regex.Pattern; 25 | 26 | import net.sf.json.JSONObject; 27 | /** 28 | * @date 2016年9月14日 下午9:26:00 29 | * @version 30 | * @since JDK 1.8 31 | */ 32 | public class StringUtil { 33 | public static boolean isEmpty(String str) { 34 | if (str == null || str.trim().equals("")) 35 | return true; 36 | return false; 37 | } 38 | 39 | public static boolean isNotEmpty(String str) { 40 | return !isEmpty(str); 41 | } 42 | 43 | public static String getJsonStr(JSONObject jso, String key) { 44 | Object o = jso.get(key); 45 | if (o != null) 46 | return o.toString(); 47 | return null; 48 | } 49 | 50 | public static String getJsonStr(String json, String key) { 51 | try { 52 | JSONObject jso = JSONObject.fromObject(json); 53 | Object o = jso.get(key); 54 | if (o != null) 55 | return o.toString(); 56 | return null; 57 | } catch (Exception e) { 58 | return null; 59 | } 60 | } 61 | 62 | public static String regex(String str, String pat) { 63 | Pattern pattern = Pattern.compile(pat); 64 | Matcher m = pattern.matcher(str); 65 | if (m.find()) { 66 | return m.group(); 67 | } 68 | return null; 69 | } 70 | 71 | public static List parseNumbers(String str) { 72 | Pattern pattern = Pattern.compile("\\d+\\.*\\d*"); 73 | Matcher m = pattern.matcher(str); 74 | 75 | List res = new ArrayList(); 76 | while (m.find()) { 77 | String tmp = m.group().trim(); 78 | res.add(Double.parseDouble(tmp)); 79 | } 80 | return res.isEmpty() ? null : res; 81 | } 82 | 83 | public static List parsePercentages(String str) { 84 | str = str.replaceAll("\\s", "").replaceAll(",", "").replaceAll("O", "0"); 85 | List arr = new ArrayList(); 86 | String pat = "(\\d+\\.*\\d*(?=-\\d*\\.*\\d*%|%))|(\\d+\\.*\\d*(?=%|%))"; 87 | Pattern p = Pattern.compile(pat); 88 | Matcher m = p.matcher(str); 89 | while (m.find()) { 90 | String tmp = m.group().trim(); 91 | // System.out.println(tmp); 92 | arr.add(Double.parseDouble(tmp)); 93 | } 94 | 95 | return arr.isEmpty() ? null : arr; 96 | } 97 | 98 | public static double findMax(List nums) { 99 | double m = Double.MIN_VALUE; 100 | for (Double x : nums) { 101 | if (m < x) 102 | m = x; 103 | } 104 | return m; 105 | } 106 | 107 | public static double findMin(List nums) { 108 | double m = Double.MAX_VALUE; 109 | for (Double x : nums) { 110 | if (m > x) 111 | m = x; 112 | } 113 | return m; 114 | } 115 | 116 | public static String sqlSingleQuote(String str) { 117 | return new StringBuffer("`").append(str).append("`").toString(); 118 | } 119 | 120 | } -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/taobao/RsaUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.taobao; 20 | 21 | import java.math.BigInteger; 22 | import java.security.KeyFactory; 23 | import java.security.interfaces.RSAPublicKey; 24 | import java.security.spec.RSAPublicKeySpec; 25 | 26 | import javax.crypto.Cipher; 27 | 28 | /** 29 | * @date 2016年9月14日 下午9:26:00 30 | * @version 31 | * @since JDK 1.8 32 | */ 33 | public class RsaUtil { 34 | public static String enCode(String pbk, String exponent, String data) throws Exception { 35 | BigInteger pbk1 = new BigInteger(pbk, 16); 36 | BigInteger exp = new BigInteger("10001", 16); 37 | 38 | KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 39 | RSAPublicKeySpec keySpec = new RSAPublicKeySpec(pbk1, exp); 40 | 41 | RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec); 42 | 43 | String ans = encryptByPublicKey(data, publicKey); 44 | 45 | return ans; 46 | 47 | } 48 | 49 | public static void main(String[] args) throws Exception { 50 | String ans = enCode( 51 | "9a39c3fefeadf3d194850ef3a1d707dfa7bec0609a60bfcc7fe4ce2c615908b9599c8911e800aff684f804413324dc6d9f982f437e95ad60327d221a00a2575324263477e4f6a15e3b56a315e0434266e092b2dd5a496d109cb15875256c73a2f0237c5332de28388693c643c8764f137e28e8220437f05b7659f58c4df94685", 52 | "10001", "123456"); 53 | System.out.println(ans.length()); 54 | } 55 | 56 | public static String encryptByPublicKey(String data, RSAPublicKey publicKey) throws Exception { 57 | Cipher cipher = Cipher.getInstance("RSA"); 58 | 59 | cipher.init(Cipher.ENCRYPT_MODE, publicKey); 60 | // 模长 61 | int key_len = publicKey.getModulus().bitLength() / 8; 62 | // 加密数据长度 <= 模长-11 63 | String[] datas = splitString(data, key_len - 11); 64 | String mi = ""; 65 | // 如果明文长度大于模长-11则要分组加密 66 | for (String s : datas) { 67 | mi += bcd2Str(cipher.doFinal(s.getBytes())); 68 | } 69 | return mi; 70 | } 71 | 72 | public static String bcd2Str(byte[] bytes) { 73 | char temp[] = new char[bytes.length * 2], val; 74 | 75 | for (int i = 0; i < bytes.length; i++) { 76 | val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f); 77 | temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); 78 | 79 | val = (char) (bytes[i] & 0x0f); 80 | temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); 81 | } 82 | return new String(temp); 83 | } 84 | 85 | public static String[] splitString(String string, int len) { 86 | int x = string.length() / len; 87 | int y = string.length() % len; 88 | int z = 0; 89 | if (y != 0) { 90 | z = 1; 91 | } 92 | String[] strings = new String[x + z]; 93 | String str = ""; 94 | for (int i = 0; i < x + z; i++) { 95 | if (i == x + z - 1 && y != 0) { 96 | str = string.substring(i * len, i * len + y); 97 | } else { 98 | str = string.substring(i * len, i * len + len); 99 | } 100 | strings[i] = str; 101 | } 102 | return strings; 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/weibo/WeibocnLogin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * 18 | */ 19 | package zz.pseas.ghost.login.weibo; 20 | 21 | import java.io.IOException; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | import org.apache.http.Consts; 26 | import org.apache.http.client.entity.UrlEncodedFormEntity; 27 | import org.apache.http.client.methods.CloseableHttpResponse; 28 | import org.apache.http.client.methods.HttpGet; 29 | import org.apache.http.client.methods.HttpPost; 30 | import org.apache.http.impl.client.CloseableHttpClient; 31 | import org.apache.http.impl.client.HttpClients; 32 | import org.apache.http.message.BasicNameValuePair; 33 | import org.apache.http.util.EntityUtils; 34 | 35 | /** 36 | * @date 2016年9月14日 下午9:26:00 37 | * @version 38 | * @since JDK 1.8 39 | */ 40 | 41 | public class WeibocnLogin { 42 | public static void main(String[] args) throws NullPointerException { 43 | 44 | // 登录页面的URL 45 | String Loginurl = "http://login.weibo.cn/login/"; 46 | String firstpage = "http://weibo.cn/?vt=4"; // 微博首页的网址 47 | String loginnum = "test"; 48 | String loginpwd = "test"; 49 | 50 | CloseableHttpClient httpclient = HttpClients.createDefault(); // 建立客户端的链接 51 | HttpGet httpget = new HttpGet(Loginurl); 52 | 53 | try { 54 | CloseableHttpResponse response = httpclient.execute(httpget); 55 | 56 | String responhtml = null; 57 | try { 58 | responhtml = EntityUtils.toString(response.getEntity()); 59 | // System.out.println(responhtml); 60 | } catch (IOException e1) { 61 | e1.printStackTrace(); 62 | } 63 | 64 | // 将vk的值截出来,split将整个的html分为两个部分,截取下面的部分 65 | String vk = responhtml.split(" pairs = new ArrayList(); 74 | pairs.add(new BasicNameValuePair("mobile", loginnum)); 75 | pairs.add(new BasicNameValuePair(finalpass, loginpwd)); 76 | pairs.add(new BasicNameValuePair("remember", "on")); 77 | pairs.add(new BasicNameValuePair("vk", vk)); 78 | 79 | UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, Consts.UTF_8); // 返回的实体 80 | HttpPost httppost = new HttpPost(Loginurl); 81 | httppost.setEntity(entity); 82 | // 检测以下响应状态是否正确 83 | CloseableHttpResponse response2 = httpclient.execute(httppost); 84 | System.out.println(response2.getStatusLine().toString()); 85 | httpclient.execute(httppost); // 登录操作 86 | System.out.println("success"); 87 | 88 | HttpGet getinfo = new HttpGet("http://m.weibo.cn/p/100803?vt=4"); 89 | CloseableHttpResponse res; 90 | res = httpclient.execute(getinfo); 91 | System.out.println("进入热门话题的页面:"); 92 | // 输出该页面的html文件 93 | System.out.println(EntityUtils.toString(res.getEntity())); 94 | res.close(); 95 | 96 | } catch (IOException e) { 97 | e.printStackTrace(); 98 | } finally { 99 | try { 100 | httpclient.close(); 101 | } catch (IOException e) { 102 | e.printStackTrace(); 103 | } 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/utils/BrowserUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.utils; 20 | 21 | import java.awt.Graphics; 22 | import java.awt.GraphicsConfiguration; 23 | import java.awt.GraphicsDevice; 24 | import java.awt.GraphicsEnvironment; 25 | import java.awt.HeadlessException; 26 | import java.awt.Image; 27 | import java.awt.Transparency; 28 | import java.awt.image.BufferedImage; 29 | import java.io.ByteArrayInputStream; 30 | import java.io.File; 31 | import java.io.IOException; 32 | 33 | import javax.imageio.ImageIO; 34 | import javax.swing.ImageIcon; 35 | 36 | import org.apache.commons.io.output.ByteArrayOutputStream; 37 | import org.openqa.selenium.By; 38 | import org.openqa.selenium.Dimension; 39 | import org.openqa.selenium.OutputType; 40 | import org.openqa.selenium.Point; 41 | import org.openqa.selenium.TakesScreenshot; 42 | import org.openqa.selenium.WebDriver; 43 | import org.openqa.selenium.WebElement; 44 | 45 | import zz.pseas.ghost.browser.BrowserFactory; 46 | /** 47 | * @date 2016年9月13日 下午9:26:00 48 | * @version 49 | * @since JDK 1.8 50 | */ 51 | public class BrowserUtil { 52 | public static byte[] captureScreenShotById(WebDriver driver, String id) throws IOException { 53 | byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); 54 | WebElement ele = driver.findElement(By.id(id)); 55 | ByteArrayInputStream in = new ByteArrayInputStream(bytes); 56 | 57 | BufferedImage image = ImgUtil.bytesToBImage(bytes); 58 | 59 | ImageIO.write(image, "jpg", new File("d:/big.jpg")); 60 | in.close(); 61 | System.out.println(image.getType()); 62 | 63 | 64 | Point p = ele.getLocation(); 65 | Dimension xy = ele.getSize(); 66 | 67 | BufferedImage subImage = image.getSubimage(p.getX(), p.getY(), xy.getWidth(), xy.getHeight()); 68 | ImageIO.write(subImage, "jpg", new File("d:/sc.jpg")); 69 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 70 | 71 | ImageIO.write(subImage, "jpg", out); 72 | out.flush(); 73 | byte[] res = out.toByteArray(); 74 | out.close(); 75 | return res; 76 | } 77 | 78 | public static void main(String[] args) throws IOException { 79 | WebDriver ie = BrowserFactory.getIE(); 80 | ie.get("https://passport.jd.com/new/login.aspx"); 81 | byte[] bytes = captureScreenShotById(ie, "loginsubmit"); 82 | BufferedImage sc1 = ImgUtil.bytesToBImage(bytes); 83 | ImageIO.write(sc1, "jpg", new File("d:/sc1.jpg")); 84 | } 85 | 86 | public static BufferedImage toBufferedImage(Image image) { 87 | if (image instanceof BufferedImage) { 88 | return (BufferedImage) image; 89 | } 90 | // This code ensures that all the pixels in the image are loaded 91 | image = new ImageIcon(image).getImage(); 92 | BufferedImage bimage = null; 93 | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 94 | try { 95 | int transparency = Transparency.OPAQUE; 96 | GraphicsDevice gs = ge.getDefaultScreenDevice(); 97 | GraphicsConfiguration gc = gs.getDefaultConfiguration(); 98 | bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); 99 | } catch (HeadlessException e) { 100 | // The system does not have a screen 101 | } 102 | if (bimage == null) { 103 | // Create a buffered image using the default color model 104 | int type = BufferedImage.TYPE_INT_RGB; 105 | bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); 106 | } 107 | // Copy image to buffered image 108 | Graphics g = bimage.createGraphics(); 109 | // Paint the image onto the buffered image 110 | g.drawImage(image, 0, 0, null); 111 | g.dispose(); 112 | return bimage; 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /ghost-login/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | zz.pseas.ghost 6 | Gost-Login 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | Ghost-Login 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 16 | 17 | 18 | 19 | 20 | org.jsoup 21 | jsoup 22 | 1.9.2 23 | 24 | 25 | 26 | 27 | org.apache.httpcomponents 28 | httpclient 29 | 4.5.2 30 | 31 | 32 | 33 | 34 | org.apache.httpcomponents 35 | httpasyncclient 36 | 4.1.1 37 | 38 | 39 | 40 | 41 | org.apache.httpcomponents 42 | httpcore 43 | 4.4.4 44 | 45 | 46 | 47 | commons-httpclient 48 | commons-httpclient 49 | 3.1 50 | 51 | 52 | 53 | 54 | org.bouncycastle 55 | bcprov-jdk15on 56 | 1.55 57 | 58 | 59 | 60 | org.seleniumhq.selenium 61 | selenium-api 62 | 2.53.0 63 | 64 | 65 | 66 | org.seleniumhq.selenium 67 | selenium-java 68 | 2.53.0 69 | 70 | 71 | 72 | org.seleniumhq.selenium 73 | selenium-server 74 | 2.53.0 75 | 76 | 77 | 78 | org.seleniumhq.selenium 79 | selenium-htmlunit-driver 80 | 2.52.0 81 | 82 | 83 | 84 | org.seleniumhq.selenium 85 | selenium-firefox-driver 86 | 2.53.0 87 | 88 | 89 | 90 | org.seleniumhq.selenium 91 | selenium-chrome-driver 92 | 2.53.0 93 | 94 | 95 | 96 | org.seleniumhq.selenium 97 | selenium-ie-driver 98 | 2.53.0 99 | 100 | 101 | 102 | org.seleniumhq.selenium 103 | selenium-support 104 | 2.53.0 105 | 106 | 107 | 108 | 109 | com.codeborne 110 | phantomjsdriver 111 | 1.3.0 112 | 113 | 114 | 115 | net.sf.json-lib 116 | json-lib 117 | 2.4 118 | jdk15 119 | 120 | 121 | 122 | 123 | org.slf4j 124 | slf4j-api 125 | 1.7.9 126 | 127 | 128 | 129 | org.slf4j 130 | slf4j-log4j12 131 | 1.7.9 132 | 133 | 134 | 135 | log4j 136 | log4j 137 | 1.2.17 138 | 139 | 140 | 141 | junit 142 | junit 143 | 3.8.1 144 | test 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/baidu/BaiduLogin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.baidu; 20 | 21 | import static zz.pseas.ghost.utils.CodeUtils.getcode; 22 | import static zz.pseas.ghost.utils.CodeUtils.getcodeWithAnd; 23 | import static zz.pseas.ghost.utils.CodeUtils.getcodeWithSingleQua; 24 | import static zz.pseas.ghost.utils.CodeUtils.getcodeWithoutQua; 25 | import static zz.pseas.ghost.utils.HttpClinetUtil.get; 26 | import static zz.pseas.ghost.utils.HttpClinetUtil.getCookie; 27 | import static zz.pseas.ghost.utils.HttpClinetUtil.post; 28 | 29 | import java.io.IOException; 30 | import java.net.URL; 31 | import java.util.HashMap; 32 | import java.util.Map; 33 | 34 | import javax.swing.ImageIcon; 35 | import javax.swing.JOptionPane; 36 | /** 37 | * @date 2016年9月14日 下午9:26:00 38 | * @version 39 | * @since JDK 1.8 40 | */ 41 | public class BaiduLogin { 42 | 43 | public static String getBaiduCookie(String username,String password) throws IOException{ 44 | get("http://passport.baidu.com/phoenix/account/jsapi"); 45 | String BAIDUID = getcode(getCookie(), "BAIDUID"); 46 | String tokenContent = get( 47 | "https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=false", 48 | "BAIDUID=" + BAIDUID + ";HOSUPPORT=1"); 49 | String token = getcodeWithSingleQua(tokenContent, "login_token"); 50 | get("https://passport.baidu.com/v2/api/?loginhistory&token=" + token 51 | + "&tpl=mn&apiver=v3&tt=" + System.currentTimeMillis() 52 | + "&callback=bd__cbs__vehc6w"); 53 | String UBI = getcodeWithoutQua(getCookie(), "UBI"); 54 | BAIDUID = getcode(getCookie(), "BAIDUID"); 55 | Map para1 = new HashMap(); 56 | para1.put("apiver", "v3"); 57 | para1.put("callback", "parent.bd__pcbs__k2eobr"); 58 | para1.put("charset", "utf-8"); 59 | para1.put("codestring", ""); 60 | para1.put("isPhone", ""); 61 | para1.put("loginmerge", "true"); 62 | para1.put("logintype", "dialogLogin"); 63 | para1.put("logLoginType", "pc_loginDialog"); 64 | para1.put("mem_pass", "on"); 65 | para1.put("password", password); 66 | para1.put("ppui_logintime", "26604"); 67 | para1.put("quick_user", "0"); 68 | para1.put("safeflg", "0"); 69 | para1.put("splogin", "rate"); 70 | para1.put("staticpage", 71 | "http://www.baidu.com/cache/user/html/v3Jump.html"); 72 | para1.put("token", token); 73 | para1.put("tpl", "mn"); 74 | para1.put("tt", Long.toString(System.currentTimeMillis())); 75 | para1.put("u", "http://www.baidu.com/"); 76 | para1.put("username", username); 77 | para1.put("verifycode", ""); 78 | String VCodeContent = post("https://passport.baidu.com/v2/api/?login", 79 | para1, getCookie()); 80 | String codeString = getcodeWithAnd(VCodeContent, "codeString"); 81 | String code = JOptionPane.showInputDialog(null, new ImageIcon(new URL( 82 | "https://passport.baidu.com/cgi-bin/genimage?" + codeString))); 83 | Map para2 = new HashMap(); 84 | para2.put("apiver", "v3"); 85 | para2.put("callback", "parent.bd__pcbs__k2eobr"); 86 | para2.put("charset", "utf-8"); 87 | para2.put("codestring", codeString); 88 | para2.put("isPhone", ""); 89 | para2.put("loginmerge", "true"); 90 | para2.put("logintype", "dialogLogin"); 91 | para2.put("logLoginType", "pc_loginDialog"); 92 | para2.put("mem_pass", "on"); 93 | para2.put("password", password); 94 | para2.put("ppui_logintime", "26604"); 95 | para2.put("quick_user", "0"); 96 | para2.put("safeflg", "0"); 97 | para2.put("splogin", "rate"); 98 | para2.put("staticpage", 99 | "http://www.baidu.com/cache/user/html/v3Jump.html"); 100 | para2.put("token", token); 101 | para2.put("tpl", "mn"); 102 | para2.put("tt", Long.toString(System.currentTimeMillis())); 103 | para2.put("u", "http://www.baidu.com/"); 104 | para2.put("username", username); 105 | para2.put("verifycode", code); 106 | String loginContent = post("https://passport.baidu.com/v2/api/?login", 107 | para2, "BAIDUID=" + BAIDUID + ";UBI=" + UBI + ";HOSUPPORT=1"); 108 | String param = getcodeWithAnd(loginContent, "hao123Param"); 109 | get("http://user.hao123.com/static/crossdomain.php?bdu=" 110 | + param + "&t=" + System.currentTimeMillis()); 111 | return getCookie(); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/jd/JDCookie.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.jd; 20 | 21 | import java.util.HashMap; 22 | import java.util.HashSet; 23 | import java.util.Map; 24 | import java.util.Set; 25 | 26 | import org.apache.http.client.CookieStore; 27 | import org.apache.http.impl.client.BasicCookieStore; 28 | import org.apache.http.impl.cookie.BasicClientCookie; 29 | import org.jsoup.Jsoup; 30 | import org.jsoup.nodes.Document; 31 | import org.jsoup.nodes.Element; 32 | import org.jsoup.select.Elements; 33 | import org.openqa.selenium.Cookie; 34 | import org.openqa.selenium.WebDriver; 35 | import org.openqa.selenium.ie.InternetExplorerDriver; 36 | import org.openqa.selenium.phantomjs.PhantomJSDriver; 37 | 38 | import zz.pseas.ghost.browser.BrowserFactory; 39 | import zz.pseas.ghost.browser.DriverInitter; 40 | import zz.pseas.ghost.utils.StringUtil; 41 | /** 42 | * @date 2016年9月14日 下午9:26:00 43 | * @version 44 | * @since JDK 1.8 45 | */ 46 | @SuppressWarnings("unused") 47 | public class JDCookie { 48 | public static int maxCheckTimes = 10; 49 | public static int waitInterval = 100; // milliseconds 50 | public static String[] requiredCookies = { "3AB9D23F7A4B3C9B" }; 51 | 52 | private WebDriver browser = null; 53 | private boolean cookieDone = false; 54 | private boolean paramDone = false; 55 | 56 | public void init() { 57 | boolean done = false; 58 | try { 59 | DriverInitter.init(); 60 | browser = BrowserFactory.getIE(); 61 | //browser = new InternetExplorerDriver(); 62 | browser.manage().deleteAllCookies(); 63 | browser.get("https://passport.jd.com/new/login.aspx"); 64 | 65 | int count = 0; 66 | while (true && count < maxCheckTimes) { 67 | System.out.println("check for the " + (++count) + "th time..."); 68 | done = isBrowserDone(); 69 | if (done) { 70 | break; 71 | } 72 | Thread.sleep(waitInterval); 73 | } 74 | 75 | if (!done) { 76 | System.out.println("browser collection failed..."); 77 | } 78 | } catch (Exception e) { 79 | e.printStackTrace(); 80 | } finally { 81 | } 82 | } 83 | 84 | public boolean isBrowserDone() { 85 | String html = browser.getPageSource(); 86 | if (!paramDone) { 87 | Document doc = Jsoup.parse(html); 88 | String eid = doc.select("input[name=eid]").first().attr("value"); 89 | String fp = doc.select("input[name=fp]").first().attr("value"); 90 | if (StringUtil.isNotEmpty(eid) && StringUtil.isNotEmpty(fp)) { 91 | paramDone = true; 92 | cookieDone = cookiesReady(); 93 | return cookieDone; 94 | } 95 | } else { 96 | cookieDone = cookiesReady(); 97 | return cookieDone; 98 | } 99 | return false; 100 | } 101 | 102 | public boolean cookiesReady() { 103 | Set cookies = browser.manage().getCookies(); 104 | Set ks = new HashSet(); 105 | for (Cookie c : cookies) { 106 | String k = c.getName(); 107 | ks.add(k); 108 | } 109 | 110 | for (String k : requiredCookies) { 111 | if (!ks.contains(k)) { 112 | return false; 113 | } 114 | } 115 | 116 | return true; 117 | 118 | } 119 | 120 | public CookieStore supplyCookies() { 121 | if (!(paramDone && cookieDone)) { 122 | return null; 123 | } 124 | 125 | BasicCookieStore cookieStore = new BasicCookieStore(); 126 | Set cookies = browser.manage().getCookies(); 127 | for (Cookie c : cookies) { 128 | BasicClientCookie c1 = new BasicClientCookie(c.getName(), c.getValue()); 129 | /* 130 | c1.setDomain(c.getDomain()); 131 | c1.setPath(c.getPath()); 132 | */ 133 | c1.setExpiryDate(c.getExpiry()); 134 | cookieStore.addCookie(c1); 135 | } 136 | return cookieStore; 137 | } 138 | 139 | public Map supplyParams() { 140 | if (!(paramDone && cookieDone)) { 141 | return null; 142 | } 143 | 144 | HashMap map = new HashMap(); 145 | 146 | String html = browser.getPageSource(); 147 | Document doc = Jsoup.parse(html); 148 | Elements inputs = doc.select("form[id=formlogin] input"); 149 | for (Element input : inputs) { 150 | String k = input.attr("name"); 151 | String v = input.attr("value"); 152 | map.put(k, v); 153 | } 154 | 155 | return map; 156 | } 157 | 158 | public void close() { 159 | if (browser != null) { 160 | browser.close(); 161 | browser.quit(); 162 | } 163 | } 164 | 165 | 166 | 167 | public WebDriver getBrowser() { 168 | return browser; 169 | } 170 | 171 | public static void main(String[] args) { 172 | JDCookie cookieParamSupplier = new JDCookie(); 173 | cookieParamSupplier.init(); 174 | CookieStore cookieStore = cookieParamSupplier.supplyCookies(); 175 | Map map = cookieParamSupplier.supplyParams(); 176 | System.out.println("done" + map + cookieStore); 177 | cookieParamSupplier.close(); 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/taobao/MTaobaoLogin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.login.taobao; 20 | 21 | import java.io.UnsupportedEncodingException; 22 | import java.util.HashMap; 23 | 24 | import org.apache.http.HttpHost; 25 | import org.apache.http.client.config.RequestConfig; 26 | import org.apache.http.client.methods.HttpUriRequest; 27 | import org.apache.http.client.methods.RequestBuilder; 28 | import org.jsoup.Jsoup; 29 | import org.jsoup.nodes.Document; 30 | 31 | import zz.pseas.ghost.client.GhostClient; 32 | import zz.pseas.ghost.client.CommonsPage; 33 | import zz.pseas.ghost.utils.FileUtil; 34 | import zz.pseas.ghost.utils.MD5Util; 35 | import zz.pseas.ghost.utils.StringUtil; 36 | /** 37 | * @date 2016年9月14日 下午9:26:00 38 | * @version 39 | * @since JDK 1.8 40 | */ 41 | public class MTaobaoLogin { 42 | public static void main(String[] args) throws UnsupportedEncodingException { 43 | 44 | 45 | String tbuserNmae="TBname"; 46 | String tbpassWord="TBpasssword"; 47 | 48 | GhostClient iPhone = new GhostClient("utf-8"); 49 | String ans = iPhone.get("https://login.m.taobao.com/login.htm"); 50 | 51 | Document doc = Jsoup.parse(ans); 52 | String url = doc.select("form#loginForm").first().attr("action"); 53 | 54 | String _tb_token = doc.select("input[name=_tb_token_]").first().attr("value"); 55 | 56 | String sid = doc.select("input[name=sid]").first().attr("value"); 57 | System.out.println(_tb_token); 58 | System.out.println(sid); 59 | System.out.println(url); 60 | 61 | HashMap map = new HashMap(); 62 | map.put("TPL_password", tbpassWord); 63 | map.put("TPL_username", tbuserNmae); 64 | map.put("_tb_token_", _tb_token); 65 | map.put("action", "LoginAction"); 66 | map.put("event_submit_do_login", "1"); 67 | map.put("loginFrom", "WAP_TAOBAO"); 68 | map.put("sid", sid); 69 | 70 | String location = null; 71 | while (true) { 72 | CommonsPage commonsPage = iPhone.postForPage(url, map); 73 | location = commonsPage.getHeader("Location"); 74 | String postAns = new String(commonsPage.getContents(), "utf-8"); 75 | if (StringUtil.isNotEmpty(location) && StringUtil.isEmpty(postAns)) { 76 | break; 77 | } 78 | 79 | String s = Jsoup.parse(postAns).select("img.checkcode-img").first().attr("src"); 80 | String imgUrl = "https:" + s; 81 | 82 | byte[] bytes = iPhone.getBytes(imgUrl); 83 | FileUtil.writeFile(bytes, "g:/tbCaptcha.jpg"); 84 | 85 | String wepCheckId = Jsoup.parse(postAns).select("input[name=wapCheckId]").val(); 86 | String captcha = null; 87 | map.put("TPL_checkcode", captcha); 88 | map.put("wapCheckId", wepCheckId); 89 | } 90 | 91 | iPhone.get(location); 92 | 93 | String tk = iPhone.getCookieValue("_m_h5_tk"); 94 | if (StringUtil.isNotEmpty(tk)) { 95 | tk = tk.split("_")[0]; 96 | } else { 97 | tk = "undefined"; 98 | } 99 | 100 | 101 | String url2 = genUrl(tk); 102 | String ans1 = iPhone.get(url2); 103 | System.out.println(url2); 104 | System.out.println(ans1); 105 | 106 | 107 | tk = iPhone.getCookieValue("_m_h5_tk").split("_")[0]; 108 | if (StringUtil.isEmpty(tk)) { 109 | tk = "undefined"; 110 | } 111 | System.out.println(tk); 112 | url2 = genUrl(tk); 113 | iPhone.showCookies(); 114 | RequestConfig requestConfig = RequestConfig.custom() 115 | .setProxy(new HttpHost("127.0.0.1",8888)) 116 | .build(); 117 | 118 | HttpUriRequest get = RequestBuilder.get() 119 | .setConfig(requestConfig) 120 | //.addHeader("User-Agent","Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16") 121 | .addHeader("Host","api.m.taobao.com") 122 | .setUri(url2) 123 | .build(); 124 | 125 | ans1 = iPhone.execute(get); 126 | 127 | System.out.println(ans1); 128 | 129 | } 130 | 131 | public static String replaceStr(String s) { 132 | String ans = s.replaceAll("\\{", "%7B").replaceAll("\\}", "%7D").replaceAll("\"", "%22") 133 | .replaceAll("\\\\", "%5C").replaceAll(":", "%3A").replaceAll("#", "%23").replaceAll(",", "%2C") 134 | .replaceFirst("%3A", ":"); 135 | return ans; 136 | } 137 | 138 | public static String genUrl(String tk) { 139 | String t = String.valueOf(System.currentTimeMillis()); 140 | String appKey = "12574478"; 141 | String data = "{\"spm\":\"a2141.7756461.2.6\",\"page\":1,\"tabCode\":\"all\",\"appVersion\":\"1.0\",\"appName\":\"tborder\"}"; 142 | String s = tk + "&" + t + "&" + appKey + "&" + data; 143 | String sign = MD5Util.string2MD5(s); 144 | 145 | StringBuffer sb = new StringBuffer("http://api.m.taobao.com/h5/mtop.order.queryboughtlist/3.0/?"); 146 | sb.append("AntiCreep=").append("true"); 147 | sb.append("&AntiFlood=").append("true"); 148 | sb.append("&LoginRequest=").append("mtop.order.queryBoughtList"); 149 | sb.append("&api=").append("mtop.order.queryBoughtList"); 150 | sb.append("&appKey=").append("12574478"); 151 | sb.append("&callback=").append("mtopjsonp1"); 152 | sb.append("&data=").append(data); 153 | sb.append("&dataType=").append("jsonp"); 154 | sb.append("&ecode=").append("1"); 155 | sb.append("&sign=").append(sign); 156 | sb.append("&t=").append(t); 157 | sb.append("&ttid=").append("##h5"); 158 | sb.append("&type=").append("jsonp"); 159 | sb.append("&v=").append("3.0"); 160 | 161 | String url = sb.toString(); 162 | 163 | return replaceStr(url); 164 | } 165 | 166 | } 167 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/client/GhostClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.client; 20 | 21 | import java.io.BufferedReader; 22 | import java.io.File; 23 | import java.io.FileReader; 24 | import java.io.IOException; 25 | import java.io.UnsupportedEncodingException; 26 | import java.util.ArrayList; 27 | import java.util.List; 28 | import java.util.Map; 29 | 30 | import org.apache.http.Header; 31 | import org.apache.http.NameValuePair; 32 | import org.apache.http.client.CookieStore; 33 | import org.apache.http.client.entity.UrlEncodedFormEntity; 34 | import org.apache.http.client.methods.CloseableHttpResponse; 35 | import org.apache.http.client.methods.HttpGet; 36 | import org.apache.http.client.methods.HttpPost; 37 | import org.apache.http.client.methods.HttpUriRequest; 38 | import org.apache.http.client.protocol.HttpClientContext; 39 | import org.apache.http.cookie.Cookie; 40 | import org.apache.http.impl.client.BasicCookieStore; 41 | import org.apache.http.impl.client.CloseableHttpClient; 42 | import org.apache.http.impl.cookie.BasicClientCookie; 43 | import org.apache.http.message.BasicNameValuePair; 44 | import org.apache.http.util.EntityUtils; 45 | 46 | import zz.pseas.ghost.utils.DownloadUtil; 47 | /** 48 | * @date 2016年9月14日 下午9:26:00 49 | * @version 50 | * @since JDK 1.8 51 | */ 52 | public class GhostClient { 53 | private String charset = "GBK"; 54 | 55 | public String getCharset() { 56 | return charset; 57 | } 58 | 59 | public void setCharset(String charset) { 60 | this.charset = charset; 61 | } 62 | 63 | private CloseableHttpClient client = ClientFactory.getNewClient(); 64 | private HttpClientContext context = HttpClientContext.create(); 65 | 66 | public GhostClient() { 67 | super(); 68 | } 69 | 70 | public GhostClient(String charset) { 71 | super(); 72 | this.charset = charset; 73 | context = HttpClientContext.create(); 74 | } 75 | 76 | public GhostClient(String charset, CookieStore cookies) { 77 | this(charset); 78 | context.setCookieStore(cookies); 79 | } 80 | 81 | public CloseableHttpClient getClient() { 82 | return client; 83 | } 84 | 85 | public void setClient(CloseableHttpClient client) { 86 | this.client = client; 87 | } 88 | 89 | public HttpClientContext getContext() { 90 | return context; 91 | } 92 | 93 | public void setContext(HttpClientContext context) { 94 | this.context = context; 95 | } 96 | 97 | public String execute(HttpUriRequest request, String charset) { 98 | DownloadUtil.setUserAgent(request); 99 | String ans = null; 100 | try { 101 | CloseableHttpResponse resp = client.execute(request, context); 102 | ans = EntityUtils.toString(resp.getEntity(), charset); 103 | EntityUtils.consume(resp.getEntity()); 104 | } catch (IOException e) { 105 | ans = null; 106 | } finally { 107 | if (request instanceof HttpGet) { 108 | ((HttpGet) request).releaseConnection(); 109 | } else if (request instanceof HttpPost) { 110 | ((HttpPost) request).releaseConnection(); 111 | } else { 112 | 113 | } 114 | } 115 | return ans; 116 | } 117 | 118 | public String execute(HttpUriRequest request) { 119 | return execute(request, charset); 120 | } 121 | 122 | public String post(String url, Map params) { 123 | HttpPost post = new HttpPost(url); 124 | ArrayList arr = new ArrayList(); 125 | for (Map.Entry entry : params.entrySet()) { 126 | arr.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); 127 | } 128 | 129 | try { 130 | UrlEncodedFormEntity entity = new UrlEncodedFormEntity(arr, "utf-8"); 131 | post.setEntity(entity); 132 | String ans = execute(post); 133 | return ans; 134 | } catch (UnsupportedEncodingException e) { 135 | e.printStackTrace(); 136 | return null; 137 | } finally { 138 | post.releaseConnection(); 139 | } 140 | } 141 | 142 | public CommonsPage postForPage(String url, Map params) { 143 | HttpPost post = new HttpPost(url); 144 | ArrayList arr = new ArrayList(); 145 | for (Map.Entry entry : params.entrySet()) { 146 | arr.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); 147 | } 148 | 149 | CommonsPage commonsPage = new CommonsPage(); 150 | try { 151 | UrlEncodedFormEntity entity = new UrlEncodedFormEntity(arr, "utf-8"); 152 | post.setEntity(entity); 153 | 154 | CloseableHttpResponse resp = client.execute(post, context); 155 | Header[] headers = resp.getAllHeaders(); 156 | for (Header h : headers) { 157 | String k = h.getName(); 158 | String v = h.getValue(); 159 | commonsPage.addHeader(k, v); 160 | } 161 | 162 | byte[] bytes = EntityUtils.toByteArray(resp.getEntity()); 163 | commonsPage.setContents(bytes); 164 | } catch (Exception e) { 165 | commonsPage.setErrMsg(e.getMessage()); 166 | } finally { 167 | post.releaseConnection(); 168 | } 169 | return commonsPage; 170 | } 171 | 172 | public String get(String url) { 173 | HttpGet get = new HttpGet(url); 174 | return execute(get); 175 | } 176 | 177 | public void showCookies() { 178 | List cookies = context.getCookieStore().getCookies(); 179 | for (Cookie c : cookies) { 180 | System.out.println(c.toString()); 181 | } 182 | } 183 | 184 | public byte[] getBytes(String url) { 185 | 186 | HttpGet get = new HttpGet(url); 187 | DownloadUtil.setUserAgent(get); 188 | CloseableHttpResponse resp; 189 | try { 190 | resp = client.execute(get, context); 191 | byte[] bs = EntityUtils.toByteArray(resp.getEntity()); 192 | EntityUtils.consume(resp.getEntity()); 193 | get.releaseConnection(); 194 | return bs; 195 | } catch (IOException e) { 196 | return null; 197 | } 198 | 199 | } 200 | 201 | public String getCookieValue(String cookieName) { 202 | List cs = context.getCookieStore().getCookies(); 203 | for (Cookie c : cs) { 204 | if (c.getName().equals(cookieName)) { 205 | return c.getValue(); 206 | } 207 | } 208 | return null; 209 | } 210 | 211 | public void addCookie(String k, String v) { 212 | BasicClientCookie cookie = new BasicClientCookie(k, v); 213 | cookie.setDomain(".taobao.com"); 214 | CookieStore cookieStore = context.getCookieStore(); 215 | if (cookieStore == null) { 216 | context.setCookieStore(new BasicCookieStore()); 217 | } 218 | context.getCookieStore().addCookie((Cookie) cookie); 219 | } 220 | 221 | public static void main(String[] args) throws Exception { 222 | GhostClient client = new GhostClient("utf-8"); 223 | /* 224 | * client.get("http://www.baidu.com"); client.addCookie("k1", "v1"); 225 | * client.showCookies(); 226 | */ 227 | client.importFirefoxCookie("g:/cookies.txt"); 228 | client.showCookies(); 229 | 230 | } 231 | 232 | public void addFirefoxCookie(String v) { 233 | 234 | } 235 | 236 | public void importFirefoxCookie(String path) throws Exception { 237 | FileReader reader = new FileReader(new File(path)); 238 | BufferedReader r2 = new BufferedReader(reader); 239 | while (true) { 240 | String s = r2.readLine(); 241 | if (s == null) 242 | break; 243 | int n = s.indexOf(";"); 244 | String s1 = s.substring(0, n); 245 | String k = s1.substring(0, s1.indexOf("=")); 246 | String v = s.substring(s.indexOf("=") + 1, s1.length()); 247 | 248 | addCookie(k, v); 249 | } 250 | r2.close(); 251 | } 252 | 253 | } 254 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/login/weibo/SinaWeiboLogin.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | */ 18 | package zz.pseas.ghost.login.weibo; 19 | 20 | import java.awt.BorderLayout; 21 | import java.awt.Container; 22 | import java.awt.Dimension; 23 | import java.awt.Graphics; 24 | import java.awt.Toolkit; 25 | import java.awt.event.ActionEvent; 26 | import java.awt.event.ActionListener; 27 | import java.awt.event.KeyAdapter; 28 | import java.awt.event.KeyEvent; 29 | import java.awt.image.BufferedImage; 30 | import java.io.File; 31 | import java.io.InputStream; 32 | import java.net.HttpURLConnection; 33 | import java.util.Set; 34 | 35 | import javax.imageio.ImageIO; 36 | import javax.swing.ImageIcon; 37 | import javax.swing.JButton; 38 | import javax.swing.JFrame; 39 | import javax.swing.JPanel; 40 | import javax.swing.JTextField; 41 | 42 | import org.apache.commons.lang3.StringUtils; 43 | import org.apache.http.HttpEntity; 44 | import org.apache.http.client.config.RequestConfig; 45 | import org.apache.http.client.methods.CloseableHttpResponse; 46 | import org.apache.http.client.methods.HttpGet; 47 | import org.apache.http.impl.client.CloseableHttpClient; 48 | import org.apache.http.impl.client.HttpClients; 49 | import org.openqa.selenium.Cookie; 50 | import org.openqa.selenium.WebElement; 51 | import org.openqa.selenium.htmlunit.HtmlUnitDriver; 52 | import org.slf4j.Logger; 53 | import org.slf4j.LoggerFactory; 54 | 55 | import zz.pseas.ghost.utils.LoginUtils; 56 | 57 | /** 58 | * @date 2016年6月24日 下午12:35:32 59 | * @version 60 | * @since JDK 1.8 61 | */ 62 | public class SinaWeiboLogin { 63 | 64 | /** 65 | * @param args 66 | * @since JDK 1.8 67 | */ 68 | 69 | public static final Logger LOG = LoggerFactory.getLogger(SinaWeiboLogin.class); 70 | 71 | public static String getSinaCookie(String username, String password) throws Exception { 72 | 73 | StringBuilder sb = new StringBuilder(); 74 | HtmlUnitDriver driver = new HtmlUnitDriver(true); 75 | // driver.setSocksProxy("127.0.0.1", 1080); 76 | // driver.setProxy("127.0.0.1", 1080); 77 | // HtmlOption htmlOption=new Ht 78 | // WebDriver driver = new FirefoxDriver(); 79 | driver.setJavascriptEnabled(true); 80 | // user agent switcher// 81 | String loginAddress = "http://login.weibo.cn/login/"; 82 | driver.get(loginAddress); 83 | WebElement ele = driver.findElementByCssSelector("img"); 84 | String src = ele.getAttribute("src"); 85 | String cookie = concatCookie(driver); 86 | 87 | CloseableHttpClient httpclient = HttpClients.createDefault(); 88 | RequestConfig requestConfig = RequestConfig.custom() 89 | .setSocketTimeout(1000) 90 | .setConnectTimeout(1000) 91 | .setCookieSpec(cookie) 92 | .build(); 93 | 94 | HttpGet httpget = new HttpGet(src); 95 | httpget.setConfig(requestConfig); 96 | CloseableHttpResponse response = httpclient.execute(httpget); 97 | 98 | HttpEntity entity; 99 | String result=null; 100 | try { 101 | 102 | if(response.getStatusLine().getStatusCode()!= HttpURLConnection.HTTP_OK){ 103 | // try again// 104 | } 105 | entity = response.getEntity(); 106 | 107 | if (entity != null) { 108 | InputStream instream = entity.getContent(); 109 | try { 110 | // do something useful 111 | InputStream inputStream = entity.getContent(); 112 | BufferedImage img = ImageIO.read(inputStream); 113 | // deal with the weibo captcha // 114 | String picName = LoginUtils.getCurrentTime("yyyyMMdd-hhmmss"); 115 | // 新建captcha目录// 116 | LoginUtils.getCaptchaDir(); 117 | picName = "./captcha/captcha-" + picName + ".png"; 118 | ImageIO.write(img, "png", new File(picName)); 119 | String userInput = new CaptchaFrame(img).getUserInput(); 120 | WebElement mobile = driver.findElementByCssSelector("input[name=mobile]"); 121 | mobile.sendKeys(username); 122 | WebElement pass = driver.findElementByCssSelector("input[name^=password]"); 123 | pass.sendKeys(password); 124 | WebElement code = driver.findElementByCssSelector("input[name=code]"); 125 | code.sendKeys(userInput); 126 | WebElement rem = driver.findElementByCssSelector("input[name=remember]"); 127 | rem.click(); 128 | WebElement submit = driver.findElementByCssSelector("input[name=submit]"); 129 | // 错误捕获// 130 | submit.click(); 131 | result = concatCookie(driver); 132 | driver.close(); 133 | } finally { 134 | instream.close(); 135 | } 136 | } 137 | } finally { 138 | response.close(); 139 | } 140 | 141 | if (result.contains("gsid_CTandWM")) { 142 | return result; 143 | } else { 144 | // throw new Exception("weibo login failed"); 145 | return null; 146 | } 147 | } 148 | 149 | public static String retryGetSinaCookie(String username, String password) { 150 | String cookie = ""; 151 | // 152 | try { 153 | do { 154 | cookie = ""; 155 | cookie = getSinaCookie(username, password); 156 | // 没有得到正确的cookie则间隔10秒钟继续响应服务器// 157 | if (!StringUtils.isEmpty(cookie)) { 158 | break; 159 | } else { 160 | LOG.error("response server failed,will sleep 10 seconds,then try again..."); 161 | LoginUtils.sleep(10 * 1000); 162 | } 163 | } while (true); 164 | 165 | } catch (Exception e) { 166 | // TODO Auto-generated catch block 167 | e.printStackTrace(); 168 | } 169 | return cookie; 170 | 171 | } 172 | 173 | public static String concatCookie(HtmlUnitDriver driver) { 174 | Set cookieSet = driver.manage().getCookies(); 175 | StringBuilder sb = new StringBuilder(); 176 | for (Cookie cookie : cookieSet) { 177 | sb.append(cookie.getName() + "=" + cookie.getValue() + ";"); 178 | } 179 | String result = sb.toString(); 180 | return result; 181 | } 182 | 183 | public static class CaptchaFrame { 184 | 185 | JFrame frame; 186 | JPanel panel; 187 | JTextField input; 188 | int inputWidth = 100; 189 | BufferedImage img; 190 | String userInput = null; 191 | 192 | public CaptchaFrame(BufferedImage img) { 193 | this.img = img; 194 | } 195 | 196 | public String getUserInput() { 197 | frame = new JFrame("Captcha Input"); 198 | final int imgWidth = img.getWidth(); 199 | final int imgHeight = img.getHeight(); 200 | int width = imgWidth * 2 + inputWidth * 2; 201 | int height = imgHeight * 4; 202 | Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 203 | int startx = (dim.width - width) / 2; 204 | int starty = (dim.height - height) / 2; 205 | frame.setBounds(startx, starty, width, height); 206 | frame.setTitle("新浪微博验证码输入框"); 207 | // String iconPath = "./icon/weibo-4.png"; 208 | String iconPath = "./icon/weibo-1.jpg"; 209 | ImageIcon testImg = new ImageIcon(new String(iconPath)); 210 | frame.setIconImage(testImg.getImage()); 211 | // 得到一个Toolkit对象 212 | // Toolkit tool = frame.getToolkit(); 213 | // 由tool获取图像 214 | // Image myimage = tool.getImage(iconPath); 215 | // frame.setIconImage(myimage); 216 | Container container = frame.getContentPane(); 217 | container.setLayout(new BorderLayout()); 218 | panel = new JPanel() { 219 | @Override 220 | public void paintComponent(Graphics g) { 221 | super.paintComponent(g); 222 | g.drawImage(img, 0, 0, imgWidth * 2, imgHeight * 2, null); 223 | } 224 | }; 225 | panel.setLayout(null); 226 | container.add(panel); 227 | input = new JTextField(6); 228 | input.setBounds(imgWidth * 2, 0, inputWidth, imgHeight * 2); 229 | panel.add(input); 230 | JButton btn = new JButton("确定"); 231 | btn.addActionListener(new ActionListener() { 232 | public void actionPerformed(ActionEvent e) { 233 | userInput = input.getText().trim(); 234 | synchronized (CaptchaFrame.this) { 235 | CaptchaFrame.this.notify(); 236 | } 237 | } 238 | }); 239 | input.addKeyListener(new KeyAdapter() { 240 | // 输入内容若为回车键则结束,捕获该事件// 241 | @Override 242 | public void keyPressed(KeyEvent e) { 243 | if (e.getKeyCode() == KeyEvent.VK_ENTER) { 244 | userInput = input.getText().trim(); 245 | synchronized (CaptchaFrame.this) { 246 | CaptchaFrame.this.notify(); 247 | } 248 | } 249 | } 250 | }); 251 | 252 | // ============================ 253 | btn.setBounds(imgWidth * 2 + inputWidth, 0, inputWidth, imgHeight * 2); 254 | panel.add(btn); 255 | frame.setVisible(true); 256 | synchronized (this) { 257 | try { 258 | this.wait(); 259 | } catch (InterruptedException ex) { 260 | ex.printStackTrace(); 261 | } 262 | } 263 | frame.dispose(); 264 | return userInput; 265 | } 266 | } 267 | } 268 | -------------------------------------------------------------------------------- /ghost-login/src/main/java/zz/pseas/ghost/utils/HttpClinetUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | * 18 | */ 19 | package zz.pseas.ghost.utils; 20 | 21 | /** 22 | * @date 2016年9月14日 下午9:26:00 23 | * @version 24 | * @since JDK 1.8 25 | */ 26 | import java.io.IOException; 27 | import java.net.SocketTimeoutException; 28 | import java.net.UnknownHostException; 29 | import java.util.Map; 30 | 31 | import org.apache.commons.httpclient.Cookie; 32 | import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; 33 | import org.apache.commons.httpclient.Header; 34 | import org.apache.commons.httpclient.HttpClient; 35 | import org.apache.commons.httpclient.HttpException; 36 | import org.apache.commons.httpclient.HttpStatus; 37 | import org.apache.commons.httpclient.NameValuePair; 38 | import org.apache.commons.httpclient.cookie.CookiePolicy; 39 | import org.apache.commons.httpclient.methods.GetMethod; 40 | import org.apache.commons.httpclient.methods.PostMethod; 41 | import org.apache.commons.httpclient.params.HttpClientParams; 42 | import org.apache.commons.httpclient.params.HttpMethodParams; 43 | import org.apache.log4j.Logger; 44 | 45 | /** 46 | * Http连接工具类 47 | * 48 | * @author GS 49 | */ 50 | public class HttpClinetUtil { 51 | private static Logger LOG = Logger.getLogger(HttpClinetUtil.class); 52 | private static final HttpClient hc; 53 | static { 54 | hc = new HttpClient(); 55 | hc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); 56 | HttpClientParams httparams = new HttpClientParams(); 57 | hc.setParams(httparams); 58 | hc.getParams() 59 | .setParameter( 60 | HttpMethodParams.USER_AGENT, 61 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");// 设置信息 62 | } 63 | 64 | /** 65 | * @author GS 66 | * @param url 67 | * @param para 68 | * get请求中携带的参数 69 | * @return 70 | * @throws IOException 71 | */ 72 | public static String get(String url, Map para) 73 | throws IOException { 74 | String responseBody = null; 75 | GetMethod getMethod = new GetMethod(url); 76 | getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 77 | new DefaultHttpMethodRetryHandler()); // 使用系统提供的默认的恢复策略 78 | NameValuePair[] data = new NameValuePair[para.size()]; 79 | int index = 0; 80 | for (String s : para.keySet()) { 81 | data[index++] = new NameValuePair(s, para.get(s)); // 获取请求参数 82 | } 83 | getMethod.setQueryString(data); // 设置请求参数 84 | try { 85 | int statusCode = hc.executeMethod(getMethod); 86 | if (statusCode != HttpStatus.SC_OK) { 87 | LOG.error("Method failed: " + getMethod.getStatusLine()); 88 | } 89 | responseBody = getMethod.getResponseBodyAsString(); 90 | } catch (SocketTimeoutException e) { 91 | e.printStackTrace(); 92 | LOG.error("发生网络超时异常,可能是网络连接有问题" + e.getMessage()); 93 | } catch (UnknownHostException e) { 94 | e.printStackTrace(); 95 | LOG.error("发生致命的异常,UnknownHostException,可能是网络连接有问题" 96 | + e.getMessage()); 97 | } catch (HttpException e) { // 发生致命的异常 98 | e.printStackTrace(); 99 | LOG.error("发生致命的异常" + e.getMessage()); 100 | } catch (IOException e) { // 发生网络异常 101 | e.printStackTrace(); 102 | LOG.error("发生网络异常" + e.getMessage()); 103 | } finally { 104 | getMethod.releaseConnection(); // 释放链接 105 | } 106 | return responseBody; 107 | } 108 | 109 | /** 110 | * @author GS 111 | * @param url 112 | * @return 113 | * @throws IOException 114 | */ 115 | public static String get(String url) throws IOException { 116 | String responseBody = null; 117 | GetMethod getMethod = new GetMethod(url); 118 | getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 119 | new DefaultHttpMethodRetryHandler()); // 使用系统提供的默认的恢复策略 120 | try { 121 | int statusCode = hc.executeMethod(getMethod); 122 | if (statusCode != HttpStatus.SC_OK) { 123 | LOG.error("Method failed: " + getMethod.getStatusLine()); 124 | } 125 | responseBody = getMethod.getResponseBodyAsString(); 126 | } catch (SocketTimeoutException e) { 127 | e.printStackTrace(); 128 | LOG.error("发生网络超时异常,可能是网络连接有问题" + e.getMessage()); 129 | } catch (UnknownHostException e) { 130 | e.printStackTrace(); 131 | LOG.error("发生致命的异常,可能是网络连接有问题" + e.getMessage()); 132 | } catch (HttpException e) { // 发生致命的异常 133 | e.printStackTrace(); 134 | LOG.error("发生致命的异常" + e.getMessage()); 135 | } catch (IOException e) { // 发生网络异常 136 | e.printStackTrace(); 137 | LOG.error("发生网络异常" + e.getMessage()); 138 | } finally { 139 | getMethod.releaseConnection(); // 释放链接 140 | } 141 | return responseBody; 142 | } 143 | 144 | /** 145 | * @author GS 146 | * @param url 147 | * @param para 148 | * Post请求中携带的参数 149 | * @return 150 | * @throws IOException 151 | */ 152 | public static String post(String url, Map para) 153 | throws IOException { 154 | String responseBody = null; 155 | PostMethod postMethod = new PostMethod(url); 156 | NameValuePair[] data = new NameValuePair[para.size()]; 157 | int index = 0; 158 | for (String s : para.keySet()) { 159 | data[index++] = new NameValuePair(s, para.get(s)); 160 | } 161 | postMethod.setRequestBody(data); // 设置请求参数 162 | try { 163 | int statusCode = hc.executeMethod(postMethod); 164 | if (statusCode != HttpStatus.SC_OK) { 165 | LOG.error("Method failed: " + postMethod.getStatusLine()); 166 | } 167 | responseBody = postMethod.getResponseBodyAsString(); 168 | } catch (UnknownHostException e) { 169 | e.printStackTrace(); 170 | LOG.error("发生致命的异常,可能是网络连接有问题" + e.getMessage()); 171 | } catch (HttpException e) { 172 | e.printStackTrace(); 173 | LOG.error(e.getMessage()); 174 | } catch (IOException e) { 175 | e.printStackTrace(); 176 | LOG.error(e.getMessage()); 177 | } finally { 178 | postMethod.releaseConnection(); // 释放链接 179 | } 180 | return responseBody; 181 | } 182 | 183 | /** 184 | * @author GS 185 | * @param url 186 | * @param para 187 | * @param cookie 188 | * @return 189 | * @throws IOException 190 | */ 191 | public static String get(String url, Map para, String cookie) 192 | throws IOException { 193 | String responseBody = null; 194 | GetMethod getMethod = new GetMethod(url); 195 | getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 196 | new DefaultHttpMethodRetryHandler()); // 使用系统提供的默认的恢复策略 197 | NameValuePair[] data = new NameValuePair[para.size()]; 198 | int index = 0; 199 | for (String s : para.keySet()) { 200 | data[index++] = new NameValuePair(s, para.get(s)); // 获取请求参数 201 | } 202 | getMethod.setQueryString(data); // 设置请求参数 203 | if (!cookie.equals("")) { 204 | getMethod.setRequestHeader("cookie", cookie); 205 | } 206 | try { 207 | int statusCode = hc.executeMethod(getMethod); 208 | if (statusCode != HttpStatus.SC_OK) { 209 | LOG.error("Method failed: " + getMethod.getStatusLine()); 210 | } 211 | responseBody = getMethod.getResponseBodyAsString(); 212 | } catch (UnknownHostException e) { 213 | e.printStackTrace(); 214 | LOG.error("发生致命的异常,可能是网络连接有问题" + e.getMessage()); 215 | } catch (HttpException e) { // 发生致命的异常 216 | e.printStackTrace(); 217 | LOG.error("发生致命的异常" + e.getMessage()); 218 | } catch (IOException e) { // 发生网络异常 219 | e.printStackTrace(); 220 | LOG.error("发生网络异常" + e.getMessage()); 221 | } finally { 222 | getMethod.releaseConnection(); // 释放链接 223 | } 224 | return responseBody; 225 | } 226 | 227 | /** 228 | * @author GS 229 | * @param url 230 | * @param para 231 | * @param cookie 232 | * @return 233 | * @throws IOException 234 | */ 235 | public static String get(String url, String cookie) throws IOException { 236 | String responseBody = null; 237 | GetMethod getMethod = new GetMethod(url); 238 | getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 239 | new DefaultHttpMethodRetryHandler()); // 使用系统提供的默认的恢复策略 240 | if (!cookie.equals("")) { 241 | getMethod.setRequestHeader("cookie", cookie); 242 | } 243 | try { 244 | int statusCode = hc.executeMethod(getMethod); 245 | if (statusCode != HttpStatus.SC_OK) { 246 | LOG.error("Method failed: " + getMethod.getStatusLine()); 247 | } 248 | responseBody = getMethod.getResponseBodyAsString(); 249 | } catch (UnknownHostException e) { 250 | e.printStackTrace(); 251 | LOG.error("发生致命的异常,可能是网络连接有问题" + e.getMessage()); 252 | } catch (HttpException e) { // 发生致命的异常 253 | e.printStackTrace(); 254 | LOG.error("发生致命的异常" + e.getMessage()); 255 | } catch (IOException e) { // 发生网络异常 256 | e.printStackTrace(); 257 | LOG.error("发生网络异常" + e.getMessage()); 258 | } finally { 259 | getMethod.releaseConnection(); // 释放链接 260 | } 261 | return responseBody; 262 | } 263 | 264 | /** 265 | * @author GS 266 | * @param url 267 | * @param headers 268 | * @return 269 | * @throws IOException 270 | */ 271 | public static String get(String url, Header[] headers) throws IOException { 272 | String responseBody = null; 273 | GetMethod getMethod = new GetMethod(url); 274 | getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 275 | new DefaultHttpMethodRetryHandler()); // 使用系统提供的默认的恢复策略 276 | for (Header h : headers) { 277 | getMethod.addRequestHeader(h); 278 | } 279 | try { 280 | int statusCode = hc.executeMethod(getMethod); 281 | if (statusCode != HttpStatus.SC_OK) { 282 | LOG.error("Method failed: " + getMethod.getStatusLine()); 283 | } 284 | responseBody = getMethod.getResponseBodyAsString(); 285 | } catch (SocketTimeoutException e) { 286 | e.printStackTrace(); 287 | LOG.error("发生网络超时异常,可能是网络连接有问题" + e.getMessage()); 288 | } catch (UnknownHostException e) { 289 | e.printStackTrace(); 290 | LOG.error("发生致命的异常,可能是网络连接有问题" + e.getMessage()); 291 | } catch (HttpException e) { // 发生致命的异常 292 | e.printStackTrace(); 293 | LOG.error("发生致命的异常" + e.getMessage()); 294 | } catch (IOException e) { // 发生网络异常 295 | e.printStackTrace(); 296 | LOG.error("发生网络异常" + e.getMessage()); 297 | } finally { 298 | getMethod.releaseConnection(); // 释放链接 299 | } 300 | return responseBody; 301 | } 302 | 303 | /** 304 | * @author GS 305 | * @param url 306 | * @param para 307 | * @param cookie 308 | * @return 309 | * @throws IOException 310 | */ 311 | public static String post(String url, Map para, 312 | String cookie) throws IOException { 313 | String responseBody = null; 314 | PostMethod postMethod = new PostMethod(url); 315 | NameValuePair[] data = new NameValuePair[para.size()]; 316 | int index = 0; 317 | for (String s : para.keySet()) { 318 | data[index++] = new NameValuePair(s, para.get(s)); 319 | } 320 | postMethod.setRequestBody(data); // 设置请求参数 321 | if (!cookie.equals("")) { 322 | postMethod.setRequestHeader("cookie", cookie); 323 | } 324 | try { 325 | int statusCode = hc.executeMethod(postMethod); 326 | if (statusCode != HttpStatus.SC_OK) { 327 | LOG.error("Method failed: " + postMethod.getStatusLine()); 328 | } 329 | responseBody = postMethod.getResponseBodyAsString(); 330 | } catch (SocketTimeoutException e) { 331 | e.printStackTrace(); 332 | LOG.error("发生网络超时异常,可能是网络连接有问题" + e.getMessage()); 333 | } catch (UnknownHostException e) { 334 | e.printStackTrace(); 335 | LOG.error("发生致命的异常,可能是网络连接有问题" + e.getMessage()); 336 | } catch (HttpException e) { 337 | e.printStackTrace(); 338 | LOG.error(e.getMessage()); 339 | } catch (IOException e) { 340 | e.printStackTrace(); 341 | LOG.error(e.getMessage()); 342 | } finally { 343 | postMethod.releaseConnection(); // 释放链接 344 | } 345 | return responseBody; 346 | } 347 | 348 | public static String getCookie() { 349 | Cookie[] cookies = hc.getState().getCookies(); 350 | String tmpcookies = ""; 351 | for (Cookie c : cookies) { 352 | tmpcookies += c.toString() + ";"; 353 | } 354 | return tmpcookies; 355 | } 356 | 357 | public void clearCookies() { 358 | hc.getState().clearCookies(); 359 | } 360 | 361 | /** 362 | * 返回值为HTTP状态码 363 | * 364 | * @author GS 365 | * @param url 366 | * @param para 367 | * @param cookie 368 | * @return 369 | * @throws IOException 370 | */ 371 | public static int getStatusCode(String url, String cookie) 372 | throws IOException { 373 | GetMethod getMethod = new GetMethod(url); 374 | getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 375 | new DefaultHttpMethodRetryHandler()); // 使用系统提供的默认的恢复策略 376 | if (!cookie.equals("")) { 377 | getMethod.setRequestHeader("cookie", cookie); 378 | } 379 | int statusCode = 0; 380 | try { 381 | statusCode = hc.executeMethod(getMethod); 382 | if (statusCode != HttpStatus.SC_OK) { 383 | LOG.error("Method failed: " + getMethod.getStatusLine()); 384 | } 385 | } catch (SocketTimeoutException e) { 386 | e.printStackTrace(); 387 | LOG.error("发生网络超时异常,可能是网络连接有问题" + e.getMessage()); 388 | } catch (UnknownHostException e) { 389 | e.printStackTrace(); 390 | LOG.error("发生致命的异常,可能是网络连接有问题" + e.getMessage()); 391 | } catch (HttpException e) { // 发生致命的异常 392 | e.printStackTrace(); 393 | LOG.error("发生致命的异常" + e.getMessage()); 394 | } catch (IOException e) { // 发生网络异常 395 | e.printStackTrace(); 396 | LOG.error("发生网络异常" + e.getMessage()); 397 | } finally { 398 | getMethod.releaseConnection(); // 释放链接 399 | } 400 | return statusCode; 401 | } 402 | } --------------------------------------------------------------------------------