zoukankan      html  css  js  c++  java
  • Java登陆测试

     1 package test001;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 import java.io.InputStream;
     7 import java.io.InputStreamReader;
     8 import java.io.OutputStreamWriter;
     9 import java.net.HttpURLConnection;
    10 import java.net.URL;
    11 
    12 import org.apache.commons.io.IOUtils;
    13 
    14 public class testhttpx {
    15     public static void testPost01() throws IOException { 
    16         String surl = "http://login.goodjobs.cn/index.php/action/UserLogin";  
    17         /** 
    18          * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using 
    19          * java.net.URL and //java.net.URLConnection 
    20          */  
    21         URL url = new URL(surl);  
    22         HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
    23           
    24         /** 
    25          * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。 
    26          * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做: 
    27          */  
    28         connection.setDoOutput(true);  
    29         /** 
    30          * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ... 
    31          */  
    32         OutputStreamWriter out = new OutputStreamWriter(connection  
    33                 .getOutputStream(), "GBK");  
    34                       //其中的memberName和password也是阅读html代码得知的,即为表单中对应的参数名称  
    35         out.write("memberName=myMemberName&password=myPassword"); // post的关键所在!  
    36         // remember to clean up  
    37         out.flush();  
    38         out.close();  
    39           
    40         // 取得cookie,相当于记录了身份,供下次访问时使用  
    41         String cookieVal = connection.getHeaderField("Set-Cookie");  
    42         String s = "http://user.goodjobs.cn/dispatcher.php/module/Resume/action/Preview";  
    43         //重新打开一个连接  
    44                 url = new URL(s);  
    45         HttpURLConnection resumeConnection = (HttpURLConnection) url  
    46           .openConnection();  
    47         if (cookieVal != null) {  
    48                         //发送cookie信息上去,以表明自己的身份,否则会被认为没有权限  
    49         resumeConnection.setRequestProperty("Cookie", cookieVal);  
    50         }  
    51         resumeConnection.connect();  
    52         InputStream urlStream = resumeConnection.getInputStream();  
    53         BufferedReader bufferedReader = new BufferedReader(  
    54           new InputStreamReader(urlStream));  
    55         String ss = null;  
    56         String total = "";  
    57         while ((ss = bufferedReader.readLine()) != null) {  
    58         total += ss;  
    59         }  
    60         IOUtils.write(total, new FileOutputStream("d:/index.html"));  
    61         bufferedReader.close();          
    62     }
    63       
    64     public static void main(String[] args) throws IOException {   
    65   
    66         testPost01();   
    67   
    68     }   
    69 }
     1 package test001;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 import java.io.InputStream;
     7 import java.io.InputStreamReader;
     8 import java.io.OutputStreamWriter;
     9 import java.net.HttpURLConnection;
    10 import java.net.URL;
    11 
    12 import org.apache.commons.io.IOUtils;
    13 
    14 public class testhttps {
    15     public static void testPost02() throws IOException { 
    16         String surl = "http://www.dekevin.com/wp-login.php";  
    17         /** 
    18          * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using 
    19          * java.net.URL and //java.net.URLConnection 
    20          */  
    21         URL url = new URL(surl);  
    22         HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
    23           
    24         /** 
    25          * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。 
    26          * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做: 
    27          */  
    28         connection.setDoOutput(true);  
    29         /** 
    30          * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ... 
    31          */  
    32         OutputStreamWriter out = new OutputStreamWriter(connection  
    33                 .getOutputStream(), "GBK");  
    34                       //其中的memberName和password也是阅读html代码得知的,即为表单中对应的参数名称  
    35         out.write("log=dekevin&pwd=luowende103807"); // post的关键所在!  
    36         // remember to clean up  
    37         out.flush();  
    38         out.close();  
    39           
    40         // 取得cookie,相当于记录了身份,供下次访问时使用  
    41         String cookieVal = connection.getHeaderField("Set-Cookie");  
    42         String s = "http://www.dekevin.com/wp-admin/";  
    43         //重新打开一个连接  
    44                 url = new URL(s);  
    45         HttpURLConnection resumeConnection = (HttpURLConnection) url  
    46           .openConnection();  
    47         if (cookieVal != null) {  
    48                         //发送cookie信息上去,以表明自己的身份,否则会被认为没有权限  
    49         resumeConnection.setRequestProperty("Cookie", cookieVal);  
    50         }  
    51         resumeConnection.connect();  
    52         InputStream urlStream = resumeConnection.getInputStream();  
    53         BufferedReader bufferedReader = new BufferedReader(  
    54           new InputStreamReader(urlStream));  
    55         String ss = null;  
    56         String total = "";  
    57         while ((ss = bufferedReader.readLine()) != null) {  
    58         total += ss;  
    59         }  
    60         IOUtils.write(total, new FileOutputStream("d:/index.html"));  
    61         bufferedReader.close();          
    62     }
    63       
    64     public static void main(String[] args) throws IOException {   
    65   
    66         testPost02();   
    67   
    68     }   
    69 
    70 }
  • 相关阅读:
    网络IO之阻塞、非阻塞、同步、异步总结
    C语言栈与调用惯例
    多个文件目录下Makefile的写法
    利用 mount 指令解决 Read-only file system的问题
    error while loading shared libraries: xxx.so.x" 错误的原因和解决办法
    Centos6.4下安装protobuf及简单使用
    Centos下修改启动项和网络配置
    Centos下配置单元测试工具gtest
    Centos配置为驱动程序开发环境
    Centos安装gcc及g++
  • 原文地址:https://www.cnblogs.com/dekevin/p/3581413.html
Copyright © 2011-2022 走看看