Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用android登录网站_Java_Android_Cookies_Https - Fatal编程技术网

Java 使用android登录网站

Java 使用android登录网站,java,android,cookies,https,Java,Android,Cookies,Https,基本上,我使用firefox扩展来获取帖子,并通过登录我的学校网站获取数据。我想能够登录,但当我尝试它说:“会话已过期,请重新登录!”。这是我目前掌握的代码,有人能告诉我我做错了什么吗?我认为这与重定向有关,因为第一篇文章应该重定向到正确的位置,但我无法让它工作,并且试图获取位置标题会抛出一个空指针 try { super.onCreate(icicle); CookieManager cookieManager = new CookieManag

基本上,我使用firefox扩展来获取帖子,并通过登录我的学校网站获取数据。我想能够登录,但当我尝试它说:“会话已过期,请重新登录!”。这是我目前掌握的代码,有人能告诉我我做错了什么吗?我认为这与重定向有关,因为第一篇文章应该重定向到正确的位置,但我无法让它工作,并且试图获取位置标题会抛出一个空指针

try {
            super.onCreate(icicle);
            CookieManager cookieManager = new CookieManager();
            CookieHandler.setDefault(cookieManager);
            BasicClientCookie netscapeCookie = new BasicClientCookie("UserLoginInfo", "SelectedTab=StudentLogin&RedirectURL=");
            netscapeCookie.setVersion(0);
            netscapeCookie.setDomain("sisk12.hannibal.k12.mo.us");
            netscapeCookie.setPath("/");
            cookieStore.addCookie(netscapeCookie);
            localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
            HttpPost httpost = new HttpPost("https://sisk12.hannibal.k12.mo.us/hb/Default.aspx?__EVENTTARGET=&__EVENTARGUMENT=&txtUserName=xxxx&txtPassword=xxxxxx&btnLogin=Login&txtSelectedTab=StudentLogin");
            HttpResponse response = httpclient.execute(httpost, localContext);
            HttpEntity entity = response.getEntity();
            // readResponse(response.getEntity().getContent());
            //Log.w("****","Login form get: " + response.getFirstHeader("Location").getValue());
            // readResponse(response.getEntity().getContent());
            if (entity != null) {
                entity.consumeContent();
            }
            Header[] headers = response.getHeaders("Location");
            if (headers != null && headers.length != 0) {
                String newUrl = headers[headers.length - 1].getValue();
                // call again with new URL
                HttpGet httpget = new HttpGet(newUrl);
                entity = response.getEntity();
                Log.w("****", "Login form get: " + response.getStatusLine());
                response = httpclient.execute(httpget, localContext);
                readResponse(response.getEntity().getContent());
            } else {
                Log.w("****", "No location header");
            }
            if (cookieStore.getCookies().isEmpty()) {
                Log.w("****", "None");
            } else {
                for (int i = 0; i < cookieStore.getCookies().size(); i++) {
                    Log.w("****", "- " + cookieStore.getCookies().get(i).toString());
                }
            }
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        } catch (IOException ex) {
            Log.e("****", Log.getStackTraceString(ex));
        }

public String readResponse(InputStream input) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(input));
    StringBuilder sb = new StringBuilder("");
    String line = "";
    String NL = System.getProperty("line.separator");

    while ((line = in.readLine()) != null) {
        sb.append(line).append(NL);
    }

    in.close();

    String page = sb.toString();
    Log.i("****", page);

    return page;
}

代码中有一个重定向,应该由post调用,但我不知道apache是否自动处理该重定向,如果不是,如何处理该重定向?

它可能与ASP.NET会话cookie有关

在浏览器中加载登录页面时(
https://sisk12.hannibal.k12.mo.us/hb/Default.aspx
)它设置一个会话cookie,
ASP.NET\u SessionId
。然后在浏览器中单击“登录”按钮时提交该cookie

但是您的代码没有提交这样的cookie。如果你做这样的事情,也许会有用:

  • 加载Default.aspx
  • 从该请求中拉出ASP.NET_会话ID cookie
  • 然后运行原始代码,但也添加会话cookie

  • 这可能与ASP.NET会话cookie有关

    在浏览器中加载登录页面时(
    https://sisk12.hannibal.k12.mo.us/hb/Default.aspx
    )它设置一个会话cookie,
    ASP.NET\u SessionId
    。然后在浏览器中单击“登录”按钮时提交该cookie

    但是您的代码没有提交这样的cookie。如果你做这样的事情,也许会有用:

  • 加载Default.aspx
  • 从该请求中拉出ASP.NET_会话ID cookie
  • 然后运行原始代码,但也添加会话cookie

  • 我不确定,但您的问题可能是由SSL连接引起的。尝试在HttpClient中设置ssl连接

    HttpParams params = new BasicHttpParams(); 
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8"); 
    
    SchemeRegistry sr = new SchemeRegistry(); 
    sr.register(new Scheme(RequestScheme.HTTP.name(), PlainSocketFactory.getSocketFactory(), RequestScheme.HTTP.port)); 
    sr.register(new Scheme(RequestScheme.HTTPS.name(), SSLSocketFactory.getSocketFactory(), RequestScheme.HTTPS.port)); 
    
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, sr); 
    Client client = new Client(manager, params)
    

    我不确定,但您的问题可能是由SSL连接引起的。尝试在HttpClient中设置ssl连接

    HttpParams params = new BasicHttpParams(); 
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8"); 
    
    SchemeRegistry sr = new SchemeRegistry(); 
    sr.register(new Scheme(RequestScheme.HTTP.name(), PlainSocketFactory.getSocketFactory(), RequestScheme.HTTP.port)); 
    sr.register(new Scheme(RequestScheme.HTTPS.name(), SSLSocketFactory.getSocketFactory(), RequestScheme.HTTPS.port)); 
    
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, sr); 
    Client client = new Client(manager, params)
    

    以下是我的建议

  • 除非存在一些JSSE配置问题(最新java版本最不可能出现这种情况),否则我看不到任何问题。HttpClient可以在不进行任何显式配置的情况下处理它。事实上,你正在访问这个网站。因此,SSL不是一个问题
  • 从这个错误来看,它似乎在会话中寻找一些标准的会话cookie,甚至在登录之前
  • 在浏览器中,它可以工作,因为首先,您使用发送会话cookie的GET访问登录页面,然后使用同一页面提交POST请求。Cookie由浏览器管理
  • 在代码中执行相同的操作
  • 首先使用GET请求访问主页/登录页,然后使用相同的实例发布请求。cookies在httClient实例中维护
  • 不要做任何显式的cookie处理(因为您使用的是
    CookieManager
    )。HttpClient能够透明地管理cookie,而无需显式处理。(假设您的httpclient实例类似于浏览器实例,并且您将其用于GET和POST)

  • 我只需查看登录页面,其中有一个名为
    \uuu VIEWSTATE
    的参数,它也与凭据一起发布。这对于确保登录帖子不是除浏览器以外的任何来源似乎至关重要。如果这是强制性的,那么这将使您的工作变得棘手,因为您需要解析登录页面html(您可以使用该html进行解析)获取该参数并在post请求中发送相同的内容

  • 以下是您可以使用的参考代码

    String getURL = "https://sisk12.hannibal.k12.mo.us/hb/";
            String postURL = getURL;
    
            HttpClient client = new HttpClient();
    
            GetMethod getMethod = new GetMethod(getURL);
            int statusCode = client.executeMethod(getMethod);
    
            //String getResponse = getMethod.getResponseBodyAsString();
            //System.out.println(getResponse);
    
            PostMethod postMethod = new PostMethod(postURL);
            postMethod.addParameter("__EVENTARGUMENT", "");
            postMethod.addParameter("__EVENTTARGET", "");
            postMethod.addParameter("__VIEWSTATE", "");//to be parsed from login page
            postMethod.addParameter("btnLogin", "Login");
            postMethod.addParameter("txtPassword", "xxxx");
            postMethod.addParameter("txtSelectedTab", "StudentLogin");
            postMethod.addParameter("txtUserName", "xxxx");
    
            client.executeMethod(postMethod);
    
            String postResponse = postMethod.getResponseBodyAsString();
            System.out.println(postResponse);
    

    如果遇到重定向,您可能需要编写一些附加代码。

    以下是我的建议

  • 除非存在一些JSSE配置问题(最新java版本最不可能出现这种情况),否则我看不到任何问题。HttpClient可以在不进行任何显式配置的情况下处理它。事实上,你正在访问这个网站。因此,SSL不是一个问题
  • 从这个错误来看,它似乎在会话中寻找一些标准的会话cookie,甚至在登录之前
  • 在浏览器中,它可以工作,因为首先,您使用发送会话cookie的GET访问登录页面,然后使用同一页面提交POST请求。Cookie由浏览器管理
  • 在代码中执行相同的操作
  • 首先使用GET请求访问主页/登录页,然后使用相同的实例发布请求。cookies在httClient实例中维护
  • 不要做任何显式的cookie处理(因为您使用的是
    CookieManager
    )。HttpClient能够透明地管理cookie,而无需显式处理。(假设您的httpclient实例类似于浏览器实例,并且您将其用于GET和POST)

  • 我只需查看登录页面,其中有一个名为
    \uuu VIEWSTATE
    的参数,它也与凭据一起发布。这对于确保登录帖子不是除浏览器以外的任何来源似乎至关重要。如果这是强制性的,那么这将使您的工作变得棘手,因为您需要解析登录页面html(您可以使用该html进行解析)获取该参数并在post请求中发送相同的内容

  • 以下是您可以使用的参考代码

    String getURL = "https://sisk12.hannibal.k12.mo.us/hb/";
            String postURL = getURL;
    
            HttpClient client = new HttpClient();
    
            GetMethod getMethod = new GetMethod(getURL);
            int statusCode = client.executeMethod(getMethod);
    
            //String getResponse = getMethod.getResponseBodyAsString();
            //System.out.println(getResponse);
    
            PostMethod postMethod = new PostMethod(postURL);
            postMethod.addParameter("__EVENTARGUMENT", "");
            postMethod.addParameter("__EVENTTARGET", "");
            postMethod.addParameter("__VIEWSTATE", "");//to be parsed from login page
            postMethod.addParameter("btnLogin", "Login");
            postMethod.addParameter("txtPassword", "xxxx");
            postMethod.addParameter("txtSelectedTab", "StudentLogin");
            postMethod.addParameter("txtUserName", "xxxx");
    
            client.executeMethod(postMethod);
    
            String postResponse = postMethod.getResponseBodyAsString();
            System.out.println(postResponse);
    

    如果遇到重定向,您可能需要编写一些附加代码。

    您是否设置了httpclient来处理https请求(不是http)?无论如何,请删除该url中的id和密码。我不确定如果没有用户名和密码,我是否会得到任何帮助,但我现在删除了它们。把他们留在网上让我有点紧张不,除了这里的代码,我什么都没做。我没有意识到我需要做那样的事情。你能举个例子说明怎么做吗?这是我的代码片段。HttpParams params=新的BasicHttpParams();HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);HttpProtocolParams.setContentCharset(参数,“UTF-8”);SchemeRegistry sr=新Sch