有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在安卓中使用HTTPClient处理Post请求时“无法从STDIN读取CGI输入”

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

我可以点击相应URL的post请求,但当我尝试检索响应时,我会得到一半的HTML内容,后跟“*无法从STDIN读取CGI输入。ALLOC_读取0后*

谁能帮我解决这个问题

下面是代码片段

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;
    }
    }

输出是 ...........无法从标准输入读取CGI输入。在ALLOC_读取0之后


共 (2) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

    看起来您没有正确地生成请求(您正在将参数放入相同的节头中,而不是正文中):试试这个

        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);
            }
        }