⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wapptr.java~349~

📁 jwap 协议 udp 可以用于手机通讯
💻 JAVA~349~
📖 第 1 页 / 共 5 页
字号:
                        System.out.println(count +
                                           " Executing GET Request for URL " +
                                           request.getURL());
                        if (log.isDebugEnabled()) {
                            log.debug("Executing GET Request for URL " +
                                      request.getURL());
                        }
                        mgr = session.s_get(headers, request.getURL());
                    } else if (request instanceof PostRequest) {
                        System.out.println(count +
                                           " Executing POST Request for URL " +
                                           request.getURL());
                        if (log.isDebugEnabled()) {
                            log.debug("Executing POST Request for URL " +
                                      request.getURL());
                        }
                        PostRequest post = (PostRequest) request;
                        mgr = session.s_post(post.getWSPHeaders(),
                                             post.getRequestBody(),
                                             post.getContentType(), post.getURL());
                    }

                }
            } else {
                break;
            }
        }
        if (response == null) {
            throw new SocketException("Timeout executing request");
        }
        return response;
    }

    /**
     * Connect to the WAP gateway. Before requests can be executed, this method
     * must be called.
     * @throws SocketException if the connection could not be established
     * @throws IllegalStateException if the client is already connected
     */
    public synchronized void connect() throws SocketException,
            IllegalStateException {
        connect(DEFAULT_CONNECT_TIMEOUT);
    }

    /**
     * Connect to the WAP gateway. Before requests can be executed, this method
     * must be called.
     * @param timeout timeout in milliseconds
     * @throws SocketException if the connection could not be established
     * @throws IllegalStateException if the client is already connected
     */
    public synchronized int connect_http(long timeout) throws SocketException,
            IllegalStateException {
        int ret = 7;
        if (http_session != null) {
            throw new IllegalStateException("Already connected");
        }
        gwPort = 80;
        disconnectTimeout = timeout;
        pendingRequests.clear();
        System.out.println("开始连接网关 " + gwAddr.getHostAddress() + ":" + gwPort);
        if (log.isDebugEnabled()) {
            log.debug("Establishing WSP session with " + gwAddr.getHostAddress() +
                      ":" + gwPort);
        }
        http_session = null;
        http_session = new HttpSession(gwAddr, gwPort, localAddr, localPort,
                                       upperLayerImpl, false);

        /* if(httpwaitForCompletion(timeout)==1)
         {
             System.exit(1);
         }*/
        ret = http_session.Sconnect(gwAddr, gwPort);
        if (ret == 5) {
            ret = 5;
            System.out.println("连接网关成功 ");
            log.debug("Connection established");
        } else {
            System.out.println("连接网关失败 ");
        }

        return ret;
    }

    /**
     * Connect to the WAP gateway. Before requests can be executed, this method
     * must be called.
     * @param timeout timeout in milliseconds
     * @throws SocketException if the connection could not be established
     * @throws IllegalStateException if the client is already connected
     */
    public synchronized int connect(long timeout) throws SocketException,
            IllegalStateException {
        int ret = 0;
        ret = connect(null, timeout);
        return ret;
    }

    /**
     * Connect to the WAP gateway. Before requests can be executed, this method
     * must be called.
     * @param timeout timeout in milliseconds
     * @param headers WSP headers used for connect or null
     *   objects. The headers will be encoded using the default WAP codepage.
     * @throws SocketException if the connection could not be established
     * @throws IllegalStateException if the client is already connected
     */
    public synchronized int connect(CWSPHeaders headers, long timeout) throws
            SocketException, IllegalStateException {

        int ret = 8;
        if (session != null) {
            throw new IllegalStateException("Already connected");
        }

        disconnectTimeout = timeout;
        pendingRequests.clear();
        System.out.println("开始连接网关 " + gwAddr.getHostAddress() + ":" + gwPort);
        if (log.isDebugEnabled()) {
            log.debug("Establishing WSP session with " + gwAddr.getHostAddress() +
                      ":" + gwPort);
        }

        session = new CWSPSession(gwAddr, gwPort, localAddr, localPort,
                                  upperLayerImpl, false);

        session.linkgate = linkgate;
        session.s_connect(headers, AgentName);

        if (linkgate == 1) {
            Object result = waitForCompletion(sessionLock, timeout);

            if (result == null) {
                CWSPSession ts = session;
                session = null;
                ret = 7;
                try {
                    ts.s_disconnect();
                } catch (Exception unknown) {}
                throw new SocketException("connect: Timeout occurred");
            }

            if (result != null) {

                if (result instanceof CWSPSocketAddress[]) {
                    // redirect received ...
                    CWSPSocketAddress[] addr = (CWSPSocketAddress[]) result;
                    if (addr.length > 0) {
                        // Take the first address and try to reconnect...
                        gwAddr = addr[0].getAddress();
                        int p = addr[0].getPort();
                        if (p > 0) {
                            gwPort = p;
                        }
                        session = null;
                        System.out.println("重定向网关 " + gwAddr.getHostAddress() +
                                           ":" +
                                           gwPort);
                        if (log.isDebugEnabled()) {
                            log.debug("Redirect to " + gwAddr.getHostAddress() +
                                      ":" + gwPort);
                        }
                        ret = connect(headers, timeout);
                        return ret;
                    }

                } else if (!CONNECTED.equals(result)) {
                    ret = 7;
                    CWSPSession ts = session;
                    session = null;
                    ts.s_disconnect();
                    if (result == null) {
                        throw new SocketException(
                                "Timeout while establishing connection");
                    } else if (!CONNECTED.equals(result)) {
                        throw new SocketException("Connection failed.");
                    }
                }
            }
            if (CONNECTED.equals(result)) {
                ret = 5;
                System.out.println("连接网关成功 ");
                log.debug("Connection established");
            } else {
                System.out.println("连接网关失败 ");
            }
        } else {
            ret = 5;
        }
        return ret;
    }

    /**
     * Disconnect from the WAP gateway. This releases used resources as well.
     */
    public synchronized void disconnect() {
        if (session == null) {
            return;
        }
        log.debug("Disconnecting client...");
        CWSPSession ts = session;
        session = null;
        ts.s_disconnect();
        // Wait for confirmation
        Object result = waitForCompletion(sessionLock, disconnectTimeout);
        session = null;
        log.debug("Client disconnected...");
    }

    public synchronized void http_disconnect() {
        if (http_session == null) {
            return;
        }
        log.debug("Disconnecting client...");
        //CWSPSession ts = session;
        //session = null;
        //ts.s_disconnect();
        // Wait for confirmation
        http_session.pThreadClose();
        http_session = null;
        log.debug("Client disconnected...");
    }


    /**
     * Check if the client is currently connected to the WAP gateway
     * @return true if the client is connected, false otherwise
     */
    public synchronized boolean isConnected() {
        return session != null;
    }

    private static byte[] readPostData(String input) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream in = null;
        if (input == null) {
            System.out.println(
                    "Reading post-data from input stream, hit EOF when done");
            in = System.in;
        } else if ("-".equals(input)) {
            in = System.in;
        } else {
            in = new FileInputStream(input);
        }
        byte[] buf = new byte[1024];
        int read = 0;
        while ((read = in.read(buf)) > 0) {
            out.write(buf, 0, read);
        }
        in.close();
        return out.toByteArray();
    }


    private int httpwaitForCompletion(long timeout) {
        int ret = 0;
        long startAt = 0;
        int first = 0;
        if (timeout > 0) {
            startAt = System.currentTimeMillis();
        } while (http_session != null) {
            if (waitfor == 1) {
                ret = 0;
                break;
            }
            if (waitfor == 2) {
                ret = 1;
                waitfor = 0;
                break;
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
            if (first == 1) {
                startAt = markflg.Getbagflg();
            }
            first = 1;
            if (timeout > 0 && (startAt + timeout) < System.currentTimeMillis()) {
                log.debug("Timeout occurred");
                System.out.println("Timeout occurred");
                ret = 1;
                break;
            }
        }
        //ret=1;
        return ret;
    }

    private Object waitForCompletion(Object key, long timeout) {
        Object object = null;
        long startAt = 0;
        long ntimeout = timeout;
        int first = 0;
        if (timeout > 0) {
            startAt = System.currentTimeMillis();
        } while (object == null) {

            if (first == 1) {
                startAt = markflg.Getbagflg();
            }
            first = 1;
            if (markflg.writefileflg() == 1) {
                ntimeout = markflg.FileWrite();
                //log.error("f:" + ntimeout);
            }
            //log.error("f:" + startAt);
            if (timeout > 0 && (startAt + ntimeout) < System.currentTimeMillis()) {
                log.debug("Timeout occurred");
                break;
            }
            synchronized (pendingRequests) {
                object = pendingRequests.remove(key);
                if (object == null) {
                    try {
                        pendingRequests.wait(ntimeout);
                    } catch (InterruptedException e) {
                        log.warn("Interrupted");
                    }
                }
            }
        }
        return object;
    }

    private void complete(Object key, Object value) {
        synchronized (pendingRequests) {
            pendingRequests.put(key, value);
            pendingRequests.notifyAll();
        }
    }

    private void writeBytesToFile(byte[] payload) {

        markflg.wflg = 1;
        String dstr, wbfile, wmlfile, htmlfile;
        wbfile = wmlfile = htmlfile = "";
        int flag20 = 0;
        int kjava = 0;
        System.out.println("writing to a file");
        Calendar ca = Calendar.getInstance();
        Date dt = new Date();
        ca.setTime(dt);
        dstr = new String(String.valueOf(ca.get(Calendar.YEAR)));
        dstr = dstr + ca.get(Calendar.MONTH) + ca.get(Calendar.DAY_OF_MONTH) +
               ca.get(Calendar.HOUR) + ca.get(Calendar.MINUTE) +
               ca.get(Calendar.SECOND);
        String currentdir = System.getProperty("user.dir");

        int wmlflag = 0;
        if (FileType.equals("wml") || FileType.equals("") ||
            FileType.equals("null")) {
            wmlflag = 1;
            if (xhtml_wml == 1) {
                LogStr[11] = "df" + dstr + ".wbxml";
            } else {
                LogStr[11] = "df" + dstr + ".wml";
            }
            wbfile = currentdir + "\\df" + dstr + ".wbxml";
            wmlfile = currentdir + "\\df" + dstr + ".wml";
            // htmlfile=currentdir + "\\df" + dstr + ".html";


        } else {
            if (FileType.equals("JAR") || FileType.equals("WAR") ||

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -