<<上篇 | 首页 | 下篇>>

使用java的HttpURLConnection模拟登陆百度

使用java的HttpURLConnection模拟登陆百度

public static void loginBaidu()
{
        URL url = null;
        HttpURLConnection httpurlconnection = null;
        try {
            url = new URL(http://www.baidu.com/);
                        httpurlconnection = (HttpURLConnection) url.openConnection();
            httpurlconnection.setRequestProperty("User-Agent", "Internet Explorer");
            httpurlconnection.setRequestProperty("Host", www.baidu.com);
            httpurlconnection.connect();
            String cookie0 = httpurlconnection.getHeaderField("Set-Cookie");
            httpurlconnection.disconnect();
            //String cookie0 = "BAIDUID=8AF5EA24DBF1275CE15C02B5FF65A265:FG=1;
BDSTAT=61a1d3a7118ce8a7ce1b9d16fdfaaf51f3deb48f8e5494eef01f3a292cf5b899; BDUSE=deleted";
            url = new URL(http://passport.baidu.com/?login);
            String strPost = "username=xxxxxx&password=xxxxxx&mem_pass=on";
            httpurlconnection = (HttpURLConnection) url.openConnection();
            httpurlconnection.setFollowRedirects(true);
            httpurlconnection.setInstanceFollowRedirects(true);
            httpurlconnection.setDoOutput(true); // 需要向服务器写数据
            httpurlconnection.setDoInput(true); // 
            httpurlconnection.setUseCaches(false); // 获得服务器最新的信息
            httpurlconnection.setAllowUserInteraction(false);
            httpurlconnection.setRequestMethod("POST");
            httpurlconnection
                    .addRequestProperty(
                            "Accept",
                            "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, */*");
            httpurlconnection.setRequestProperty("Referer", 
                            "http://passport.baidu.com/?login&tpl=mn&u=http%3A//www.baidu.com/");             httpurlconnection.setRequestProperty("Accept-Language", "zh-cn"); 
            httpurlconnection.setRequestProperty("Content-Type",                     "application/x-www-form-urlencoded");             httpurlconnection.setRequestProperty("Accept-Encoding",
                    "gzip, deflate");
            httpurlconnection
                    .setRequestProperty(
                            "User-Agent",
                            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727;
MEGAUPLOAD 1.0)"); 
            httpurlconnection.setRequestProperty("Host", "passport.baidu.com");
            httpurlconnection.setRequestProperty("Content-Length", strPost
                    .length()
                    + "");
            httpurlconnection.setRequestProperty("Connection", "Keep-Alive");
            httpurlconnection.setRequestProperty("Cache-Control", "no-cache");
            httpurlconnection.setRequestProperty("Cookie", cookie0);
            httpurlconnection.getOutputStream().write(strPost.getBytes());
            httpurlconnection.getOutputStream().flush();
            httpurlconnection.getOutputStream().close();
            httpurlconnection.connect();
            int code = httpurlconnection.getResponseCode();
            System.out.println("code   " + code);
            String cookie1 = httpurlconnection.getHeaderField("Set-Cookie");
            System.out.print(cookie0 + "; " + cookie1);
            httpurlconnection.disconnect();

 

 

 

                       url = new URL(http://www.baidu.com/);
                        httpurlconnection = (HttpURLConnection) url.openConnection();
            httpurlconnection.setRequestProperty("User-Agent", "Internet Explorer");
            httpurlconnection.setRequestProperty("Host", www.baidu.com);
            httpurlconnection.setRequestProperty("Cookie", cookie0 + "; " + cookie1);
            httpurlconnection.connect();
            InputStream urlStream = httpurlconnection.getInputStream();
            BufferedInputStream buff = new BufferedInputStream(urlStream);
            Reader r = new InputStreamReader(buff, "gbk");
            BufferedReader br = new BufferedReader(r);
            StringBuffer strHtml = new StringBuffer("");
            String strLine = null;
            while ((strLine = br.readLine()) != null)
{
                strHtml.append(strLine + "\r\n");
            }
            System.out.print(strHtml.toString());
        }
catch (Exception e)
{
            e.printStackTrace();
        }
finally {
            if (httpurlconnection != null)
                httpurlconnection.disconnect();
        }
    }

标签 :

Java程序员应该掌握的10个JSP标签库

Java程序员应该掌握的10个JSP标签库

One of JSP’s most overlooked features is its ability to work with reusable code blocks, aka “tag libraries,” to add new functionality to a JSP application. These tag libraries, declared using XML-type markup, provide ready-to-use widgets that can easily be integrated into a JSP page. They’re also a useful way to separate an application’s business logic from its visual presentation.

阅读全文……

标签 : ,

我收藏的链接(45)

我收藏的链接(45)
使用分层的 Selenium 框架进行复杂 Web 应用的自动测试
Selenium 是一种 Web 应用的自动测试工具,通过模拟用户对 Web 页面的各种操作,可以精确重现软件测试人员编写的 Test Cases 步骤。Selenium 包含三个工具:Selenium-IDE,Selenium-RC 以及 Selenium-Core。其中,Selenium-Core 是驱动 Selenium 工作的核心部分,作为一个用 JavaScript 编写的测试引擎,它可以操作 Web 页面上的各种元素,诸如:点击按钮、输入文本框,以及断言 Web 页面上存在某些文本与 Web 元素等
Selenium web application testing system
Selenium IDE is a Firefox add-on that records clicks, typing, and other actions to make a test, which you can play back in the browser.
The Apache Cassandra Project
The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.
Data integration issues challenge Cal power operation's move to SOA
数据集成问题挑战加州电力,采用CIM公共信息模型、基于SOA SOAP的web service服务实现多个系统的互操作集成
转:Chrome的一些实用脚本 | 谷奥——探寻谷歌的奥秘
chrome扩展,浏览器扩展,
当你输入一个网址的时候,实际会发生什么
一个HTTP请求/响应的流程
搜狗细胞词库_词库下载_词典_输入法字典
下载输入法词库、关键词库TXT链接
http://pinyin.sogou.com/dict/download_txt.php?id=
词库下载-拼音加加输入法
拼音加加输入法词库,关键词库
搜狗实验室资料下载 - 互联网词库
搜狗输入法词库,关键词库
ANTLR Parser Generator
ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages. ANTLR provides excellent support for tree construction, tree walking, translation, error recovery, and error reporting. There are currently about 5,000 ANTLR source downloads a month.
[官方攻略]双屏显示实现方法详解
[官方攻略]双屏显示实现方法详解
An introduction to backup, restore, and rollforward recovery in IBM ...
Derby在线离线备份恢复
SQL 语句性能调优
SQL 语句性能调优
Java - Auto Reloading of Configuration File | twit88.com
This is a follow up to my previous article, XML Configuration for Java Program. In the article I uses Apache Commons Configuration for my application configuration, and I also provided a singleton wrapper class for it.
Java Tip 125: Set your timer for dynamic properties - JavaWorld
Property files are widely used to control application configurations. One great advantage of property files: they let you change your application's configuration without recompilation. However, you most likely need to restart your application for the new configuration to take effect. This might not be a constraint for client applications, where users stop and start the application. In fact, some GUI (graphical user inferface) programs even let you change properties through the user interface, without restarting the program.
Property文件Reload方法(不用重起服务器) - 当当的量子篮 - JavaEye技术网站
Property文件Reload方法
NIO: High Performance File Copying
NIO: High Performance File Copying
Web service performance tips
Email summarizing best practices for Promoting Scalable Web Services
Caching SOAP services
Quality of service for web services
Scaling SOAP-based web services.
Scaling web services
Webservices SOAP communications overheads
AppFuse 2.0研究(一) - 下一道彩虹 - CSDN博客
AppFuse代码生成,maven
AppFuse 2.0初体验 - 下一道彩虹 - CSDN博客
AppFuse 2.0 快速上手,maven
Derby Reference Manual
Derby 10.5 Reference Manual
Derby 10.5 参考手册,SQL语法
Fiddler Web Debugger - A free web debugging tool
Fiddler is a Web Debugging Proxy which logs all HTTP(S) traffic between your computer and the Internet. Fiddler allows you to inspect all HTTP(S) traffic, set breakpoints, and "fiddle" with incoming or outgoing data. Fiddler includes a powerful event-based scripting subsystem, and can be extended using any .NET language.
Java的DOM编辑:创建一个新的DOM解析树 : DOM编辑 « 可扩展标记语言 « Java
Java的DOM编辑:创建一个新的DOM解析树
你所不知道的电力真相_网易新闻
近来,“电煤荒”、“拉闸限电”充斥了所有媒体的头条。这一切,被归因于一场突如其来的暴雪。但事实上,“电荒”一直是中国经济每年的固定节目。除了自然气候之外,中国电力的体制性问题究竟在电荒中扮演了什么样的角色?电究竟经过了哪些环节才最终到消费者这里,这其中有哪些企业参与,有多高的成本,会怎么影响到我们的生活?网易新闻另一面从发电、配送电,到最终的销售环节,为您揭开电力行业的真相。

阅读全文……

标签 :