Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here and here.(注意:本文中的建议仅适用于JxBrowser6。请在此处和此处参阅有关JxBrowser 7的相应文章。)
The user-agent string can be modified only once, before you create any Browser instances. You can provide your own user-agent string using the BrowserPreferences.setUserAgent(String userAgent) method or via the jxbrowser.chromium.user-agent Java System Property: (在创建任何浏览器实例之前,只能修改一次用户代理字符串。您可以使用BrowserPreferences.setUserAgent(String userAgent)方法或通过jxbrowser.chromium.user-agent Java系统属性来提供自己的用户代理字符串:)
BrowserPreferences.setUserAgent("My User Agent String");
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserPreferences;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import javax.swing.*;
import java.awt.*;
/**
* The sample demonstrates how to configure user-agent string
* for all Browser instances.
*/
public class UserAgentSample {
public static void main(String[] args) {
// It's very important to configure user-agent string
// before any Browser instance is created.
BrowserPreferences.setUserAgent("My User Agent String");
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);
browser.loadURL("http://whatsmyuseragent.com/");
}
}