【发布时间】:2020-10-08 22:27:57
【问题描述】:
我试图将JSON 对象从android 发送到我在NodeJS 中编写的服务器。
服务器正在运行和工作,我可以使用Postman 软件对其进行POST。
但是当我尝试使用 android 发送JSON 时,我得到了SSLProtocolException: SSL handshake aborted。
完整的日志:
javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xd7a38728: Failure in SSL library, usually a protocol error
error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER (third_party/openssl/boringssl/src/ssl/tls_record.cc:242 0xbc4ddce6:0x00000000)
我使用此处的代码尝试发送 http 请求: How To Send json Object to the server from my android app
在我得到SSLProtocolException 之后,我尝试了这个解决方案:
SSLSocket exception handshake error when trying to send to SSLServerSocket
javax.net.ssl.SSLException: SSL handshake aborted on android old devices
但似乎没有任何效果。
我在 localhost 上运行,但它说有一次我无法连接到本地主机,这就是为什么我在下面的代码中更改了 url。
我的代码:
public class SendProductJsonTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
String data = "";
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) new URL(strings[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes("PostData="+strings[1]);
wr.flush();
wr.close();
InputStream in = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(in);
int inputStreamData = inputStreamReader.read();
while (inputStreamData != -1) {
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
data += current;
}
} catch (Exception e) {
Log.d("ccb", "doInBackground16: "+e.getCause());
Log.d("ccb", "doInBackground17: "+e.getMessage());
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("ccb", "onPostExecute: "+s);
}
}
我如何调用 http 请求:
findViewById(R.id.sendBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: in a click it will gather all data from the user, turn it into a json object, and will send it to the DB;
nameOfProduct = ((EditText)findViewById(R.id.etNameOfPr)).getText().toString();
serialOfProduct = ((EditText)findViewById(R.id.etSerialOfPr)).getText().toString();
priceOfProduct = ((EditText)findViewById(R.id.etPriceOfPr)).getText().toString();
try {
productJson(nameOfProduct, serialOfProduct, priceOfProduct, getGenderRB(), uriList, getColorsCB(), getSizeCB(), new JSONMadeListener() {
@Override
public void onJSONMade(JSONObject object) {
try {
SSLContext.getInstance("TLSv1.2");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
ProviderInstaller.installIfNeeded(getApplicationContext());
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
new SendProductJsonTask().execute("https://10.0.2.2:4000/products/add",object.toString());
}
});
} catch (JSONException e) {
e.printStackTrace();
}
【问题讨论】:
-
不要在 HTTPS URL 中使用 IP 地址。这很可能会失败并解释您的问题,因为证书很可能不是用于 IP 地址而是用于名称,因此客户端完成的验证将失败。