android https 证书校验问题
在做C/S https请求时,服务端加入了证书校验。我在发送请求时已经做了信任所有证书的动作,但是获取返回数据,进行数据流操作时,报javax.net.ssl.SSLException: Connection closed by peer异常,求大神指点!!!代码如下:
public static Response connect(Request request)
{
Response response = new Response();
response.setCorrespondingRequest(request);
HttpURLConnection httpConn = null;
System.setProperty("http.keepAlive", "false");
try
{
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new MyTrustManager() }, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new MyHostnameVerifier());
URL url = new URL(request.getUrl());
if (!url.getProtocol().toLowerCase().equals("https"))
{
httpConn = (HttpURLConnection) url.openConnection();
}
else
{
httpConn = getHttpsConn(url);
}
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setUseCaches(false);
// set request method.
setRequestMethod(request, httpConn);
// set request property.
setRequestProperty(request, httpConn);
httpConn.setConnectTimeout(CONNECT_TIMEOUT);
httpConn.setReadTimeout(READ_TIMEOUT);
if (request.getBody() != null)
{
// set request content.
Logger.i(TAG, "request body : \n" + request.getBody());
byte[] data = request.getBody().getBytes("UTF-8");
// write data.
OutputStream os = httpConn.getOutputStream();
if (request.isGZip())
{
GZIPOutputStream gzos = new GZIPOutputStream(os);
gzos.write(data);
gzos.flush();
gzos.close();
}
else
{
os.write(data);
os.flush();
}
os.close();
}
// get response code.
int responseCode = initResponseCode(response, httpConn);
// connect OK(200) or Created(201)
}
catch 省略
}
红色OutputStream os = httpConn.getOutputStream();报异常。 --------------------编程问答-------------------- 这个一般是服务器提前关闭 或者某个IO流未关闭造成的。 --------------------编程问答-------------------- httpConn.connect();加上试试, --------------------编程问答--------------------
这段代码有,我是放在线程池里做的。只是在上面的代码里没体现出来。 --------------------编程问答--------------------
可是下面有 os.close();呀 --------------------编程问答-------------------- OutputStream os = httpConn.getOutputStream();
这个代码前 已经做了httpConn.connect(); ? 咋没看到。
补充:移动开发 , Android