有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用HttpUrlConnection的Java类引发IOException,无法读取或写入

我构建了这个类,它使用HttpUrlConnection处理登录到站点的操作 和HttpsUrlConnection:

import java.io.*;
import java.net.*;
import javax.net.ssl.*;

public class HttpLogin
{
    public String text;

    String host;
    int port;
    String uri;
    String user_p;
    String pass_p;
    String username;
    HttpURLConnection connection = null;
    HttpsURLConnection secure_connection = null;
    URL url;

    String method = "GET";


    public HttpLogin(String host, int port, String uri, String user_p, String pass_p, String username, String http_method)
    {
        this.host = host;
        this.port = port;
        this.uri = uri;
        this.user_p = user_p;
        this.pass_p = pass_p;
        this.username = username;
        this.method = http_method; 
        try{
            this.url  = new URL(host);        
        }
        catch(IOException e)
        {
            this.url = null;
        }
    }

    public void sendlogin(String password) throws IOException
    {
        if(this.host.startsWith("https://") == true)
        {   try{
                do{
                  this.secure_connection = getSecureUrlConnection();
                  this.secure_connection.connect();
                 }while(this.secure_connection == null);    

                sendSecureRequest(password);
                this.text = getSecureResponse();                    
            }
            catch(IOException e)
            {
                this.secure_connection.disconnect();
                return;
            }
        }
        else if(this.host.startsWith("http://") == true)
        {   
            try{
                do{
                    this.connection = getUrlConnection();
                    this.connection.connect();
               }while(this.connection == null);

               sendRequest(password);  
               this.text = getResponse();
            }
            catch(IOException e)
            {
                this.connection.disconnect();
                return;
            }
        }
    }

    private HttpURLConnection getUrlConnection()
    {
        try
        {
            return (HttpURLConnection) url.openConnection();
        }
        catch(NullPointerException | IOException e)
        {
            System.out.println("Cannot resolve hostname: " + host);
            return null;
        }
    }

    private HttpsURLConnection getSecureUrlConnection()
    {
        try
        {
            return (HttpsURLConnection) url.openConnection();
        }
        catch(NullPointerException | IOException e)
        {
            System.out.println("Cannot resolve hostname: " + host);
            return null;
        }
    }

    private String getResponse()
    {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(this.connection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String temp;
            while((temp = br.readLine()) != null)
            {
                sb.append(temp);
            }
            br.close();
            return sb.toString();
        }
        catch(IOException e)
        {
            System.out.println("Error in reading");
            connection.disconnect();
            return null;                
        }
    }

    private String getSecureResponse()
    {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(this.secure_connection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String temp;
            while((temp = br.readLine()) != null)
            {
                sb.append(temp);
            }
            br.close();
            return sb.toString();
        }
        catch(IOException e)
        {
            System.out.println("Error in reading");
            this.secure_connection.disconnect();
            return null;                
        }
    }

    private void sendSecureRequest(String password) throws IOException
    {
        String body = user_p+"="+username+"&"+pass_p+"="+password;
        this.connection.setDoOutput(true);
        this.connection.setRequestMethod(this.method);
        this.connection.setInstanceFollowRedirects(true);
        this.connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        this.connection.setRequestProperty("Content-Lenght", Integer.toString(body.length()));
        OutputStreamWriter sw = new OutputStreamWriter(this.connection.getOutputStream());
        sw.write(body);
        sw.close();
    }

    private void sendRequest(String password) throws IOException
    {
        String body = user_p+"="+username+"&"+pass_p+"="+password;
        this.secure_connection.setDoOutput(true);
        this.secure_connection.setRequestMethod(this.method);
        this.secure_connection.setInstanceFollowRedirects(true);
        this.secure_connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        this.secure_connection.setRequestProperty("Content-Lenght", Integer.toString(body.length()));
        OutputStreamWriter sw = new OutputStreamWriter(this.secure_connection.getOutputStream());
        sw.write(body);
        sw.close();
    }

    public Boolean isLogin()
    {   
        if(this.connection != null)
        {
            String header = this.connection.getHeaderField("location");
            try{
                if(header.equals(this.uri) == false)
                {
                    return true;
                }
            }
            catch(NullPointerException e)
            {
                return false;
            }  
        } 
        else if(this.secure_connection != null)
        {
            String header = this.secure_connection.getHeaderField("location");
            try{
                if(header.equals(this.uri) == false)
                {
                    return true;
                }
            }
            catch(NullPointerException e)
            {
                return false;
            }  
        } 
       return false;
    }
}

但是当我测试它时,它会给我IOException,异常是由一些方法处理的,这些方法会关闭连接

如果我尝试使用这个类并执行登录,它会打印“读取错误”

有什么问题吗?我不能读也不能写这个连接

编辑:

这是抛出的异常的堆栈跟踪:

java.io.IOException: Server returned HTTP response code: 403 for URL: https://example.com
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
        at HttpLogin.getSecureResponse(HttpLogin.java:122)
        at HttpLogin.sendlogin(HttpLogin.java:49)
        at Test.main(Test.java:13)

我将stacktrace中的url编辑为https://example.com

编辑2:

我更改了登录的参数,现在不会抛出异常,isLogin()函数返回true。但反应从来都不是红色的。变量文本为空


共 (0) 个答案