jxbrowser 監聽所有網路請求 jxbrowser 系列教程2
阿新 • • 發佈:2018-11-10
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<Cookie> cookies)
決定是否可以將cookie傳送回Web伺服器時,將呼叫此方法。
|
boolean |
onCanSetCookies(java.lang.String url, java.util.List<Cookie> 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");