public String readResponse(HttpResponse response) {
    String output = "";

    HttpEntity entity = response.getEntity();

    try {
      trapException(response.getStatusLine().getStatusCode());
    } catch (CrowdFlowerException e1) {
      e1.printStackTrace();
    }

    InputStream instream;
    try {
      instream = entity.getContent();
      BufferedReader reader = new BufferedReader(new InputStreamReader(instream));

      // do something useful with the response
      output = output + reader.readLine();
      instream.close();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return output;
  }
Beispiel #2
0
  public static void main(String[] args) throws Exception {

    System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
    /**
     * This test does not establish any connection to the specified URL, hence a dummy URL is used.
     */
    URL foobar = new URL("https://example.com/");

    HttpsURLConnection urlc = (HttpsURLConnection) foobar.openConnection();

    try {
      urlc.getCipherSuite();
    } catch (IllegalStateException e) {
      System.out.print("Caught proper exception: ");
      System.out.println(e.getMessage());
    }

    try {
      urlc.getServerCertificateChain();
    } catch (IllegalStateException e) {
      System.out.print("Caught proper exception: ");
      System.out.println(e.getMessage());
    }

    try {
      urlc.setDefaultHostnameVerifier(null);
    } catch (IllegalArgumentException e) {
      System.out.print("Caught proper exception: ");
      System.out.println(e.getMessage());
    }

    try {
      urlc.setHostnameVerifier(null);
    } catch (IllegalArgumentException e) {
      System.out.print("Caught proper exception: ");
      System.out.println(e.getMessage());
    }

    try {
      urlc.setDefaultSSLSocketFactory(null);
    } catch (IllegalArgumentException e) {
      System.out.print("Caught proper exception: ");
      System.out.println(e.getMessage());
    }

    try {
      urlc.setSSLSocketFactory(null);
    } catch (IllegalArgumentException e) {
      System.out.print("Caught proper exception");
      System.out.println(e.getMessage());
    }
    System.out.println("TESTS PASSED");
  }
Beispiel #3
0
  // sends a response to the Client based upon the contents of the receive packet
  private void reply(DatagramPacket receivedPacket) {
    // verify the packet
    byte serverMsg[] = {};
    try {
      Request recReq = new Request(receivedPacket.getData(), receivedPacket.getLength());
      if (!recReq.isValid()) {
        throw new IllegalStateException();
      }
      switch (recReq.getFormat()) {
        case RRQ:
          serverMsg = new byte[] {0, 3, 0, 1};
          break;
        case WRQ:
          serverMsg = new byte[] {0, 4, 0, 0};
          break;
        default:
          throw new IllegalStateException();
      }
    } catch (IllegalStateException e) {
      e.printStackTrace();
      System.exit(1);
    }

    DatagramPacket sendPacket =
        new DatagramPacket(
            serverMsg, serverMsg.length, receivedPacket.getAddress(), receivedPacket.getPort());

    readSendPacket(sendPacket);

    try {
      // Send the datagram packet to the client via the send socket.
      DatagramSocket sendSocket = new DatagramSocket();
      sendSocket.send(sendPacket);
      sendSocket.close();
    } catch (SocketException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    System.out.println("Server: packet sent.\n");
  }