Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.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.net登录网站,但不确定如何处理cookie_Java_Cookies - Fatal编程技术网

尝试使用java.net登录网站,但不确定如何处理cookie

尝试使用java.net登录网站,但不确定如何处理cookie,java,cookies,Java,Cookies,我在尝试登录时不断收到“您的会话超时”,我怀疑这是由于根本没有处理cookies造成的。HTTPS会阻止我通过Java登录吗 代码如下: package logintopage; import java.net.*; import java.io.*; public class LoginToPage{ // Variables to hold the URL object and its connection to that URL. private static URL URLObj;

我在尝试登录时不断收到“您的会话超时”,我怀疑这是由于根本没有处理cookies造成的。HTTPS会阻止我通过Java登录吗

代码如下:

package logintopage;

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

public class LoginToPage{

// Variables to hold the URL object and its connection to that URL.
private static URL URLObj;
private static URLConnection connect;

public static void main(String[] args) {
    try {
        // Establish a URL and open a connection to it. Set it to output mode.
        URLObj = new URL("my url");
        connect = URLObj.openConnection();
        connect.setDoOutput(true);  


    }
    catch (MalformedURLException ex) {
        System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
        System.exit(1); 
    }
    catch (Exception ex) {
        System.out.println("An exception occurred. " + ex.getMessage());
        System.exit(1);
    }


    try {

        // Create a buffered writer to the URLConnection's output stream and write our forms parameters.
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));

        writer.write("userIdentifier=user&password=pass&LoginUser=Sign+In");
        writer.close();

        // Now establish a buffered reader to read the URLConnection's input stream.
        BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));

        String lineRead = "";

        // Read all available lines of data from the URL and print them to screen.
        while ((lineRead = reader.readLine()) != null) {
            System.out.println(lineRead);
        }

        reader.close();
    }
    catch (Exception ex) {
        System.out.println("There was an error reading or writing to the URL: " + ex.getMessage());
    }
}

我对Java的网络世界相当陌生,因此非常感谢您的帮助和信息来源。

以上代码仅适用于http连接

如果需要通过https连接到url,则需要使用SSLSocket和SSLSocketFactory类。看看这个基本的例子:或者

<>你也可以考虑简化你的代码:


URLObj=新URL(“我的URL?用户标识符=用户和密码=通过和登录用户=登录”)

完整的stacktrace将帮助我们诊断原因。。。