1

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

我可以将 post 请求发送到相应的 URL,但是当我尝试检索响应时,我得到了一半的 HTML 内容,后面跟着“ *Couldn't read CGI input from STDIN.AFTER ALLOC_READ 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;
    }
    }

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

4

2 回答 2

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);
        }
    }
于 2012-11-06T19:48:49.013 回答
0

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

于 2012-11-06T19:58:30.333 回答