Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)

JxBrowser API provides functionality that allows accessing and modifying POST/PUT/PATCH upload data before it will be sent to a web server. POST/PUT/PATCH upload data can be one of the following types:(JxBrowser API提供的功能允许在将POST / PUT / PATCH上载数据发送到Web服务器之前对其进行访问和修改。 POST / PUT / PATCH上载数据可以是以下类型之一:)

  • PLAIN_TEXT(纯文本)
  • BYTES(字节)
Depending on the upload data type, you can use different strategies for accessing and modifying upload data. The following sample demonstrates how to do this:(根据上传数据类型,您可以使用不同的策略来访问和修改上传数据。 下面的示例演示如何执行此操作)
import com.teamdev.jxbrowser.chromium.*;
import com.teamdev.jxbrowser.chromium.swing.DefaultNetworkDelegate;

 * This sample demonstrates how to read and modify POST data of
 * HTTP request using NetworkDelegate.
public class POSTDataSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserContext browserContext = browser.getContext();
        NetworkService networkService = browserContext.getNetworkService();
        networkService.setNetworkDelegate(new DefaultNetworkDelegate() {
            public void onBeforeURLRequest(BeforeURLRequestParams params) {
                if ("POST".equals(params.getMethod())) {
                    UploadData uploadData = params.getUploadData();
                    UploadDataType dataType = uploadData.getType();
                    if (dataType == UploadDataType.FORM_URL_ENCODED) {
                        FormData data = (FormData) uploadData;
                        data.setPair("key1", "value1", "value2");
                        data.setPair("key2", "value2");
                    } else if (dataType == UploadDataType.MULTIPART_FORM_DATA) {
                        MultipartFormData data = (MultipartFormData) uploadData;
                        data.setPair("key1", "value1", "value2");
                        data.setPair("key2", "value2");
                        data.setFilePair("file3", "C:\\");
                    } else if (dataType == UploadDataType.PLAIN_TEXT) {
                        TextData data = (TextData) uploadData;
                        data.setText("My data");
                    } else if (dataType == UploadDataType.BYTES) {
                        BytesData data = (BytesData) uploadData;
                        data.setData("My data".getBytes());
                    // Apply modified upload data that will be sent to a web server.
        browser.loadURL(new LoadURLParams("http://localhost/", "key=value"));