当前位置:编程学习 > JAVA >>

模拟126邮箱自动登录

我需要java模拟鼠标自动选择下拉菜单技术,想采用httpclient那个技术,通过126邮箱测试一下 System.out.println(response.getStatusLine())返回来的是HTTP/1.1 405 Method Not Allowed,全部java代码如下:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public  class ChooseList 

public static void main(String[]args) throws IOException
{
new ChooseList().printText();

}
 private static String userName = "kankanchengbucheng";  
   private static String password = "kk123456";  
   private static String redirectURL = "http://cwebmail.mail.126.com/js5/main.jsp?sid=EAOBcKPiiHwGeQJuTRiigrKxSZYedPrb#module=welcome.WelcomeModule%7C%7B%7D";
       private static String loginURL = "http://mail.126.com/errorpage/err_126.htm?errorType=Login_Timeout";  
       private HttpResponse response;  
   private DefaultHttpClient httpclient = new DefaultHttpClient();  


   private boolean login()
   {  
       HttpPost httpost = new HttpPost(loginURL);  
     

       List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
       nvps.add(new BasicNameValuePair("origURL", redirectURL));  
       nvps.add(new BasicNameValuePair("user", userName));  
       nvps.add(new BasicNameValuePair("password", password));  
       nvps.add(new BasicNameValuePair("style", "网易邮箱5.0简约版"));//简约3.0网易邮箱5.0简约版
       try 
       {  
           httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 
          
           response = httpclient.execute(httpost);
           response.getStatusLine();
           System.out.println(response.getStatusLine());
       }
       catch (Exception e) 
       {  
           e.printStackTrace();  
           return false;  
       } 
       finally 
       {  
           httpost.abort();  
       }  
       return true;  
   }  
   
   private String getRedirectLocation()
   {  
      
   org.apache.http.Header locationHeader = response.getFirstHeader("Location");
       if (locationHeader == null)
       {  System.out.println("null");
           return null;  
       }  
       return locationHeader.getValue();  
   }  


   private String getText(String redirectLocation) 
   {  
       HttpGet httpget = new HttpGet(redirectLocation);  
       
       ResponseHandler<String> responseHandler = new BasicResponseHandler();  
       String responseBody = "";  
       try 
       {  
           responseBody = httpclient.execute(httpget, responseHandler);  
       }
       catch (Exception e)
       {  
           e.printStackTrace();  
           responseBody = null;  
       } 
       finally 
       {  
           //httpget.abort();  
          // httpclient.getConnectionManager().shutdown();  
       }  
       return responseBody;  
   }  


   public void printText() throws IOException  
   {  
       if (login()) 
       { 
           String redirectLocation = getRedirectLocation();  
           if (redirectLocation != null)
           {  
               String html=getText(redirectLocation);
               Document doc = Jsoup.parse(html);
               Element body = doc.body();
              
        Elements es =  body.select("title");
    Iterator it = es.iterator();
  String[]s=new String[100];
  int i=0;
     while( it .hasNext() )
  {
  Element e = (Element) it.next();
      s[i]=e.text();
     System.out.println(s[i]);
  i++;
 
  }
  }
           }  
       }  
}

登录网址要选http://mail.126.com/errorpage/err_126.htm?errorType=Login_Timeout,因为只有这个有下拉菜单我就是通过nvps.add(new BasicNameValuePair("style", "网易邮箱5.0简约版"));//简约3.0网易邮箱5.0简约版
来改变value然后进入子网页后获得标签title信息,如果不同说明我的这个方法是可行的,但是一直结果没结果,打印response.getStatusLine()返回来的是HTTP/1.1 405 Method Not Allowed
请大神解惑,急求! java --------------------编程问答-------------------- 这是那两个图
引用 楼主 u011573355 的回复:
我需要java模拟鼠标自动选择下拉菜单技术,想采用httpclient那个技术,通过126邮箱测试一下 System.out.println(response.getStatusLine())返回来的是HTTP/1.1 405 Method Not Allowed,全部java代码如下:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public  class ChooseList 

public static void main(String[]args) throws IOException
{
new ChooseList().printText();

}
 private static String userName = "kankanchengbucheng";  
   private static String password = "kk123456";  
   private static String redirectURL = "http://cwebmail.mail.126.com/js5/main.jsp?sid=EAOBcKPiiHwGeQJuTRiigrKxSZYedPrb#module=welcome.WelcomeModule%7C%7B%7D";
       private static String loginURL = "http://mail.126.com/errorpage/err_126.htm?errorType=Login_Timeout";  
       private HttpResponse response;  
   private DefaultHttpClient httpclient = new DefaultHttpClient();  


   private boolean login()
   {  
       HttpPost httpost = new HttpPost(loginURL);  
     

       List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
       nvps.add(new BasicNameValuePair("origURL", redirectURL));  
       nvps.add(new BasicNameValuePair("user", userName));  
       nvps.add(new BasicNameValuePair("password", password));  
       nvps.add(new BasicNameValuePair("style", "网易邮箱5.0简约版"));//简约3.0网易邮箱5.0简约版
       try 
       {  
           httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 
          
           response = httpclient.execute(httpost);
           response.getStatusLine();
           System.out.println(response.getStatusLine());
       }
       catch (Exception e) 
       {  
           e.printStackTrace();  
           return false;  
       } 
       finally 
       {  
           httpost.abort();  
       }  
       return true;  
   }  
   
   private String getRedirectLocation()
   {  
      
   org.apache.http.Header locationHeader = response.getFirstHeader("Location");
       if (locationHeader == null)
       {  System.out.println("null");
           return null;  
       }  
       return locationHeader.getValue();  
   }  


   private String getText(String redirectLocation) 
   {  
       HttpGet httpget = new HttpGet(redirectLocation);  
       
       ResponseHandler<String> responseHandler = new BasicResponseHandler();  
       String responseBody = "";  
       try 
       {  
           responseBody = httpclient.execute(httpget, responseHandler);  
       }
       catch (Exception e)
       {  
           e.printStackTrace();  
           responseBody = null;  
       } 
       finally 
       {  
           //httpget.abort();  
          // httpclient.getConnectionManager().shutdown();  
       }  
       return responseBody;  
   }  


   public void printText() throws IOException  
   {  
       if (login()) 
       { 
           String redirectLocation = getRedirectLocation();  
           if (redirectLocation != null)
           {  
               String html=getText(redirectLocation);
               Document doc = Jsoup.parse(html);
               Element body = doc.body();
              
        Elements es =  body.select("title");
    Iterator it = es.iterator();
  String[]s=new String[100];
  int i=0;
     while( it .hasNext() )
  {
  Element e = (Element) it.next();
      s[i]=e.text();
     System.out.println(s[i]);
  i++;
 
  }
  }
           }  
       }  
}

登录网址要选http://mail.126.com/errorpage/err_126.htm?errorType=Login_Timeout,因为只有这个有下拉菜单我就是通过nvps.add(new BasicNameValuePair("style", "网易邮箱5.0简约版"));//简约3.0网易邮箱5.0简约版
来改变value然后进入子网页后获得标签title信息,如果不同说明我的这个方法是可行的,但是一直结果没结果,打印response.getStatusLine()返回来的是HTTP/1.1 405 Method Not Allowed
请大神解惑,急求!
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,