php – 使用GWT从url获取内容

我需要使用Google Web Toolkit(GWT)从url(http://myweb.com/test.php)获取内容
我试过这个:

GWT:

    RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, 
"http://myweb.com/test.PHP?id=65472");
     try {
        rb.sendRequest(null, new RequestCallback() {
             public void one rror(final Request request, final Throwable exception) {
                 Window.alert(exception.getMessage());
             }
             public void onResponseReceived(final Request request, final Response response) {
                 //do something
                 GWT.log("response: (" + response.getText() + ")", null);
             }
        });
     } catch (final Exception e) {
             Window.alert(e.getMessage());
     }

PHP

<?PHP

$var1 = $_GET["id"];
echo "The id is: ".$var1;

?>

但它总是返回空 – >响应();

解决方法:

您可以在托管模式下使用代理Servlet.

如果在web.xml中的正确路径下注册提供的servlet,并在servlet中设置PHP服务器的正确主机/端口,则可以在托管模式下发布到开发服务器.

注意只实施POST …..

public class ProxyServlet extends HttpServlet {

    /**
     * 
     */
    private static final long   serialVersionUID    = 8L;

    private static final String targetServer        = "localhost";
    private static final int    targetPort          = 80;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws servletexception, IOException {
        System.err.println("GET NOT implemented!");
        resp.sendError(HttpStatus.ORDINAL_501_Not_Implemented);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws servletexception, IOException {
        handleRequest(req, res);
    }

    @SuppressWarnings("unchecked")
    protected void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws servletexception, IOException {

        final StringBuffer file = new StringBuffer();

        file.append(req.getRequestURI());
        if (req.getQueryString() != null) {
            file.append("?" + req.getQueryString());
        }

        HttpURLConnection conn = (HttpURLConnection) new URL("http", targetServer, targetPort, file.toString()).openConnection();

        conn.setRequestMethod("POST");

        // copy header
        // copy headers
        Enumeration headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = (String) headerNames.nextElement();
            String value = req.getHeader(name);

            value = value.replace(":8080", ":80");

            conn.addRequestProperty(name, value);
        }

        conn.setDoOutput(true);

        try {
            OutputStream out = conn.getoutputStream();
            fastStreamcopy(req.getInputStream(), out);
        } catch (Exception e) {
            resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Server down");
            return;
        }

        try {
            int code = conn.getResponseCode();
            String msg = conn.getResponseMessage();

            System.out.println("code: " + code + ", msg: " + msg);
            resp.setStatus(code, msg);
            Map<String, List<String>> headerFields = conn.getHeaderFields();

            for (Map.Entry<String, List<String>> e : headerFields.entrySet()) {
                for (String val : e.getValue()) {
                    if (e.getKey() != null)
                        resp.addHeader(e.getKey(), val);
                }
            }

            // Try normal stream, then error stream and accept if remote host says that there is no content.
            try {
                fastStreamcopy(conn.getInputStream(), resp.getoutputStream());
            } catch (IOException e) {
                try {
                    fastStreamcopy(conn.getErrorStream(), resp.getoutputStream());
                } catch (Exception e1) {
                    if (conn.getContentLength() == 0) {
                        // That ok - nothing there
                    } else {
                        throw e;
                    }

                }
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
            throw e;
        }

    }

    public static void fastStreamcopy(InputStream input, OutputStream output) throws IOException {
        final ReadableByteChannel inputChannel = Channels.newChannel(input);
        final WritableByteChannel outputChannel = Channels.newChannel(output);
        // copy the channels
        fastChannelcopy(inputChannel, outputChannel);
    }

    public static void fastChannelcopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException {
        final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
        while (src.read(buffer) != -1) {
            // prepare the buffer to be drained
            buffer.flip();
            // write to the channel, may block
            dest.write(buffer);
            // If partial transfer, shift remainder down
            // If buffer is empty, same as doing clear()
            buffer.compact();
        }
        // EOF will leave buffer in fill state
        buffer.flip();
        // make sure the buffer is fully drained.
        while (buffer.hasRemaining()) {
            dest.write(buffer);
        }
    }
}

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...