首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

模拟登陆兑现不了!求指教!100分哦

2013-07-04 
模拟登陆实现不了!!!求指教!100分哦!本帖最后由 xyz 于 2013-06-30 22:08:12 编辑import java.awt.image.B

模拟登陆实现不了!!!求指教!100分哦!
本帖最后由 xyz 于 2013-06-30 22:08:12 编辑
import java.awt.image.BufferedImage; 
import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.Scanner; 
import javax.imageio.ImageIO; 
import javax.net.ssl.HttpsURLConnection;//这句貌似没有用到!
public class wangrongfeng 


    public static void Login() throws Exception
    { 
        
            // 下载验证码到本地 
            URL url = new URL("http://5pi.cn/code.php"); 
            HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
            connection.setUseCaches(false); 
            connection.setDoOutput(true); 
            connection.setDoInput(true); 
            connection.setConnectTimeout(5000); 
            connection.setReadTimeout(10000); 
            ((HttpURLConnection) connection).setRequestMethod("GET"); 
            connection.setRequestProperty("User-Agent", 
                    "Mozilla/5.0 (compatible; MSIE 8.0; Windows 7)"); 
            connection.connect(); 

            String cookies = connection.getHeaderField("Set-Cookie"); 
            if (cookies!= null) 
            { 
                cookies = cookies.substring(0, cookies.indexOf(";")); 
            } 




            File imgCodeFile = new File("/Users/mac/Pictures" 
                    + System.currentTimeMillis() + ".gif"); 
            BufferedImage image = ImageIO.read(connection.getInputStream()); 
            ImageIO.write(image, "gif", imgCodeFile); 

            String checkCode = GetCheckCode(); 
            String nextUrl = "http://5pi.cn/task_list.php?taskfrom=all&order=point"; 
            nextUrl = URLEncoder.encode(nextUrl, "GBK"); 

            //用户要输入值 
            String userName = "mememomo"; 
            String password = "wrfl1346"; 
            String chkcode = checkCode;
            //生成地址https://login.passport.9you.com/checkCode?id=SSO_PAY&s=5384a672b08ac3e96ad534ac67e30442&userIp=60.191.73.11&userName=zhao88zhao8&password=458458&identifyingCode=5pre
            String paramStr = "?"+ "&userName=" + userName + "&password=" + password + "&chkcode=" + chkcode+"login=1"; 
            String loginUrl = "http://5pi.cn/member/checklogin.php" + paramStr; 
            System.out.println("请求地址:" + loginUrl); 
            //根据 
            url = new URL("http://5pi.cn/member/checklogin.php"); 
            connection = (HttpURLConnection) url.openConnection(); 
            connection.setUseCaches(false); 
            connection.setDoOutput(true); 


            connection.setDoInput(true); 
            connection.setConnectTimeout(20000); 
            connection.setReadTimeout(20000); 
            connection.setRequestMethod("GET"); 
            connection.setRequestProperty("Content-Type", 
                    "application/x-www-form-urlencoded"); 
            connection.setRequestProperty("Content-Length", String.valueOf(paramStr.length())); 
            connection.setRequestProperty("Cookie", cookies); 
            connection.setRequestProperty("User-Agent", 
                    "Mozilla/5.0 (compatible; MSIE 8.0; Windows 7)"); 
            connection.connect(); 
            DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); 
            dos.writeBytes(paramStr); 
            dos.flush(); 
            dos.close(); 
            int res = connection.getResponseCode(); 

            if (res == 200) 
            { 
                BufferedReader in = new BufferedReader(new InputStreamReader( 
                        connection.getInputStream(), "GBK")); 
                String retVal; 
                while ((retVal = in.readLine()) != null) 
                { 


                    System.out.println(retVal); 
                } 
            } 

     

    } 

    private static String GetCheckCode() 
    { 
        Scanner sc = new Scanner(System.in); 
        System.out.print("验证码在C:\\ImageCode目录下 ,请你查看并输入:"); 
        String inputCode = sc.next(); 
        System.out.println("您输入的验证码是:" + inputCode); 
        return inputCode; 
    } 

    public static void main(String[] args)throws Exception 
    { 
       wangrongfeng.Login(); 
    } 
}  

模拟登陆兑现不了!求指教!100分哦


我的做法就是模拟登陆5pi.cn这个网站,但是我用wireshark抓包的时候抓包抓到的是模拟登陆兑现不了!求指教!100分哦
我想知道,是不是系统本身设定的是post请求,是不是我用了get就不对了?
还有我的get的请求参数是不是不对?尤其是我只添加了密码和帐号验证码以及后面的那个抓到的标志!其余的还需要添加什么么?
抱歉,我是新手,第一次常识做这个,求大神解救哦!!!!!!!! java 验证码 模拟登陆 URL 异常
[解决办法]
应该是不能用get的,开源项目httpclient比较好用

post地址:http://5pi.cn/member/checklogin.php
参数:
username:mememomo
password:******
chkcode:5354
login:1
[解决办法]
给你一个模拟登录qq空间的例子:

登录方法:


        public static String login(final String qq,final String password){


                HashMap<String, String> params=new HashMap<String, String>();
                params.put("login_url", "http://pt.3g.qq.com/s?aid=nLogin");
                params.put("sidtype", "1");
                params.put("loginTitle", "手机腾讯网");
                params.put("bid", "0");
                params.put("qq", qq);
                params.put("pwd", password);
                params.put("loginType", "1");
                try {
                        String response=WebUtils.doPost(QQ_LOGIN_URL, params, 0, 0);
                        int sidIndex=response.indexOf("sid");
                        SID=response.substring(sidIndex+4, sidIndex+28);
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return SID;
        }



WebUtils类的doPost方法:
        
public static String doPost(String url, Map<String, String> params,int connectTimeout,int readTimeout) throws IOException {
                String query=buildQuery(params, DEFAULT_CHARSET);
                byte[] content=null;


                if(query!=null){
                        content=query.getBytes(DEFAULT_CHARSET);
                }
                return doPost(url, CTYPE, content, connectTimeout, readTimeout);
        }




        public static String doPost(String url, String ctype, byte[] content,int connectTimeout,int readTimeout) throws IOException {
                HttpURLConnection conn = null;
                OutputStream out = null;
                String rsp = null;
                try {
                        try{
                                conn = getConnection(new URL(url), METHOD_POST, ctype); 
                                conn.setConnectTimeout(connectTimeout);
                                conn.setReadTimeout(readTimeout);
                                out = conn.getOutputStream();
                                out.write(content);
                                rsp = getResponseAsString(conn);


                        }catch(IOException e){
                                throw e;
                        }
                }finally {
                        if (out != null) {
                                out.close();
                        }
                        if (conn != null) {
                                conn.disconnect();
                        }
                }
                return rsp;
        }




        private static HttpURLConnection getConnection(URL url, String action, String ctype)
                        throws IOException {
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod(action);
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setRequestProperty("Accept", "text/xml,text/javascript,text/html");


                conn.setRequestProperty("User-Agent", USER_AGENT);
                conn.setRequestProperty("Content-Type", ctype);
                return conn;
        }




        private static String getStreamAsString(InputStream stream, String charset) throws IOException {
                try {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
                        StringWriter writer = new StringWriter();
                        char[] chars = new char[256];
                        int count = 0;
                        while ((count = reader.read(chars)) > 0) {
                                writer.write(chars, 0, count);
                        }
                        return writer.toString();
                } finally {
                        if (stream != null) {
                                stream.close();
                        }


                }
        }


[解决办法]
返回信息是验证码出错,说明楼主get方式提交时成功的,只是验证码输入错了
看了下paramStr的拼凑
 String paramStr = "?"+ "&userName=" + userName + "&password=" + password + "&chkcode=" + chkcode+"&login=1"; //验证码和login参数直接少了个&导致你验证码变成了你输入的1386login=1了所以返回了验证码出错。楼主把这个&符号加上再试试吧

[解决办法]
应该不是post 的问题,数据没有获取到应该是参数问题。

热点排行