gpt4 book ai didi

android - 您的浏览器发送了该服务器无法理解 HttpsURLConnection 的请求

转载 作者:太空宇宙 更新时间:2023-11-03 13:54:58 24 4
gpt4 key购买 nike

我正在制作一个登录应用程序,以从代码中出现的链接中检索一些信息。我的问题是我在日志文件中收到“400 错误请求。您的浏览器发送了该服务器无法理解的请求。[...] 服务器位于 idp.tut.fi 端口 443”。

此外,该链接使用用于身份验证的 SHA1 和用于 key 交换的 RSA 处理 RC4_128,但我真的不知道我可以在我的代码中的什么地方应用这些协议(protocol)。

感谢您的帮助!

这是我的代码:

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;
}

最佳答案

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

问题出在您发送的数据中。看起来您需要使用 java.net.Authenticator 而不是尝试手动完成。

关于android - 您的浏览器发送了该服务器无法理解 HttpsURLConnection 的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19534921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com