示例#1
0
 private Image getAvatar() {
   HttpConnection httemp = null;
   InputStream istemp = null;
   Image avatar = null;
   try {
     httemp = (HttpConnection) Connector.open(url);
     if (HttpConnection.HTTP_OK != httemp.getResponseCode()) {
       throw new IOException();
     }
     istemp = httemp.openInputStream();
     byte[] avatarBytes = read(istemp, (int) httemp.getLength());
     // #sijapp cond.if modules_TRAFFIC is "true" #
     Traffic.getInstance().addInTraffic(avatarBytes.length);
     // #sijapp cond.end#
     avatar = javax.microedition.lcdui.Image.createImage(avatarBytes, 0, avatarBytes.length);
     avatarBytes = null;
   } catch (Exception e) {
   }
   try {
     httemp.close();
     istemp.close();
   } catch (Exception e) {
   }
   return avatar;
 }
示例#2
0
 private Object request(
     String url, boolean post, Hashtable params, boolean basicAuth, boolean processOutput)
     throws Exception {
   HttpConnection conn = null;
   Writer writer = null;
   InputStream is = null;
   try {
     if (!post && (params != null)) {
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       OutputStreamWriter osw = new OutputStreamWriter(baos, this.encoding);
       this.encodeParams(params, osw);
       osw.flush();
       osw.close();
       osw = null;
       url += "?" + new String(baos.toByteArray(), this.encoding);
       baos = null;
     }
     conn = (HttpConnection) Connector.open(url);
     conn.setRequestMethod(post ? HttpConnection.POST : HttpConnection.GET);
     if (basicAuth) {
       if (!this.areCredentialsSet()) throw new Exception("Credentials are not set");
       String token = base64Encode((this.user + ":" + this.pass).getBytes(this.encoding));
       conn.setRequestProperty("Authorization", "Basic " + token);
     }
     if (post && (params != null)) {
       OutputStream os = conn.openOutputStream();
       writer = new OutputStreamWriter(os, this.encoding);
       this.encodeParams(params, writer);
       writer.flush();
       writer.close();
       os = null;
       writer = null;
     }
     int code = conn.getResponseCode();
     if ((code != 200) && (code != 302))
       throw new Exception("Unexpected response code " + code + ": " + conn.getResponseMessage());
     is = conn.openInputStream();
     if (processOutput) {
       synchronized (this.json) {
         return this.json.parse(is);
       }
     } else {
       this.pump(is, System.out, 1024);
       return null;
     }
   } finally {
     if (writer != null) writer.close();
     if (is != null) is.close();
     if (conn != null) conn.close();
   }
 }
示例#3
0
  private String getContent(String url) {
    HttpConnection httemp = null;
    InputStream istemp = null;
    String content = "";

    try {
      httemp = (HttpConnection) Connector.open(url);
      httemp.setRequestProperty("Connection", "cl" + "ose");
      if (HttpConnection.HTTP_OK != httemp.getResponseCode()) {
        throw new IOException();
      }

      istemp = httemp.openInputStream();
      int length = (int) httemp.getLength();
      if (-1 != length) {
        byte[] bytes = new byte[length];
        istemp.read(bytes);
        content = new String(bytes);

      } else {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        while (true) {
          int ch = istemp.read();
          if (-1 == ch) break;
          bytes.write(ch);
        }
        content = new String(bytes.toByteArray());
        bytes.close();
      }

    } catch (Exception e) {
      content = "Error: " + e.getMessage();
    }
    try {
      httemp.close();
      istemp.close();
    } catch (Exception e) {
    }
    return StringConvertor.removeCr(content);
  }
示例#4
0
  public InputStream makeRequest(String url, OutputStream pos) throws IOException {
    HttpConnection conn = null;
    ByteArrayOutputStream bos = null;
    DataInputStream is = null;
    DataOutputStream os = null;
    InputStream pis = null;

    try {
      conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
      conn.setRequestMethod(HttpConnection.POST);
      conn.setRequestProperty("Content-Type", "application/octet-stream");
      conn.setRequestProperty("Accept", "application/octet-stream");

      if (sessionCookie == null) {
        conn.setRequestProperty("version", "???");
      } else {
        conn.setRequestProperty("cookie", sessionCookie);
      }

      // Getting the output stream may flush the headers
      os = conn.openDataOutputStream();
      os.write(pos.getBytes());
      os.close();

      int responseCode;
      try {
        responseCode = conn.getResponseCode();
      } catch (IOException e) {
        throw new IOException("No response from " + url);
      }

      if (responseCode != HttpConnection.HTTP_OK) {
        throw new IllegalArgumentException();
      }

      bos = new ByteArrayOutputStream();
      {
        is = conn.openDataInputStream();
        String sc = conn.getHeaderField("set-cookie");
        if (sc != null) {
          sessionCookie = sc;
        }

        while (true) {
          int ch = is.read();
          if (ch == -1) break;

          bos.write(ch);
        }

        is.close();
        is = null;

        conn.close();
        conn = null;
      }
      pis = new InputStream(bos.toByteArray());

      bos.close();
      bos = null;
    } catch (Exception e) {
      e.printStackTrace();
      try {
        if (conn != null) {
          conn.close();
          conn = null;
        }

        if (bos != null) {
          bos.close();
          bos = null;
        }

        if (is != null) {
          is.close();
          is = null;
        }
      } catch (Exception exc) {
      }
      throw new IOException();
    }

    return pis;
  }