Dancingsky 2019-10-31 16:17 采纳率: 0%
浏览 386

JAVA POST登录异常问题?

网上找的HTTP登录代码,登录后返回能200,但是不用用户名密码也返回200,且用Cookie[] cookies = httpClient.getState().getCookies();
StringBuffer tmpcookies = new StringBuffer();
for (Cookie c : cookies) {
tmpcookies.append(c.toString() + ";");
System.out.println("cookies = "+c.toString());
}
能取到Cookies,这个搞不懂
登录成功后跳到另一个网页,但是用System.out.println("ResponseBody:\n" + stringBuffer.toString());返回如下:

<html>
<head><title></title>
<SCRIPT LANGUAGE="JavaScript">
var currentWindow = window;
if(currentWindow != null){
currentWindow.top.location = "/NMMP/jsp/login.jsp";
}
</SCRIPT>
</head>

感觉跳转时少了东西,所以跳转后的网页不对,请各们大神帮分析下,或者给个思路,本人不是搞程序的,只是想做一个来应对自己的重复工作,完整代码如下:本人无C币,如果有兴趣帮我做可商量

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
/**
 *
    * @ClassName: HttpLogin
    * @Description:  java通过httpclient获取cookie模拟登录
    * @author zeze
    * @date 2015年11月10日 下午4:18:08
    *
 */

public class HttpLogin {

    public static void main(String[] args) {
        // 登陆 Url
        String loginUrl = "http://10.154.13.82:7011/NMMP/jsp/login.jsp";
        // 需登陆后访问的 Url
        String dataUrl = "http://10.154.13.82:7011/NMMP/wyMainAction!init.ilf";
        //http://10.154.13.82:7011/NMMP//jsp/wyService/mytasklist.jsp
        HttpClient httpClient = new HttpClient();

        // 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
        PostMethod postMethod = new PostMethod(loginUrl);

        // 设置登陆时要求的信息,用户名和密码
        NameValuePair[] data = { new NameValuePair("useraccount", ""), new NameValuePair("password", "Hn_yd45") };
        postMethod.setRequestBody(data);
        try {
            // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
            httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
            int statusCode=httpClient.executeMethod(postMethod);

            // 获得登陆后的 Cookie
            Cookie[] cookies = httpClient.getState().getCookies();
            StringBuffer tmpcookies = new StringBuffer();
            for (Cookie c : cookies) {
                tmpcookies.append(c.toString() + ";");
                System.out.println("cookies = "+c.toString());
            }
            System.out.println(statusCode);
            if(statusCode==200){//重定向到新的URL
                System.out.println("模拟登录成功");
                // 进行登陆后的操作
                GetMethod getMethod = new GetMethod(dataUrl);
                // 每次访问需授权的网址时需带上前面的 cookie 作为通行证
               // getMethod.setRequestHeader("cookie", tmpcookies.toString());
                // 你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
                // 例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外
               // postMethod.setRequestHeader("Referer", "http://passport.mop.com/");

                getMethod.setRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
                getMethod.setRequestHeader("Referer","http://10.154.13.82:7011/NMMP/login.ilf");
                getMethod.setRequestHeader("Accept-Encoding","gzip, deflate");
                getMethod.setRequestHeader("Accept-Language","zh-CN,zh;q=0.9");
                getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"    );
                httpClient.executeMethod(getMethod);
                statusCode=httpClient.executeMethod(getMethod);
                System.out.println("模拟登录成功"+statusCode);
                // 打印出返回数据,检验一下是否成功
               // String text = getMethod.getResponseBodyAsString();

                InputStream inputStream = getMethod.getResponseBodyAsStream();   
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));   
                StringBuffer stringBuffer = new StringBuffer();   
                String str= "";   
                while((str = br.readLine()) != null){   
                    stringBuffer .append(str );   
                }   
           System.out.println("ResponseBody:\n" + stringBuffer.toString());

           HttpRequestProxy hrp = new HttpRequestProxy();
           Map date = new HashMap();
           date.put("jyidApplet", "1");
           date.put("codeNumApplet", "1");
           date.put("jymxIdApplet", "447");
           date.put("patientIdApplet", "1118");
           String str1 = hrp.doRequest(dataUrl,date, null, null);
           System.out.println("sd"+str1);


            }
            else {
                System.out.println("登录失败");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

登录网页源码如下:

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>综合管理系统</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<style type="text/css">
*{
    margin:auto;
}
html,body{
    overflow:hidden;
    width:100%;
    height:100%;
    margin:auto;
}
body{
    background:url(/NMMP/common-skins/maintain/images/login_bg_1.0.gif);
}
#login_panel{
    background-image:url(/NMMP/common-skins/maintain/images/login_panel_bg_1.0.png);
    background-repeat:repeat-x;
    height:640px;
    width:100%;
    text-align:center;
    position:relative;
}
#login_input{
    background-image:url(/NMMP/common-skins/maintain/images/login_input_1.0.png);
    background-repeat:no-repeat;
    width:668px;
    height:403px;
    position:relative;
}
#useraccount{
    border:none;
    position:absolute;
    left:392px;
    top:149px;
    height:24px;
    width:136px;
    background-color:#99deff;
}
#password{
    border:none;
    position:absolute;
    left:392px;
    top:183px;
    height:24px;
    width:136px;
    background-color:#99deff;
}
#login_botton{
    position:absolute;
    left:537px;
    _left:538px;
    top:147px;
}
#message{
    position:absolute;
    left:348px;
    top:218px;
    color:#F00;
    font-size:14px;
    font-weight:bold;
}
#technical{
    position:absolute;
    left:375px;
    top:258px;
    text-align:left;
    color:#CCC;
    font-size:12px;
    line-height:30px;
}
#copyright{
    position:absolute;
    bottom:65px;
    right:0px;
    color:#CCC;
    font-size:12px;
    text-align:right;
    line-height:20px;
}
</style>
<!--[if IE 6]>
<script language="javascript"
        type="text/javascript"
        src="/NMMP/common-skins/default/jslib/DD_belatedPNG.js">
</script>
<script language="javascript" type="text/javascript">
    DD_belatedPNG.fix('#login_panel');
    DD_belatedPNG.fix('#login_input');
</script>
<![endif]-->


</head>

<body windowc_onresizez="true">
    <div id="login_panel">
        <div id="login_input" style="top: 110.5px;">
        <form name="form1" action="/NMMP/CtrlUser.action?action=login&amp;surl=jsp/home/main.jsp&amp;furl=jsp/login.jsp" method="post" 360chrome_form_autofill="2">
            <input name="useraccount" id="useraccount" style="background-color: rgb(250, 255, 189);" required="true" type="text">
            <input name="password" id="password" style="background-color: rgb(250, 255, 189);" required="true" type="password">
            <input id="login_botton" type="image" src="/NMMP/common-skins/maintain/images/login_botton.png">
            <div id="message">

            </div>
            <!-- div id="technical"><a style="color:orange" href="javascript:void(0)" onclick="resetpwd();">密码重置</a></div-->
            <div id="copyright">版权所有:</div>
        </form>
        </div>
    </div>

<script language="javascript" type="text/javascript">
    document.getElementById("login_input").style.top=(document.body.clientHeight-403)/2+"px";

    function resetpwd(){
        var url = "/NMMP/resetpwdAction.ilf";
        window.location.href=url;
    }
</script>
</body></html>
  • 写回答

2条回答 默认 最新

  • zqbnqsdsmd 2019-10-31 23:49
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题