Пример #1
0
  /**
   * 请求xml数据
   *
   * @param url
   * @param soapAction
   * @param xml
   * @return
   */
  public static String sendXMl(String url, String soapAction, String xml) {
    HttpURLConnection conn = null;
    InputStream in = null;
    InputStreamReader isr = null;
    OutputStream out = null;
    StringBuffer result = null;
    try {
      byte[] sendbyte = xml.getBytes("UTF-8");
      URL u = new URL(url);
      conn = (HttpURLConnection) u.openConnection();
      conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
      conn.setRequestProperty("SOAPAction", soapAction);
      conn.setRequestProperty("Content-Length", sendbyte.length + "");
      conn.setDoInput(true);
      conn.setDoOutput(true);
      conn.setConnectTimeout(5000);
      conn.setReadTimeout(5000);

      out = conn.getOutputStream();
      out.write(sendbyte);

      if (conn.getResponseCode() == 200) {
        result = new StringBuffer();
        in = conn.getInputStream();
        isr = new InputStreamReader(in, "UTF-8");
        char[] c = new char[1024];
        int a = isr.read(c);
        while (a != -1) {
          result.append(new String(c, 0, a));
          a = isr.read(c);
        }
      }

    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (conn != null) {
        conn.disconnect();
      }
      try {
        if (in != null) {
          in.close();
        }
        if (isr != null) {
          isr.close();
        }
        if (out != null) {
          out.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return result == null ? null : result + "";
  }
 private static String readStreamToString(InputStream in, String encoding) throws IOException {
   StringBuffer b = new StringBuffer();
   InputStreamReader r = new InputStreamReader(in, encoding);
   int c;
   while ((c = r.read()) != -1) {
     b.append((char) c);
   }
   return b.toString();
 }
Пример #3
0
  public void Connect() throws IOException {
    try {
      InetAddress addr = InetAddress.getByName(server);
      Socket socket = new Socket(addr, port);

      in = new InputStreamReader(socket.getInputStream());
      out = new OutputStreamWriter(socket.getOutputStream());

      char c[] = new char[LINE_LENGTH];

      boolean authed = false;

      while (true) {
        if (in.read(c) > 0) {
          String msg = new String(c).trim();

          if (!authed) {
            if (msg.endsWith("No Ident response")) {

              System.out.println("...sending identity...");
              out.write(String.format("NICK %s\r\n", nickname));
              out.write(String.format("USER %s %s %s :s\r\n", nickname, host, server, realname));
              out.flush();

              authed = true;

              for (IRCAuthListener a : authListeners) {
                a.afterAuthentication(this);
              }
            }
          }

          if (msg.startsWith("PING")) {
            System.out.println("Sending pong...");
            out.write("PONG\r\n");
            out.flush();
          }

          for (IRCRawDataListener d : rawDataListeners) {
            d.processRawData(this, msg);
          }

          c = new char[LINE_LENGTH];
        } else {
          System.out.println("Oops, if you see this we just got kicked or connection timed out...");
          break;
        }
      }
    } catch (IOException ioe) {
      throw (ioe);
    }
  }
Пример #4
0
 private boolean checkServer(int port, boolean ssl) {
   try {
     URL url = new URL("http://127.0.0.1:" + port);
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     conn.setRequestMethod("GET");
     conn.connect();
     int length = conn.getContentLength();
     StringBuffer text = new StringBuffer();
     InputStream is = conn.getInputStream();
     InputStreamReader isr = new InputStreamReader(is);
     int size = 256;
     char[] buf = new char[size];
     int len;
     while ((len = isr.read(buf, 0, size)) != -1) text.append(buf, 0, len);
     isr.close();
     if (programName.equals("ISN")) return !shutdown(port, ssl);
     return true;
   } catch (Exception ex) {
     return false;
   }
 }
Пример #5
0
  @Override
  public void run() {
    System.out.println("Starting Listener");
    int character;
    try {
      socket1 = new ServerSocket(port);
      game.gui.lock("Waiting for other player");
      connection = socket1.accept();

      bis = new BufferedInputStream(connection.getInputStream());
      isr = new InputStreamReader(bis);

      while ((character = isr.read()) != 13) {
        strb.append((char) character);
      }

      // if received CLOSEMESSAGE -> end listening and do something
      if (strb.toString().equals(Online.CLOSEMESSAGE)) {
        System.out.println("Looks like we have ended...");
      }

      // check incoming message
      System.out.println(strb);

      Figure f = game.parseFigure(strb.toString());
      Position p = game.parsePosition(strb.toString());
      game.playerPlay(f, p);

      game.switchPlayers();
      game.gui.unlock();
      connection.close();
      System.out.println("moved, your turn!");
      socket1.close();

    } catch (IOException e) {
      System.out.println("Unexpected exception on server side" + e);
    }
  }
Пример #6
0
  public void print(IWContext iwc) throws Exception {

    if (doPrint(iwc)) {

      if (getMarkupLanguage().equals("HTML")) {

        // if (getInterfaceStyle().equals("something")){

        // }

        // else{

        try {

          URL url = new URL(getURL());

          String returnString = "";

          // URL url = new URL("http://localhost/servlet/plainTextModule");

          URLConnection uc = url.openConnection();

          uc.setDoOutput(true);

          uc.setDoInput(true);

          uc.setUseCaches(false);

          uc.setRequestProperty("Content-type", "text/html");

          // uc.setRequestProperty("Content-type","application/x-www-form-urlencoded");

          /*DataOutputStream dos = new DataOutputStream(uc.getOutputStream());

          dos.writeBytes(qry);

          dos.flush();

          dos.close();*/

          InputStreamReader in = new InputStreamReader(uc.getInputStream());

          int chr = in.read();

          while (chr != -1) {

            returnString = returnString + (String.valueOf((char) chr));

            chr = in.read();
          }

          println(returnString);

          // clean up!

          in.close();

        } catch (MalformedURLException e) {

          throw new IOException(e.getMessage());
        }

        // }

      } else if (getMarkupLanguage().equals("WML")) {

      }

    } else {

      super.print(iwc);
    }
  }