【问题标题】:Cannot get HTTP Response Body from Network Logs - Selenium无法从网络日志中获取 HTTP 响应正文 - Selenium
【发布时间】:2020-05-17 05:35:43
【问题描述】:

在使用 Java 的 Selenium 中,我试图通过 Chromedriver 获取一个特定 HTTPS 请求的响应正文。我正在使用 LogEntries,但我从日志中获取所有信息,响应正文除外。 有没有办法得到响应体?

        System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\webdrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability(ChromeOptions.CAPABILITY, options);

        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
        options.setCapability("goog:loggingPrefs", logPrefs);
        ChromeDriver driver = new ChromeDriver(options);
        System.out.println("Navigate to " + url);
        driver.navigate().to(url);

        LogEntries logs = driver.manage().logs().get("performance");

        for (Iterator<LogEntry> it = logs.iterator(); it.hasNext();) {
            LogEntry entry = it.next();
            try {
                JSONObject json = new JSONObject(entry.getMessage());
                JSONObject message = json.getJSONObject("message");
                String method = message.getString("method");
                if (method != null && "Network.responseReceived".equals(method)) {
                    method = message.getString("method");
                    JSONObject params = message.getJSONObject("params");
                    JSONObject response = params.getJSONObject("response");
                    String messageUrl = response.getString("url");
                    if (messageUrl.equals("https://pay.sandbox.realexpayments.com/api/auth"))
                        System.out.println(json.toString());
                }
         }

我得到了这个 JSON 响应,它没有响应正文

{ 
   "webview":"221612BC1DF99CA155A0301EEA4FBDAA",
   "message":{ 
      "method":"Network.responseReceived",
      "params":{ 
         "frameId":"221612BC1DF99CA155A0301EEA4FBDAA",
         "requestId":"46320.53",
         "response":{ 
            "headers":{ 
               "Keep-Alive":"timeout=20",
               "Transfer-Encoding":"chunked",
               "Server":"Apache Tomcat",
               "Connection":"keep-alive",
               "X-CONTENT-TYPE-OPTIONS":"nosniff",
               "X-Application-Context":"HPPAzure:Azure:8080",
               "Date":"Fri, 31 Jan 2020 12:27:30 GMT",
               "Content-Type":"application/json;charset=UTF-8",
               "Request-Context":"appId=cid-v1:d078d25d-db86-42cf-88e7-0d8f9ef12ee7"
            },
            "securityDetails":{ 
               "cipher":"AES_256_GCM",
               "signedCertificateTimestampList":[ 
                  { 
                     "signatureData":"3044022056068FEDDA9F229659822AC94F5D35E8BEA1B99C1E4AA4681ED61604DF1AC033022064CFED8EA8FB473AEF690DF63F79FE693D4DF1ED612F09FA4C7A715A6952279E",
                     "logDescription":"Google 'Pilot' log",
                     "origin":"Embedded in certificate",
                     "logId":"A4B90990B418581487BB13A2CC67700A3C359804F91BDFB8E377CD0EC80DDC10",
                     "hashAlgorithm":"SHA-256",
                     "signatureAlgorithm":"ECDSA",
                     "status":"Verified",
                     "timestamp":1.537278661211E12
                  },
                  { 
                     "signatureData":"3046022100A23157B9BF476E1602EA6654620173A78BC47F445E632FDEA433C6B8853D31E1022100F0FFD6115606BAE5FFF0EDDA34DC28BA392DCDC84AC5B1AF8B3D9620BE2850E5",
                     "logDescription":"DigiCert Log Server",
                     "origin":"Embedded in certificate",
                     "logId":"5614069A2FD7C2ECD3F5E1BD44B23EC74676B9BC99115CC0EF949855D689D0DD",
                     "hashAlgorithm":"SHA-256",
                     "signatureAlgorithm":"ECDSA",
                     "status":"Verified",
                     "timestamp":1.537278661319E12
                  },
                  { 
                     "signatureData":"3044022060A1856FF22896D4388E3643132224C9371362F0E5CC0E92F9E9092ABFA8B61A02201433B88051A0E0B1D6814AA1D8CE5E1B45EDB404AE32546749C31947652AB686",
                     "logDescription":"Google 'Skydiver' log",
                     "origin":"Embedded in certificate",
                     "logId":"BBD9DFBC1F8A71B593942397AA927B473857950AAB52E81A909664368E1ED185",
                     "hashAlgorithm":"SHA-256",
                     "signatureAlgorithm":"ECDSA",
                     "status":"Verified",
                     "timestamp":1.53727866125E12
                  }
               ],
               "protocol":"TLS 1.2",
               "certificateId":0,
               "certificateTransparencyCompliance":"compliant",
               "sanList":[ 
                  "pay.sandbox.realexpayments.com"
               ],
               "validFrom":1537228800,
               "issuer":"Thawte EV RSA CA 2018",
               "keyExchange":"ECDHE_RSA",
               "keyExchangeGroup":"P-256",
               "subjectName":"pay.sandbox.realexpayments.com",
               "validTo":1606132800
            },
            "connectionReused":true,
            "fromPrefetchCache":false,
            "timing":{  },
            "encodedDataLength":344,
            "remotePort":443,
            "mimeType":"application/json",
            "securityState":"secure",
            "url":"https://pay.sandbox.realexpayments.com/api/auth",
            "protocol":"http/1.1",
            "fromDiskCache":false,
            "fromServiceWorker":false,
            "remoteIPAddress":"52.155.173.65",
            "statusText":"",
            "connectionId":70,
            "status":200
         },
         "loaderId":"0CB9CB8372A2D2D60E34F31ADDA813D5",
         "type":"XHR",
         "timestamp":3810237.716388
      }
   }
}

我是否遗漏了什么,或者可能有其他方法可以实现这一点? 提前致谢

【问题讨论】:

    标签: selenium selenium-chromedriver


    【解决方案1】:

    试试下面的方法 导入 io.restassured.RestAssured;

    公共类 HttpResponseCode {

    public int httpResponseCodeViaGet(String url) {
    
            return RestAssured.get(url).statusCode();
    
    }
    
    public int httpResponseCodeViaPost(String url) {
    
        return RestAssured.post(url).statusCode();
    
    }
    
    public static void main(String args[]) {
    
        new HttpResponseCode().httpResponseCodeViaGet("http://www.google.com");
    
    }
    

    }

    【讨论】:

    • 其实,我只是想记录不是由我拨打的电话,而是来自应用程序本身的电话。有了restAssured,如果我拨打电话,我将能够得到答复,但不幸的是,这不是我的情况。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2021-03-28
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2022-11-17
      • 2021-12-26
      • 2017-06-24
      • 2018-08-31
      • 1970-01-01
      相关资源
      最近更新 更多