安卓-绑定移动网络套接字时出现“Permissiondenied”错误(OkHttp.socketFactory(network.socketFactory))
创始人
2024-09-01 16:00:10
0

在AndroidManifest.xml文件中添加以下权限:



然后在代码中使用以下方法创建OkHttpClient:

OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .socketFactory(new RestrictedSocketFactory())
        .build();

其中,RestrictedSocketFactory是自定义的一个类,用于限制套接字的绑定:

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

public class RestrictedSocketFactory extends SSLSocketFactory {

    private SSLSocketFactory delegate;

    public RestrictedSocketFactory() {
        try {
            delegate = (SSLSocketFactory) SSLSocketFactory.getDefault();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Socket createSocket() throws IOException {
        return delegate.createSocket();
    }

    @Override
    public Socket createSocket(String host, int port) throws IOException {
        return createSocket(InetAddress.getByName(host), port);
    }

    @Override
    public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
        return createSocket(host, port);
    }

    @Override
    public Socket createSocket(InetAddress host, int port) throws IOException {
        Socket socket = new Socket();
        socket.bind(null);
        socket.connect(new InetSocketAddress(host, port));
        return socket;
    }

    @Override
    public Socket createSocket(InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException {
        Socket socket = new Socket();
        socket.bind(new InetSocketAddress(localAddress, localPort));
        socket.connect(new InetSocketAddress(host, port));
        return socket;
    }

    @Override
    public Socket createSocket(Socket s, InputStream consumed, boolean autoClose) throws IOException {
        byte[]

相关内容

热门资讯

辅助透视!wepoker破解器... 辅助透视!wepoker破解器激活码,wepoker透视脚本免费使用视频(透视)玩家教程(有挂工具)...
透视计算!we-poker辅助... 透视计算!we-poker辅助,wepoker免费透视(透视)攻略教程(有挂脚本)在进入wepoke...
透视安装!wepoker有没有... 透视安装!wepoker有没有辅助,wepoker插件辅助(透视)细节方法(有挂教程)1、首先打开w...
辅助透视!wejoker内置辅... 辅助透视!wejoker内置辅助,we-poker辅助器(透视)揭秘教程(有挂工具)1、该软件可以轻...
透视肯定!wepoker有没有... 透视肯定!wepoker有没有透视方法,wepoker好友房开挂(透视)力荐教程(有挂黑科技)1)w...
透视模拟器!wepoker怎么... 透视模拟器!wepoker怎么买辅助,wepoker私人局规律(透视)切实教程(有挂细节)1、让任何...
透视脚本!wepoker俱乐部... 透视脚本!wepoker俱乐部辅助,淘宝买wepoker透视有用吗(透视)解密教程(有挂解说)1)淘...
透视挂透视!wepoker透视... 透视挂透视!wepoker透视器免费,wepoker透视破解版(透视)揭秘教程(有挂详情);1、下载...
透视实锤!wepoker怎么设... 透视实锤!wepoker怎么设置房间,we-poker是什么软件(透视)靠谱教程(有挂详情);wep...
透视免费!wepoker私人局... 透视免费!wepoker私人局辅助挂,we-poker辅助软件教程(透视)透明挂教程(有挂解密)1、...