【问题标题】:HttpURLConnection POST Request Returns No ValueHttpURLConnection POST 请求无返回值
【发布时间】:2010-12-26 18:17:21
【问题描述】:

我在处理不返回数据的 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=\"chalkboard.status@fdq.att.com\" 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 循环中,直到将消息发送到锁定我的应用程序的机器人。如何确定服务器是否没有响应?

【问题讨论】:

    标签: java webrequest httpurlconnection


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = 新的 HttpPost(UPLOAD_URL); MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 尝试 { reqEntity.addPart("param1", new StringBody("yes")); reqEntity.addPart("param2", new StringBody("no")); httppost.setEntity(reqEntity); LOG.debug("执行请求" + httppost.getRequestLine()); HttpResponse 响应 = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); 字符串 urlImageShack = null; 如果(resEntity!= null){ // Imageshack 返回的 XML 字符串页面 = EntityUtils.toString(resEntity); LOG.debug("返回:" + page); }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多