Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.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和存储cookie登录并下载网页_Java_Login_Download_Httpurlconnection_Moodle - Fatal编程技术网

使用java和存储cookie登录并下载网页

使用java和存储cookie登录并下载网页,java,login,download,httpurlconnection,moodle,Java,Login,Download,Httpurlconnection,Moodle,我登录学校moodle网页下载源代码时遇到问题, 到目前为止,我能够收到它从未真正登录过的登录页面, 任何帮助都将不胜感激。我已经被这个问题困扰了好几个星期了。 下面的代码不是我自己的,而是我在web上找到的多个示例的修改版本 import java.net.*; import java.io.*; import java.util.*; public class LoginByHttpPost { private static final String POST_CONTENT_TY

我登录学校moodle网页下载源代码时遇到问题, 到目前为止,我能够收到它从未真正登录过的登录页面, 任何帮助都将不胜感激。我已经被这个问题困扰了好几个星期了。 下面的代码不是我自己的,而是我在web上找到的多个示例的修改版本

import java.net.*;
import java.io.*;
import java.util.*;

public class LoginByHttpPost
{
    private static final String POST_CONTENT_TYPE = "application/x-www-form-urlencoded";     
    private static final String LOGIN_USER_NAME = "myusername";
    private static final String LOGIN_PASSWORD = "mypassword";
    private static final String LOGIN_DOMAIN = "students.ltu.edu.au";

    private static final String TARGET_URL = "https://www.latrobe.edu.au/lms/login/";
    private String page ="";

    public static void main (String args[])
    {
        LoginByHttpPost httpUrlBasicAuthentication = new LoginByHttpPost();
        httpUrlBasicAuthentication.httpPostLogin();
    }

    public void httpPostLogin ()
    {
        try
        {
            String urlEncodedContent = preparePostContent(LOGIN_USER_NAME, LOGIN_PASSWORD, LOGIN_DOMAIN);

            HttpURLConnection urlConnection = doHttpPost(TARGET_URL, urlEncodedContent);

            page = readResponse(urlConnection);

            System.out.println("Successfully made the HTPP POST.");
            System.out.println("Recevied response is: '/n" + page + "'");

        }
        catch(IOException ioException)
        {
            System.out.println("Problems encounterd.");
        }
    }


    private String preparePostContent(String loginUserName, String loginPassword, String loginDomain) throws UnsupportedEncodingException
    {
        String encodedLoginUserName = URLEncoder.encode(loginUserName, "UTF-8");
        String encodedLoginPassword = URLEncoder.encode(loginPassword, "UTF-8");
        String encodedLoginDomain = URLEncoder.encode(loginDomain, "UTF-8");

        String content = URLEncoder.encode("username=", "UTF-8") + encodedLoginUserName
             + URLEncoder.encode("&password=", "UTF-8") + encodedLoginPassword
             + URLEncoder.encode("&domain=", "UTF-8") + encodedLoginDomain
             + URLEncoder.encode("&Login=", "UTF-8") + URLEncoder.encode("Login", "UTF-8");

        return content;

    }


    public HttpURLConnection doHttpPost(String targetUrl, String content) throws IOException
    {
        DataOutputStream dataOutputStream = null;
        HttpURLConnection conn = null; 
        String cookieFirst = null;
        String cookieValue = null;
        String totalCookie = "";
        try
        {
            CookieManager manager = new CookieManager();
            manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
            CookieHandler.setDefault(manager);


            URL url = new URL(targetUrl);
            conn = (HttpURLConnection)url.openConnection();

            conn.getContent();

            CookieStore cookiejar = manager.getCookieStore();
            List<HttpCookie> cookiesList = cookiejar.getCookies();
            for(HttpCookie cookiel: cookiesList)
            {
               totalCookie += cookiel+"; ";
            }
            totalCookie = totalCookie.substring(0, totalCookie.length()-1);
            System.out.println("Total Cookie: " + totalCookie);

        }
        catch(Exception e)
        {
           System.out.println("Something went wrong");
        }

         HttpURLConnection urlConnection = null;

      try{
            URL url = new URL(targetUrl);
            urlConnection = (HttpURLConnection)url.openConnection();
            urlConnection.setDoInput(true);

            urlConnection.setDoOutput(true);

            urlConnection.setUseCaches(true);

            urlConnection.setRequestProperty("Content-Type", POST_CONTENT_TYPE);

            urlConnection.setRequestProperty("Content-Length", Integer.toString(content.length()));

            urlConnection.setInstanceFollowRedirects(true);

            urlConnection.setRequestMethod("POST");

            urlConnection.setRequestProperty("Cookie", totalCookie);

            urlConnection.connect();


            dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());

            dataOutputStream.writeBytes(content);
            dataOutputStream.flush();
            dataOutputStream.close();

        }
        catch(IOException ioException)
        {
            System.out.println("I/O problems while trying to do a HTTP post.");
            ioException.printStackTrace();

            if (dataOutputStream != null)
            {
                try
                {
                    dataOutputStream.close();
                }
                catch(Throwable ignore)
                {
                }
            }
            if (urlConnection != null)
            {
                urlConnection.disconnect();
            }

            throw ioException;
        }

            return urlConnection;

    }


    private String readResponse(HttpURLConnection urlConnection) throws IOException
    {

        BufferedReader bufferedReader = null;
        try
        {
            bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String responeLine;

            StringBuilder response = new StringBuilder();

            while ((responeLine = bufferedReader.readLine()) != null)
            {
                response.append(responeLine + "\n");
            }

            return response.toString();
        }
        catch(IOException ioException)
        {
            System.out.println("Problems while reading the response");
            ioException.printStackTrace();
            throw ioException;

        }
        finally
        {
            if (bufferedReader != null)
            {
                try
                {
                    bufferedReader.close();
                }
                catch(Throwable ignore)
                {
                }
            }

        }
    }
}
import java.net.*;
导入java.io.*;
导入java.util.*;
公共类loginbyhttpost
{
私有静态最终字符串POST\u CONTENT\u TYPE=“application/x-www-form-urlencoded”;
私有静态最终字符串登录\u USER\u NAME=“myusername”;
私有静态最终字符串登录\u PASSWORD=“mypassword”;
私有静态最终字符串LOGIN_DOMAIN=“students.ltu.edu.au”;
私有静态最终字符串TARGET_URL=”https://www.latrobe.edu.au/lms/login/";
私有字符串页=”;
公共静态void main(字符串参数[])
{
LoginByHttpPost httpUrlBasicAuthentication=新的LoginByHttpPost();
httpUrlBasicAuthentication.httpPostLogin();
}
公共无效httpPostLogin()
{
尝试
{
字符串urlEncodedContent=preparePostContent(登录名、登录密码、登录域);
HttpURLConnection-urlConnection=doHttpPost(TARGET_URL,urlEncodedContent);
page=readResponse(urlConnection);
System.out.println(“成功制作了HTPP帖子”);
System.out.println(“收到的响应为:'/n“+第+”);
}
捕获(IOException IOException)
{
System.out.println(“遇到的问题”);
}
}
私有字符串preparePostContent(字符串loginUserName、字符串loginPassword、字符串loginDomain)引发UnsupportedEncodingException
{
字符串encodedLoginUserName=URLEncoder.encode(loginUserName,“UTF-8”);
字符串encodedLoginPassword=URLEncoder.encode(loginPassword,“UTF-8”);
字符串encodedLoginDomain=URLEncoder.encode(loginDomain,“UTF-8”);
字符串内容=urlcoder.encode(“username=,“UTF-8”)+encodedLoginUserName
+urlcoder.encode(“&password=”,“UTF-8”)+encodedLoginPassword
+urlcoder.encode(“&domain=”,“UTF-8”)+encodedLoginDomain
+urlcoder.encode(“&Login=”,“UTF-8”)+urlcoder.encode(“Login”,“UTF-8”);
返回内容;
}
公共HttpURLConnection doHttpPost(字符串targetUrl,字符串内容)引发IOException
{
DataOutputStream DataOutputStream=null;
HttpURLConnection conn=null;
字符串cookieFirst=null;
字符串cookieValue=null;
字符串totalCookie=“”;
尝试
{
CookieManager=新建CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
setDefault(管理器);
URL URL=新URL(targetUrl);
conn=(HttpURLConnection)url.openConnection();
conn.getContent();
CookieStore cookiejar=manager.getCookieStore();
List-cookiesList=cookiejar.getCookies();
用于(HttpCookie Cookie-Cookie:cookiesList)
{
totalCookie+=cookiel+“;”;
}
totalCookie=totalCookie.substring(0,totalCookie.length()-1);
System.out.println(“总Cookie:+totalCookie”);
}
捕获(例外e)
{
System.out.println(“出错”);
}
HttpURLConnection-urlConnection=null;
试一试{
URL URL=新URL(targetUrl);
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(true);
setRequestProperty(“内容类型”,POST_内容类型);
urlConnection.setRequestProperty(“Content-Length”,Integer.toString(Content.Length());
urlConnection.setInstanceFlowRedirects(true);
urlConnection.setRequestMethod(“POST”);
setRequestProperty(“Cookie”,totalCookie);
urlConnection.connect();
dataOutputStream=新的dataOutputStream(urlConnection.getOutputStream());
dataOutputStream.writeBytes(内容);
dataOutputStream.flush();
dataOutputStream.close();
}
捕获(IOException IOException)
{
println(“尝试执行HTTP post时出现I/O问题”);
ioException.printStackTrace();
如果(dataOutputStream!=null)
{
尝试
{
dataOutputStream.close();
}
捕获(可丢弃忽略)
{
}
}
if(urlConnection!=null)
{
urlConnection.disconnect();
}
抛出ioException;
}
返回URL连接;
}
私有字符串读取响应(HttpURLConnection-urlConnection)引发IOException
{
BufferedReader BufferedReader=null;
尝试
{
bufferedReader=新的bufferedReader(新的InputStreamReader(urlConnection.getInputStream());
字符串响应线;
StringBuilder响应=新建StringBuilder();
而((responeLine=bufferedReader.readLine())!=null)
{
response.append(responeLine+“\n”);
}
返回response.toString();
}
捕获(IOException IOException)
{
System.out.println(“读取响应时出现问题”);
ioException.printStackTrace();
抛出ioException;
}
最后
{
if(bufferedReader!=null)
{
尝试
{
bufferedReader.close()