简体   繁体   English

Java-客户端如何从HttpServlet获得响应

[英]Java - how can client get reponse from HttpServlet

My client does not implement the HttpServlet interface. 我的客户端未实现HttpServlet接口。 It connects to an HttpServlet running remotely 它连接到远程运行的HttpServlet

 url = new URL("http://tomcat-location:8180/myContext");

This is what it uses to send messages to the servlet. 这就是它用来将消息发送到servlet的方式。 But how can it get back responses? 但是,它如何获得回复? When I try to read from this location, I am reading the content of the specified page. 当我尝试从该位置读取内容时,我正在读取指定页面的内容。 Maybe my whole approach is wrong and this is not how the client and the servlet should talk to each other? 也许我的整个方法是错误的,这不是客户端和Servlet应该如何相互通信? How can I make them talk in a simple way? 如何让他们以简单的方式交谈? It seems that using the URL, they are communicating by posting and reading on that page. 似乎他们使用该URL通过在该页面上发布和阅读进行交流。 Is there any other way they can talk without writing on the page? 他们可以在不写页面的情况下进行交谈吗? Thanks 谢谢

Try this: 尝试这个:

public static String getURLData( String url ) {

    // creates a StringBuilder to store the data
    StringBuilder out = new StringBuilder();

    try {

        // creating the URL
        URL u = new URL( url );

        // openning a connection
        URLConnection uCon = u.openConnection();

        // getting the connection's input stream
        InputStream in = uCon.getInputStream();

        // a buffer to store the data
        byte[] buffer = new byte[2048];


        // try to insert data in the buffer until there is data to be read
        while ( in.read( buffer ) != -1 ) {

            // storing data...
            out.append( new String( buffer ) );

        }

        // closing the input stream
        in.close();            

        // exceptions...  
    } catch ( MalformedURLException exc )  {

        exc.printStackTrace();

    } catch ( IOException exc ) {

        exc.printStackTrace();

    } catch ( SecurityException exc ) {

        exc.printStackTrace();

    } catch ( IllegalArgumentException exc ) {

        exc.printStackTrace();

    } catch ( UnsupportedOperationException exc ) {

        exc.printStackTrace();

    }

    // returning data
    return out.toString();

}

If you need to work with a proxy you will need to do some more work, since you will need to authenticate. 如果您需要使用代理,则需要进行更多工作,因为您需要进行身份验证。 Here you can read something about it: How do I make HttpURLConnection use a proxy? 在这里您可以阅读有关它的内容: 如何使HttpURLConnection使用代理?

If you want a client that works like a browser, you can try the HttpClient from Apache HttpComponentes 如果您想要一个像浏览器一样工作的客户端,则可以尝试使用Apache HttpComponentes中的HttpClient。

Now, if you need a behavior where the server notifies the client, so you will need to use another approaches, like creating your own server and working with sockets. 现在,如果您需要服务器通知客户端的行为,那么您将需要使用其他方法,例如创建自己的服务器和使用套接字。 If you work with a regular browser you can use websockets, but it seems that is not your case. 如果您使用常规浏览器,则可以使用websockets,但事实并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM