我需要添加什么来发送用户名和密码?

时间:2017-04-27 17:00:56

标签: java httpurlconnection httpsurlconnection

我正在尝试使用Java和库HttpURLConnection将登录信息(用户名,密码)发送到网站。我正处于程序的这一点....我成功链接到页面,填写表格,但从未尝试过“组合”...有人知道我做错了什么?

Ty:)

这是我的方法

public void SecureConnection() 
{

    try {
        URL url = new URL(this.URL); 
        HttpsURLConnection hConnection = (HttpsURLConnection)url.openConnection();
        HttpsURLConnection.setFollowRedirects(true); 
        hConnection.setDoInput(true);
        hConnection.setRequestProperty("User-Agent", USER_AGENT);
    hConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

            hConnection.setDoOutput(true); 
            hConnection.setRequestMethod("POST");
            PrintStream ps = new PrintStream( hConnection.getOutputStream() );  
            ps.print(this.txtUser_name + "=" + this.Username + "&" + this.txtPassword_name + "=" + this.Password);
            ps.close(); 
            hConnection.connect();
            System.out.println(hConnection.getResponseCode());

            if( HttpsURLConnection.HTTP_OK == hConnection.getResponseCode() ) {

                System.out.println("it works");

                InputStream is = hConnection.getInputStream(); 
                OutputStream os = new FileOutputStream("output.html"); 
                int data; 
                while((data=is.read()) != -1) { 
                    os.write(data); 
                } 
                is.close(); 
                os.close(); 
                hConnection.disconnect(); 
            }
    }

0 个答案:

没有答案