public String getContent(URL url, boolean altercookies) {
    String s = "";
    try {
      HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
      con.setRequestProperty("Cookie", cookies); // Retain our sessoin
      BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
      String input;
      while ((input = br.readLine()) != null) {
        s += input + "\n";
      }
      br.close();

      StringBuilder sb = new StringBuilder();

      // find the cookies in the response header from the first request
      List<String> cookie = con.getHeaderFields().get("Set-Cookie");
      if (cookie != null) {
        for (String cooki : cookie) {
          if (sb.length() > 0) {
            sb.append("; ");
          }

          // only want the first part of the cookie header that has the value
          String value = cooki.split(";")[0];
          sb.append(value);
        }
      }
      if (altercookies) cookies = sb.toString();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return s;
  }
Beispiel #2
0
 /** SSF-13 HttpOnly flag SSF-16 Secure flag */
 private void checkCookieFlags(HttpsURLConnection connection) {
   List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
   boolean foundSessionCookie = false;
   for (String cookie : cookies) {
     if (StringUtils.containsIgnoreCase(cookie, "JSESSIONID")) {
       foundSessionCookie = true;
       assertThat(cookie).containsIgnoringCase("Secure").containsIgnoringCase("HttpOnly");
     }
   }
   if (!foundSessionCookie) {
     fail("Session cookie not found");
   }
 }
  protected static String getResponseForXPayToken(
      String endpoint, String xpaytoken, String payload, String method, String crId)
      throws IOException {

    logRequestBody(payload, endpoint, xpaytoken, crId);
    HttpsURLConnection conn = null;
    OutputStream os;
    BufferedReader br = null;
    InputStream is;
    String output;
    String op = "";

    URL url1 = new URL(endpoint);
    //	getCertificate();

    conn = (HttpsURLConnection) url1.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod(method);
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("x-request-id", "1234");
    conn.setRequestProperty("x-pay-token", xpaytoken);
    conn.setRequestProperty("X-CORRELATION-ID", crId);

    if (!StringUtils.isEmpty(payload)) {
      os = conn.getOutputStream();
      os.write(payload.getBytes());
      os.flush();
    }
    if (conn.getResponseCode() >= 400) {
      is = conn.getErrorStream();
    } else {
      is = conn.getInputStream();
    }
    if (is != null) {
      br = new BufferedReader(new InputStreamReader(is));
      while ((output = br.readLine()) != null) {
        op += output;
      }
    }

    // Log the response Headers
    Map<String, List<String>> map = conn.getHeaderFields();
    // for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    logger.info("Response Headers: " + map.toString());
    // }

    conn.disconnect();
    logResponseBody(op);

    return op;
  }
  protected Map<String, List<String>> httpsConnection(String url) {

    Map<String, List<String>> headers = null;
    try {
      URL urlobj = new URL(url);
      HttpsURLConnection con = (HttpsURLConnection) urlobj.openConnection();
      headers = con.getHeaderFields();
      Log.d("RES", headers.toString());
      return headers;
    } catch (Exception e) {
      Log.e("ERROR", "Error in httpsConnection():" + e.toString());
      return headers;
    }
  }