您的浏览器发送了一条请求,说明此服务器无法理解HttpsURLConnection

时间:2013-10-23 07:00:40

标签: android ssl https httpsurlconnection

我正在创建一个登录应用程序,以从代码中显示的链接中检索某些信息。我的问题是我收到了“400 Bad request。你的浏览器发送了一个请求,这个服务器无法理解。[...]服务器在idp.tut.fi端口443”在日志文件中。

此外,该链接处理RC4_128与SHA1进行身份验证和RSA进行密钥交换,但实际上并不知道我可以在代码中应用这些协议的位置。

感谢您的帮助!

这是我的代码:

public class POPLogin extends Activity {

private EditText usr, pass;
private Button submitButton;
private CharSequence notify;
private String res, resp;
private final String link = "https://idp.tut.fi/idp/Authn/UserPassword";
private URL url;

public static boolean isNetworkAvailable(Context context) {
    return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo() != null;
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_poplogin);
    usr = ((EditText) findViewById(R.id.usernameField));            
    pass = ((EditText) findViewById(R.id.passwordField));           
    submitButton = (Button) findViewById(R.id.submitButton);        
    submitButton.getBackground().setColorFilter(new LightingColorFilter(0x00FFB90F, 0xFFAA0000));   

    /*
     * This method controls the login button so that the correct
     * information is sent to the server.
     */
    submitButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if(isNetworkAvailable(getApplicationContext())) {   // If there is an Internet connection available

                /*
                * A new thread is created from the main one 
                * to separate the login process (AsyncTask, https operations).
                */
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            url = new URL(link);
                            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

                            // Create the SSL Connection
                            SSLContext sc;
                            sc = SSLContext.getInstance("TLS");
                            sc.init(null, null, new java.security.SecureRandom());
                            conn.setSSLSocketFactory(sc.getSocketFactory());

                            // SSL Authentication
                            String userpass = usr.getText().toString() + ":" + pass.getText().toString();
                            String basicAuth = "Basic " + Base64.encodeToString(userpass.getBytes(), Base64.DEFAULT);
                            conn.setRequestProperty("Authorization", basicAuth);

                            Log.i("NOTIFICATION", "All SSL parameters set");

                            // Set timeout and method
                            conn.setReadTimeout(7000);
                            conn.setConnectTimeout(7000);
                            conn.setRequestMethod("POST");
                            conn.setDoInput(true);          // Flag indicating this connection is used for output (POST)
                            conn.connect();

                            Log.i("NOTIFICATION", "Connection request sent");

                            // Check HTTP Response Code
                            int status = conn.getResponseCode();
                            InputStream is;
                            if (status >= 400 ) {
                                is = conn.getErrorStream();
                            } else {
                                is = conn.getInputStream();
                            }

                            Log.i("NOTIFICATION", "HTTP Code Checked");

                            // Receives the answer
                            resp = null;
                            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                            while((res = rd.readLine()) != null) {
                                resp += res;
                            }
                            Log.i("NOTIFICATION", "Answer received");
                            Log.i("CODE", resp);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            } else {
                notify = "Internet Connection not available";
            }

            if(notify != null) {
                Toast toast = Toast.makeText(getApplicationContext(), notify, Toast.LENGTH_SHORT);
                toast.show();
                notify = null;
            } 
        }
    });
}

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.poplogin, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:2)

如果您收到HTTP错误代码,则可以证明您的HTTPS以及SSL设置工作正常。

问题在于您发送的数据。看起来你需要使用java.net.Authenticator,而不是试图手工完成。