【问题标题】:Getting "Couldn't read CGI input from STDIN" while processing Post request using HTTPClient in android在 android 中使用 HTTPClient 处理 Post 请求时出现“无法从 STDIN 读取 CGI 输入”
【发布时间】:2012-11-06 19:40:54
【问题描述】:

我目前正在开发一个android应用程序,提交一个post请求并处理相应的响应。

我能够将发布请求发送到相应的 URL,但是当我尝试检索响应时,我得到了一半的 HTML 内容,后面跟着“*无法从 STDIN 读取 CGI 输入。 AFTER ALLOC_READ 0*"

谁能帮我解决这个问题。

这里是代码sn-ps

private void processRequest(String... params){

        HttpPost post = new HttpPost("http://www.xyz.com");
        HttpParams httpParams = post.getParams();
        pnr = params[i];
        httpParams.setParameter("param1", params[i]);
        //User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1
        httpParams.setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1");

        post.setParams(httpParams);
        HttpClient client = new DefaultHttpClient();

        try {
            HttpResponse response = client.execute(post);

            HttpEntity entity = response.getEntity();


            try {
                processHtmlString(pnr, inputStreamToString(entity.getContent()).toString());
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            client.getConnectionManager().shutdown();
        }
  }


   private String processHtmlString(String pnr, String htmlString) throws Exception{

    int index = 0;
    while(index < htmlString.length()){
        int endIndex = (index + 3000) < (htmlString.length()) ? (index + 3000) : htmlString.length();
        Log.i("HttpHelper1","HTML1 : "+htmlString.substring(index, endIndex));
        index += 3000;
    }
    }

输出是 .........无法从 STDIN 读取 CGI 输入。AFTER ALLOC_READ 0

【问题讨论】:

    标签: java android html post httpclient


    【解决方案1】:

    看起来您没有正确地形成请求(您将参数放入相同的部分标题中,而不是在正文中):试试这个。

        List<NameValuePair> bodyParams = new ArrayList<NameValuePair>();
        bodyParams.add(new BasicNameValuePair("param1", params[i]);
        if (bodyParams.size() > 0) {
            try {
                // Include the request body
                post.setEntity(new UrlEncodedFormEntity(bodyParams));
            } catch (UnsupportedEncodingException e) {
                throw new IllegalStateException("Body parameters produced unsupported encoding?", e);
            }
        }
    

    【讨论】:

    • 非常感谢“forgivegod”。代码运行良好:)
    【解决方案2】:

    不要使用字符串,使用实体本身提供的 InputStream。字符串的长度有限,这可能会导致您的问题。直接从流中读取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多