HttpURLConnection POST 请求不返回任何值

发布于 2024-10-09 20:35:33 字数 2253 浏览 5 评论 0原文

我在处理不返回数据的 HTTP URL 连接时遇到问题。我正在使用的代码如下...它基本上是一个消息传递客户端,此方法获取发送给虚假用户(机器人)的任何消息,然后我能够操纵消息,查找关键字并从机器人。

public void getMessages(String bot)
{
    xml = "" +
          "xmlMessage=<message type=\"comet.get.message.updates\" "
          + "id=\"" + bot + "\" "
          + "password=\"" + password + "\" />";

    // Replace spaces (partial url encode).
    xml = xml.replace(" ", "%20");

    String serverResponse = "";

    try
    {
        // Build URL
        url = new URL(botUrl);
        request = (HttpURLConnection)url.openConnection();

        // Set the Request Method.
        request.setRequestMethod("POST");
        request.setDoInput(true);
        request.setDoOutput(true);
        request.setUseCaches(false);
        request.setAllowUserInteraction(false);
        request.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");

        out = request.getOutputStream();
        writer = new OutputStreamWriter(out, "UTF-8");
        writer.write(xml);
        writer.close();

        String temp = "";

        buff = new BufferedReader ( new InputStreamReader ( request.getInputStream() ) );
        while ( (temp = buff.readLine()) != null )
        {
            serverResponse = serverResponse + temp;
        }

//     XML RESPONSE EXAMPLE
//            xml = "- <message type=\"comet.message.updates\" id=\"[email protected]\" count=\"2\">z" +
//                        "- <contact id=\"jy5740\" />" +
//                             "<statement text=\"test\" from=\"jy5740\" />" +
//                             "<statement text=\"testing 123\" from=\"jy5740\" />" +
//                          "</contact>" +
//                     "</message>";
    }
    catch (MalformedURLException ex)
    {
        System.out.println("Bad URL: " + ex);
    }
    catch (IOException ex)
    {
        System.out.println("Connection error: " + ex);
    }

    // do stuff with the serverResponse string

如果在方法调用时尚未收到消息,则该方法可以正常工作。问题是自上次检查以来没有任何消息。该方法只是停留在 while 循环中,直到一条消息发送到锁定我的应用程序的机器人。如何判断服务器是否没有响应?

I am having an issue handling HTTP URL Connections that do not return data. The code I am using is below... It is basically a messaging client, and this method gets any messages sent to a fake user (bot) and then I am able to manipulate the messages, look for key words, and respond from the bot.

public void getMessages(String bot)
{
    xml = "" +
          "xmlMessage=<message type=\"comet.get.message.updates\" "
          + "id=\"" + bot + "\" "
          + "password=\"" + password + "\" />";

    // Replace spaces (partial url encode).
    xml = xml.replace(" ", "%20");

    String serverResponse = "";

    try
    {
        // Build URL
        url = new URL(botUrl);
        request = (HttpURLConnection)url.openConnection();

        // Set the Request Method.
        request.setRequestMethod("POST");
        request.setDoInput(true);
        request.setDoOutput(true);
        request.setUseCaches(false);
        request.setAllowUserInteraction(false);
        request.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");

        out = request.getOutputStream();
        writer = new OutputStreamWriter(out, "UTF-8");
        writer.write(xml);
        writer.close();

        String temp = "";

        buff = new BufferedReader ( new InputStreamReader ( request.getInputStream() ) );
        while ( (temp = buff.readLine()) != null )
        {
            serverResponse = serverResponse + temp;
        }

//     XML RESPONSE EXAMPLE
//            xml = "- <message type=\"comet.message.updates\" id=\"[email protected]\" count=\"2\">z" +
//                        "- <contact id=\"jy5740\" />" +
//                             "<statement text=\"test\" from=\"jy5740\" />" +
//                             "<statement text=\"testing 123\" from=\"jy5740\" />" +
//                          "</contact>" +
//                     "</message>";
    }
    catch (MalformedURLException ex)
    {
        System.out.println("Bad URL: " + ex);
    }
    catch (IOException ex)
    {
        System.out.println("Connection error: " + ex);
    }

    // do stuff with the serverResponse string

The method works perfectly if there are messages that have not been received at the time of the method call. The problem is when there have not been any messages since the last check. The method just stays in the while loop until a message is sent to the bot locking my app. How do I determine if there was no response from the server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七色彩虹 2024-10-16 20:35:33

comet 的要点是“长轮询”——您发出请求,直到收到真正的响应或超时后,请求才会完成。换句话说,如果没有消息,我希望对 readLine 的调用会阻塞很长时间。

如果您需要发出一个不会需要很长时间才能超时的请求,那么您需要在某处指定超时(可能在 HTTP 级别,也可能在 XML 中)内容)或使用不同的调用来开始 - 很可能有一种不同类型的消息用于非挂起请求。

The point of comet is "long polling" - that you make a request and it won't complete until there's a real response, or until it times out. In other words, if there are no messages I'd expect the call to readLine to block for a long time.

If you need to make a request which won't take a long time to time-out, you'll either need to specify a time-out somewhere (possibly at the HTTP level, possibly within the XML content) or use a different call to start with - there may well be a different kind of message used for non-hanging requests.

み青杉依旧 2024-10-16 20:35:33

不知道,但这是我的 HTTP Post 代码:

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(UPLOAD_URL);

            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

            try {
                reqEntity.addPart("param1", new StringBody("yes"));
                reqEntity.addPart("param2", new StringBody("no"));

                httppost.setEntity(reqEntity);

                LOG.debug("executing request " + httppost.getRequestLine());
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity resEntity = response.getEntity();

                String urlImageShack = null;
                if (resEntity != null) {
                    // XML returned by Imageshack
                    String page = EntityUtils.toString(resEntity);
                    LOG.debug("It return: " + page);
                }

No idea, but here is my code for HTTP Post:

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(UPLOAD_URL);

            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

            try {
                reqEntity.addPart("param1", new StringBody("yes"));
                reqEntity.addPart("param2", new StringBody("no"));

                httppost.setEntity(reqEntity);

                LOG.debug("executing request " + httppost.getRequestLine());
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity resEntity = response.getEntity();

                String urlImageShack = null;
                if (resEntity != null) {
                    // XML returned by Imageshack
                    String page = EntityUtils.toString(resEntity);
                    LOG.debug("It return: " + page);
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文