Volley访问HTTPS


Volley  获取请求队列的静态方法

外部可以实现支持https的 HttpStack 

如果不为空 就用我们自定义的httpstack构造 Network 

如果为null 就用默认的 当版本小于9 用HttpClient 

大于9的 用HttpUrlConnection 链接


<span style="font-size:18px;"> public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
        File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

        String userAgent = "volley/0";
        try {
            String packageName = context.getPackageName();
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            userAgent = packageName + "/" + info.versionCode;
        } catch (NameNotFoundException e) {
        }

        if (stack == null) {
            if (Build.VERSION.SDK_INT >= 9) {
                stack = new HurlStack();
            } else {
                // Prior to Gingerbread, HttpUrlConnection was unreliable.
                // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
                stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
            }
        }

        Network network = new BasicNetwork(stack);

        RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
        queue.start();

        return queue;
    }</span>


下面是从demo拿来修改一些

<span style="font-size:18px;">public class EasySSLSocketFactory implements LayeredSocketFactory {

    private SSLContext sslcontext = null;
    private boolean isConnectingYourServer = true;
    private static Context mContext;
    
  
    public EasySSLSocketFactory(boolean isConnectingToGoinoutServer,Context context) {</span>
<span style="font-size:18px;">       
    	this.isConnectingYourServer = isConnectingToGoinoutServer;
    	this.mContext=context;
    	
    }

    private static SSLContext createEasySSLContext() throws IOException {
        try {
        	
           
        	InputStream clientStream = mContext.getResources().openRawResource(R.raw.client);
        	char[] password = "123456".toCharArray();
	        
	        KeyStore keyStore = KeyStore.getInstance("PKCS12");
	        keyStore.load(clientStream, password);
           
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keyStore, password);

	        KeyStore trustStore  = KeyStore.getInstance("BKS");
	        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
//	        trustStore.load(null);
	        InputStream instream = null;
	        instream = mContext.getResources().openRawResource(R.raw.success);
//	        trustStore.setCertificateEntry("dd", certificateFactory.generateCertificate(instream));
	        try {
	            trustStore.load(instream, "123456".toCharArray());
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try { instream.close(); } catch (Exception ignore) {}
	        }            

            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
         
            tmf.init(trustStore);

            SSLContext context = SSLContext.getInstance("TLS");
         context.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());

            return context;
        } catch (Exception e) {
        	e.printStackTrace();
            throw new IOException(e.getMessage());
        }
    }

    private static SSLContext createIgnoreSSLContext() throws IOException {
        try {
        	SSLContext context = SSLContext.getInstance("TLS");
        	context.init(null, new TrustManager[]{new IgnoreCertTrustManager()}, null);
        	return context;
        } catch (Exception e) {
        	e.printStackTrace();
        	throw new IOException(e.getMessage());
        }
    }

    private SSLContext getSSLContext() throws IOException {
        if (this.sslcontext == null) {
        	if(isConnectingYourServer) {
        		this.sslcontext = createEasySSLContext();
        	} else {
        		//Ignore the Certificate Authority check if not connecting to your server.
        		this.sslcontext = createIgnoreSSLContext();
        	}
        }
        
        return this.sslcontext;
    }

    public Socket connectSocket(Socket sock, String host, int port,
                                InetAddress localAddress, int localPort, HttpParams params)
            throws IOException, UnknownHostException, ConnectTimeoutException {
        int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
        int soTimeout = HttpConnectionParams.getSoTimeout(params);

        InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
        SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());

        if ((localAddress != null) || (localPort > 0)) {
            // we need to bind explicitly
            if (localPort < 0) {
                localPort = 0; // indicates "any"
            }
            InetSocketAddress isa = new InetSocketAddress(localAddress,
                    localPort);
            sslsock.bind(isa);
        }

        sslsock.connect(remoteAddress, connTimeout);
        sslsock.setSoTimeout(soTimeout);
        return sslsock;

    }

    public Socket createSocket() throws IOException {
        return getSSLContext().getSocketFactory().createSocket();
    }

    public boolean isSecure(Socket socket) throws IllegalArgumentException {
        return true;
    }

    public Socket createSocket(Socket socket, String host, int port,
                               boolean autoClose) throws IOException, UnknownHostException {
        return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
    }

    public boolean equals(Object obj) {
        return ((obj != null) && obj.getClass().equals(
                EasySSLSocketFactory.class));
    }

    public int hashCode() {
        return EasySSLSocketFactory.class.hashCode();
    }


}</span>

isConnectingYourServer true 证书验证

false 忽略证书验证 


<span style="font-size:18px;"> private SSLContext getSSLContext() throws IOException {
        if (this.sslcontext == null) {
        	if(isConnectingYourServer) {
        		this.sslcontext = createEasySSLContext();
        	} else {
        		//Ignore the Certificate Authority check if not connecting to your server.
        		this.sslcontext = createIgnoreSSLContext();
        	}
        }
        
        return this.sslcontext;</span>


<span style="font-size:18px;">public class IgnoreCertTrustManager implements X509TrustManager {


    @Override
    public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
}
</span>


自定义HttpStack

<span style="font-size:18px;">public class SslHttpStack implements HttpStack {
    
	private boolean mIsConnectingToYourServer = false;    
    private final static String HEADER_CONTENT_TYPE = "Content-Type";
    private Context mContext;

    public SslHttpStack(boolean isYourServer,Context context) {
    	mIsConnectingToYourServer = isYourServer;
    	mContext=context;
    }
    
    private static void addHeaders(HttpUriRequest httpRequest, Map<String, String> headers) {
        for (String key : headers.keySet()) {
            httpRequest.setHeader(key, headers.get(key));
        }
    }

    @SuppressWarnings("unused")
    private static List<NameValuePair> getPostParameterPairs(Map<String, String> postParams) {
        List<NameValuePair> result = new ArrayList<NameValuePair>(postParams.size());
        for (String key : postParams.keySet()) {
            result.add(new BasicNameValuePair(key, postParams.get(key)));
        }
        return result;
    }

    @Override
    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
            throws IOException, AuthFailureError {
        HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
        addHeaders(httpRequest, additionalHeaders);
        addHeaders(httpRequest, request.getHeaders());
        onPrepareRequest(httpRequest);
        HttpParams httpParams = httpRequest.getParams();
        
        int timeoutMs = request.getTimeoutMs();
    
        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
        HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
     
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", new PlainSocketFactory(), 80));
        registry.register(new Scheme("https", new EasySSLSocketFactory(mIsConnectingToYourServer,mContext), 443));
     
        ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(httpParams, registry);
        HttpClient httpClient = new DefaultHttpClient(manager, httpParams); 

        return httpClient.execute(httpRequest);
    }

    /**
     * Creates the appropriate subclass of HttpUriRequest for passed in request.
     */
    @SuppressWarnings("deprecation")
    /* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
            Map<String, String> additionalHeaders) throws AuthFailureError {
        switch (request.getMethod()) {
            case Method.DEPRECATED_GET_OR_POST: {
                // This is the deprecated way that needs to be handled for backwards compatibility.
                // If the request's post body is null, then the assumption is that the request is
                // GET.  Otherwise, it is assumed that the request is a POST.
                byte[] postBody = request.getPostBody();
                if (postBody != null) {
                    HttpPost postRequest = new HttpPost(request.getUrl());
                    postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
                    HttpEntity entity;
                    entity = new ByteArrayEntity(postBody);
                    postRequest.setEntity(entity);
                    return postRequest;
                } else {
                    return new HttpGet(request.getUrl());
                }
            }
            case Method.GET:
                return new HttpGet(request.getUrl());
            case Method.DELETE:
                return new HttpDelete(request.getUrl());
            case Method.POST: {
                HttpPost postRequest = new HttpPost(request.getUrl());
                postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
                setMultiPartBody(postRequest,request);
                setEntityIfNonEmptyBody(postRequest, request);
                return postRequest;
            }
            case Method.PUT: {
                HttpPut putRequest = new HttpPut(request.getUrl());
                putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
                setMultiPartBody(putRequest,request);
                setEntityIfNonEmptyBody(putRequest, request);
                return putRequest;
            }
            // Added in source code of Volley libray.
//            case Method.PATCH: {
//            	HttpPatch patchRequest = new HttpPatch(request.getUrl());
//            	patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
//                setEntityIfNonEmptyBody(patchRequest, request);
//                return patchRequest;
//            }
            default:
                throw new IllegalStateException("Unknown request method.");
        }
    }

    private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest,
            Request<?> request) throws AuthFailureError {
        byte[] body = request.getBody();
        if (body != null) {
            HttpEntity entity = new ByteArrayEntity(body);
            httpRequest.setEntity(entity);
        }
    }

    private static void setMultiPartBody(HttpEntityEnclosingRequestBase httpRequest,
            Request<?> request) throws AuthFailureError {
    	
    	// Return if Request is not MultiPartRequest
    	if(request instanceof MultiPartRequest == false) {
    		return;
    	}
        
    	MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
 
        Map<String,File> fileUpload = ((MultiPartRequest)request).getFileUploads();
        for (Map.Entry<String, File> entry : fileUpload.entrySet()) {
        	System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        	multipartEntity.addPart(((String)entry.getKey()), new FileBody((File)entry.getValue()));
        }

        Map<String,String> stringUpload = ((MultiPartRequest)request).getStringUploads();
        for (Map.Entry<String, String> entry : stringUpload.entrySet()) {
        	System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        	try {
        	multipartEntity.addPart(((String)entry.getKey()), new StringBody((String)entry.getValue()));
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        }
        
        httpRequest.setEntity(multipartEntity);
    }

    /**
     * Called before the request is executed using the underlying HttpClient.
     *
     * <p>Overwrite in subclasses to augment the request.</p>
     */
    protected void onPrepareRequest(HttpUriRequest request) throws IOException {
        // Nothing.
    }
}</span>



最后测试

<span style="font-size:18px;">	RequestQueue requestQueue= Volley.newRequestQueue(context, new SslHttpStack(true, context));
	StringRequest s= new StringRequest(Request.Method.GET, "https://localhost:8443/123.html", new Listener<String>() {

		@Override
		public void onResponse(String arg0) {
			Log.e("success", arg0);
			
		}
	}, new Response.ErrorListener() {

		@Override
		public void onErrorResponse(VolleyError arg0) {
			Log.e("erro", arg0.toString());
			
		}
	});
	 
	 requestQueue.add(s);
		
		
	} </span>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值