jxbrowser 监听所有网络请求 jxbrowser 系列教程2

jxbrowser 在 jxbrowser  中有一个接口叫做 NetworkDelegate  处理url 请求和相应

Modifier and Type Method and Description
boolean onAuthRequired(AuthRequiredParams params)
当请求接收到身份验证并无法使用缓存凭据进行响应时,将调用此方法。
void onBeforeRedirect(BeforeRedirectParams params)
当服务器发起重定向即将发生时,将调用此方法
void onBeforeSendHeaders(BeforeSendHeadersParams params)
当一个请求即将发生并且初始头已经准备好时调用这个方法
void onBeforeSendProxyHeaders(BeforeSendProxyHeadersParams params)
The similar to  onBeforeSendHeaders(BeforeSendHeadersParams) method.
void onBeforeURLRequest(BeforeURLRequestParams params)
在发起请求之前
boolean onCanGetCookies(java.lang.String url, java.util.List cookies)
决定是否可以将cookie发送回Web服务器时,将调用此方法。
boolean onCanSetCookies(java.lang.String url, java.util.List cookies)
This method is invoked when Chromium engine decides whether  cookies can be saved for the  url or not.
void onCompleted(RequestCompletedParams params)
请求成功的时候调用
void onDataReceived(DataReceivedParams params)
接收数据的时候调用
void onDestroyed(RequestParams params)
This method is invoked when a request is being destroyed.
void onHeadersReceived(HeadersReceivedParams params)
This method is invoked each time that an HTTP(S) response header is received.
void onPACScriptError(PACScriptErrorParams params)
This method is invoked when Chromium proxy resolver failed to parse PAC script file to extract proxy settings.
void onResponseStarted(ResponseStartedParams params)
This method is invoked when the first byte of the response body is received.
void onSendHeaders(SendHeadersParams params)
This method is invoked right before the HTTP headers are sent to the network.

我的需求是 访问一个网页,这个网页会发送一个ajax请求,我要拦截他,获取到json
首先我们自己写一个NetworkDelegate  只需要继承DefaultNetworkDelegate 即可。
然后写你需要重写的方法。
因为要模拟手机端访问所以我要重写onBeforeSendHeaders 
因为要接收ajax返回内容故我需要重写onDataReceived
public class JackNetworkDelegate4A47 extends DefaultNetworkDelegate
{
    private  JFrame frame;
    private BrowserView browserView;
    
    public JackNetworkDelegate4A47( JFrame frame,BrowserView browserView)
    {
        this.frame = frame;
        this.frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.browserView = browserView;
    }
    
    
    public void onBeforeSendHeaders(BeforeSendHeadersParams params)
    {
        params.getHeadersEx().setHeader("User-Agent", "Mozilla/5.0 (Linux; U; Android 6.0.1; zh-cn; ZUK Z2131 Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/7.5 Mobile Safari/537.36");
        params.getHeadersEx().setHeader("upgrade-insecure-requests","1");
        params.getHeadersEx().setHeader("cache-control","max-age=0");
        params.getHeadersEx().setHeader("accept-encoding","gzip, deflate, sdch, br");
        params.getHeadersEx().setHeader("accept-language","zh-CN,zh;q=0.8");
        params.getHeadersEx().setHeader("upgrade-insecure-requests","1");
        params.getHeadersEx().removeHeader("Connection");
    }
    public void onDataReceived(DataReceivedParams params)
    {
       // System.out.println(params.getURL());
        if(params.getURL().contains("webmain.php"))
        {
            System.out.println(new String(params.getData()));
            params.getBrowser().dispose();
            frame.dispose();
            frame = null;
        }
        // params.getData()
    }
    
}

在主线程
final Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browserView, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        // Handle proxy authorization.
        browser.getContext().getNetworkService().setNetworkDelegate(new JackNetworkDelegate4A47(frame,browserView));  
        browser.loadURL("https://api.47ks.com/webcloud/?v=http://film.sohu.com/album/9353216.html?channeled=1200110001&type=lemid");
        


你可能感兴趣的:(jxbrowser)