gpt4 book ai didi

java - Android Studio 中 ssl 认证的 url 抛出 IOException

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

我在包含 ssl 证书的服务器上部署了一个 REST api。当我从我的 android 应用程序访问 api 时,我遇到了信任 anchor 证书错误,因此我将代码添加到我的处理程序中以信任所有主机,但现在我在 url 上收到了 IOException。请建议我该怎么做?

HttpHandler.java

package abc.com.abcpos.Handlers;

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class HttpHandler {

private static final String TAG = HttpHandler.class.getSimpleName();

public HttpHandler(){

}

public String makeServiceCall(String reqUrl){
String response=null;
try{
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};

// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};

HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
URL url=new URL(reqUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");

//read the response
InputStream in=new BufferedInputStream(conn.getInputStream());
response=convertStreamToString(in);
}catch (MalformedURLException e){
Log.e(TAG, "MalformedURLException: "+e.getMessage() );
}catch (ProtocolException e){
Log.e(TAG, "ProtocolException: "+e.getMessage() );
}catch (IOException e){
Log.e(TAG, "IOException: "+e.getMessage());
}catch (Exception e){
Log.e(TAG, "Exception: "+e.getMessage() );
}
Log.d(TAG, "makeServiceCall: "+response);
return response;
}


private String convertStreamToString(InputStream is) {

BufferedReader reader=new BufferedReader(new InputStreamReader(is));
StringBuilder sb=new StringBuilder();
String line;
try {
while ((line=reader.readLine())!=null){
sb.append(line).append('\n');
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
is.close();
}catch (IOException e){
e.printStackTrace();
}
}
return sb.toString();
}

}

在我调用的 Activity 的doInBackground 方法中

HttpHandler sh = new HttpHandler();
String url = myUrl;

我应该为此做什么?请帮助我。

日志跟踪:

08-24 13:13:43.774 10431-10436/abc.com.abcpos I/art: Do partial code cache collection, code=62KB, data=61KB
08-24 13:13:43.775 10431-10436/abc.com.abcpos I/art: After code cache collection, code=62KB, data=61KB
Increasing code cache capacity to 256KB
08-24 13:13:49.668 10431-10485/abc.com.abcpos E/HttpHandler: IOException: myUrl
08-24 13:13:49.668 10431-10485/abc.com.abcpos D/HttpHandler: makeServiceCall: null
08-24 13:13:49.706 10431-10431/abc.com.abcpos D/AndroidRuntime: Shutting down VM
08-24 13:13:49.707 10431-10431/abc.com.abcpos E/AndroidRuntime: FATAL EXCEPTION: main
Process: abc.com.abcpos, PID: 10431
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String abc.com.abcpos.Models.EmpPersonalDetails.getFirstName()' on a null object reference
at abc.com.abcpos.forms.PersonalDetailsFormActivity$GetContacts.onPostExecute(PersonalDetailsFormActivity.java:520)
at abc.com.abcpos.forms.PersonalDetailsFormActivity$GetContacts.onPostExecute(PersonalDetailsFormActivity.java:375)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)

由于某些原因,我已将日志中的实际 url 更改为 myUrl,请忽略它。调试后我知道我在 InputStream in=new BufferedInputStream(conn.getInputStream()); 处遇到错误,请帮助解决它

最佳答案

嘿,你应该检查你正在使用的调用类型。您确定它的 POST 不是 GET 吗?

关于java - Android Studio 中 ssl 认证的 url 抛出 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51999854/

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