使用Java登录网站

时间:2014-06-20 12:54:35

标签: java html web

首先是的,我知道有一百万个问题要求同样的事情,但他们的答案都没有起作用。我使用以下代码:

    URL objtest = new URL("http://rccpdems01/ems/Login.php");
    HttpURLConnection con = (HttpURLConnection) objtest.openConnection();
    con.setRequestMethod("POST"); 
    String datas = "userid=G434326&psword=TestPass";
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(datas);
    wr.flush();
    wr.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close(); 

然而,返回的行看起来似乎没有在HTML表单上发生,如果我传递了错误的输入细节,则不会显示错误消息,如果它是正确的,那么下一个或"主页& #34;不返回HTML代码。

为什么?我是否还需要激活按钮?如果是这样,我在这里如何做按钮细节:

type="submit" name="btnLogin" value="Login"

由于 乌尔里希


它返回的是以下html代码:

<html>
<style type="text/css"> 
.m0  {
font-weight: bold;
font-family: Arial;
color: black;
background-color: black;
font-size: x-small;;
  }
.m1  {
font-weight: bold;
font-family: Arial;
color: white;
background-color: red;
font-size: x-small;
letter-spacing: normal;
word-spacing: normal;
width: 100%;
border-top-style: double;
border-right-style: double;
border-bottom-style: double;
border-left-style: double;
  }

我没有发布所有内容,但你明白了。


嗨,好的,我发现我需要做的答案也是定义了按下登录按钮:

userid=G4737326&psword=UwwiTest&btnLogin=Login

现在我只需要弄清楚如何将我得到的结果发布回浏览器。

1 个答案:

答案 0 :(得分:0)

发布您所看到的内容会有所帮助。如果没有更多信息,我无法告诉您代码有什么问题,但我建议您尝试Apache HTTP Client。根据您的问题,您可能会发现基于表单的登录Example很有帮助。

编辑:

听起来你正试图模拟按下按钮。如果是这种情况,则无需从页面中删除HTML。您需要弄清楚按下按钮产生的URL,并以编程方式提交。然后,您可能需要读取标头,并确定设置了哪个登录cookie。 Apache HTTP Client已经为您做到了这一点。