展开全部
这个看着是https协议的URL,用普通的http请求就报错了,我这里只有请求https到流的代码,给你先看看,把流再转成文件 就可以e5a48de588b662616964757a686964616f31333361306438了@SuppressLint("ParserError")
public static BufferedInputStream DownloadHttpsUrl2Stream(String downUrl, String postStr) throws IOException {
String res = "";
HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
SSLContext context = null;
try {
context = SSLContext.getInstance("TLS");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
context.init(null, new X509TrustManager[]{new myX509TrustManager()}, new SecureRandom());
} catch (KeyManagementException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
URL dUrl = new URL(downUrl);
HttpsURLConnection dConn = (HttpsURLConnection)dUrl.openConnection();
dConn.setDoInput(true);
if (postStr != ""){
dConn.setDoOutput(true);
dConn.setRequestMethod("POST");
}
dConn.connect();
if (postStr != ""){
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
dConn.getOutputStream()));
out.write(postStr);
out.flush();
} catch (Exception e) {
String errMsg = e.getMessage();
if (null != errMsg) {
Toast toast = Toast.makeText(null, errMsg, Toast.LENGTH_LONG);
toast.show();
}
e.printStackTrace();
}
}
BufferedInputStream in = new BufferedInputStream(dConn.getInputStream());
return in;
}
class NullHostNameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
//Log.i("RestUtilImpl", "Approving certificate for " + hostname);
return true;
}
}
class myX509TrustManager implements X509TrustManager {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
// TODO Auto-generated method stub
}
}