【问题标题】:Using HTTP Post between 2 Java servlets在 2 个 Java servlet 之间使用 HTTP Post
【发布时间】:2012-07-20 05:12:15
【问题描述】:

我已经设置了一个 java servlet,它接受来自 URL 的参数并让它正常工作:

public class GetThem extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws IOException, ServletException
{
    try {

            double lat=Double.parseDouble(request.getParameter("lat"));
            double lon=Double.parseDouble(request.getParameter("lon"));
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println(lat + " and " + lon);

        } catch (Exception e) {

            e.printStackTrace();
    }
  }       
}

所以访问此链接: http://www.example.com:8080/HttpPost/HttpPost?lat=1&lon=2 会输出:

  "1.0 and 2.0"

我目前正在使用此代码从另一个 java 程序调用它:

try{
            URL objectGet = new URL("http://www.example.com:8080/HttpPost/HttpPost?lat=" + Double.toString(dg.getLatDouble()) + "&lon=" + Double.toString(dg.getLonDouble()));
            URLConnection yc = objectGet.openConnection();
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    yc.getInputStream()));
            in = new BufferedReader(
            new InputStreamReader(
            yc.getInputStream()));
            ...

现在我想更改它,以便不使用 URL 参数将此数据传递给服务器。我想向该服务器发送更大的消息。我知道我需要使用 http post 而不是 http get 来实现这一点,但不知道该怎么做。

我是否需要更改正在接收数据的服务器端的任何内容?在发布此数据的客户端上我需要做什么?

任何帮助将不胜感激。 理想情况下,我想以 JSON 格式发送这些数据。

【问题讨论】:

    标签: java http servlets http-post


    【解决方案1】:

    下面是“java HTTP POST example”在谷歌中找到的第一个链接的示例。

    try {
        // Construct data
        StringBuilder dataBuilder = new StringBuilder();
        dataBuilder.append(URLEncoder.encode("key1", "UTF-8")).append('=').append(URLEncoder.encode("value1", "UTF-8")).
           append(URLEncoder.encode("key2", "UTF-8")).append('=').append(URLEncoder.encode("value2", "UTF-8"));
    
        // Send data
        URL url = new URL("http://hostname:80/cgi");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(dataBuilder.toString());
        wr.flush();
    
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            // Process line...
        }
        wr.close();
        rd.close();
    } catch (Exception e) {
    }
    

    【讨论】:

    • 感谢您的回复。是的,也许我在这里发布的有点过早。我对 Apache HTTPClient 和 URLConnection 感到困惑,使用哪一个等等。我也遇到了麻烦,因为 HTTPClient 的 API 从 V3 更改为 V4,但我使用的是 V3 的教程。我现在使用 URLConnection 让它工作了。
    【解决方案2】:

    我认为您应该使用 HTTPClient 而不是处理连接和流。检查http://hc.apache.org/httpclient-3.x/tutorial.html

    【讨论】:

    • 您好,感谢您的回复。我研究了这个(第 4 版教程),但决定使用 URLConnection,因为:1)我只是在学习如何自己编程和处理连接,这将使我比使用 HTTPClient 更好地理解。 2)我也在开发一个Android应用程序。 HTTPClient 已被 google 贬低,他们推荐使用 URLConnection。我想在 android 和其他 servlet 之间以相同的方式连接以保持简单。
    猜你喜欢
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 2018-11-14
    • 1970-01-01
    相关资源
    最近更新 更多