0 0

httpclient 调用DWR应用时发生The specified call count is not a number 错误25

用的是htppclient 4.0访问DWR时发生。
throw 'allowScriptTagRemoting is false.';
//#DWR-REPLY
if (window.dwr) dwr.engine._remoteHandleBatchException({ name:'org.directwebremoting.extend.ServerException', message:'The specified call count is not a number' });
else if (window.parent.dwr) window.parent.dwr.engine._remoteHandleBatchException({ name:'org.directwebremoting.extend.ServerException', message:'The specified call count is not a number' });


代码如下:

import java.io.InputStream;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
import java.util.Set;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  
 
import org.apache.http.Header;  
import org.apache.http.HttpEntity;  
import org.apache.http.HttpResponse;  
import org.apache.http.HttpVersion;  
import org.apache.http.NameValuePair;  
import org.apache.http.client.CookieStore;  
import org.apache.http.client.entity.UrlEncodedFormEntity;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.conn.ClientConnectionManager;  
import org.apache.http.conn.params.ConnManagerParams;  
import org.apache.http.conn.scheme.PlainSocketFactory;  
import org.apache.http.conn.scheme.Scheme;  
import org.apache.http.conn.scheme.SchemeRegistry;  
import org.apache.http.cookie.Cookie;  
import org.apache.http.impl.client.DefaultHttpClient;  
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;  
import org.apache.http.impl.cookie.BasicClientCookie;  
import org.apache.http.message.BasicHeader;  
import org.apache.http.message.BasicNameValuePair;  
import org.apache.http.params.BasicHttpParams;  
import org.apache.http.params.HttpParams;  
import org.apache.http.params.HttpProtocolParams;  
import org.apache.http.protocol.HTTP;  
  
public class DwrTest {  
private DefaultHttpClient httpclient;  
      
    private Header[] headers = {new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; " +  
            "Windows NT 5.1; SV1; .NET CLR 2.0.50727; CIBA)"),   
            new BasicHeader("Accept-Language", "zh-cn"),   
            new BasicHeader("Accept", " image/gif, image/x-xbitmap, image/jpeg, " +  
                    "image/pjpeg, application/x-silverlight, application/vnd.ms-excel, " +  
                    "application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"),   
            new BasicHeader("Content-Type", "application/x-www-form-urlencoded"),   
            new BasicHeader("Accept-Encoding", "gzip, deflate")};  
      
    private Cookie cookie;  
      
    /**  
     * 登录请求的地址  
     */  
    private String action;  
      
    /** 
     * 用户名 
     */ 
    private String username;  
      
    /** 
     * 密码 
     */ 
    private String password;  
      
    /** 
     * 请求的其他参数 
     */ 
    private Map<String, String> map = new HashMap<String, String>();  
      
    public static void main(String[] args) throws Exception {  
        String action = "http://localhost:8080/dwrDemo/index.jsp";  
       // String username = "admin";  
        //String password = "admin";  
        Map<String, String> map = new HashMap<String, String>();  
        //map.put("yourName", username);  
        //map.put("&j_password", password);  
          
        DwrTest dwrTest = new DwrTest();  
        dwrTest.setAction(action);  
       // dwrTest.setMap(map);  
        String html = dwrTest.login();  // 登录  
        System.out.println(html);  
          
        action = "http://localhost:8080/dwrDemo/js/engine.js";  
        map.clear();  
        dwrTest.setAction(action);  
        html = dwrTest.execute();  
       // System.out.println(html);  
        String regEx = "dwr\\.engine\\._origScriptSessionId = \"(\\w+)\"";  
        String scriptSessionId = null;  
        Pattern p = Pattern.compile(regEx);  
        Matcher m = p.matcher(html);  
        while (m.find()) {  
            scriptSessionId = m.group(1);  
        }  
          System.out.println(scriptSessionId);
        DefaultHttpClient httpClient = dwrTest.getHttpclient();  
        CookieStore cookes = httpClient.getCookieStore();  
        List<Cookie> list = cookes.getCookies();  
        System.out.println(list.size()+"CCCCCCCCC");
        String httpSessionId = "";  
        for (Cookie cookie : list) {  
            String cookieName = cookie.getName(); 
            //System.out.println(cookieName+"==========");
            if ("JSESSIONID".equals(cookieName)) {  
                httpSessionId = cookie.getValue();  
                System.out.println(httpSessionId);
            }  
        }  
          
        Header[] headers = {new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; " +  
        "Windows NT 5.1; SV1; .NET CLR 2.0.50727; CIBA)"),   
        new BasicHeader("Accept-Language", "zh-cn"),  
        new BasicHeader("Content-Type", "text/plain"),
        new BasicHeader("X_REQUESTED_WITH", "XMLHttpRequest"),
        new BasicHeader("Accept-Encoding", "gzip, deflate")};
        //new BasicHeader("X_REQUESTED_WITH", "XMLHttpRequest");
        action = "http://localhost:8080/dwrDemo/dwr/call/plaincall/service.HelloWorld.dwr";  
        map.clear();  
        map.put("model", "CNPC_GWLIST");  
        map.put("httpSessionId", httpSessionId);  
        map.put("scriptSessionId", scriptSessionId);  
        map.put("c0-scriptName", "Service");  
        map.put("c0-methodName", "HelloWorld");  
        map.put("&c0-id", "0");  
        map.put("&c0-param0", "string:gwtype%3D'fw'");  
        map.put("&c0-param1", "string:");  
        map.put("&c0-param2", "string:admin");  
        map.put("&c0-param3", "number:0");  
        map.put("&c0-param4", "string:15");  
        map.put("&c0-param5", "string:SENDTIME%20DESC");  
        map.put("&batchId", "0");  
        dwrTest.setAction(action);  
        dwrTest.setMap(map);  
        dwrTest.setHeaders(headers);  
        html = dwrTest.execute();  

        System.out.println(html);  
    }  
      
    /** 
     * 初始化 
     */ 
    public DwrTest() {  
        HttpParams params = new BasicHttpParams();  
        ConnManagerParams.setMaxTotalConnections(params, 100);  
        ConnManagerParams.setTimeout(params, 1000);  
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);  
        // HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_0);  
        SchemeRegistry schemeRegistry = new SchemeRegistry();  
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));  
        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);  
          
        httpclient = new DefaultHttpClient(cm, params);  
        httpclient = new DefaultHttpClient();  
    }  
      
    /** 
     * 初始化 
     *  
     * @param action 
     * @param username 
     * @param passwork 
     * @param map 
     */ 
    public DwrTest(String action,   
            String username, String passwork, Map<String, String> map) {  
 
        HttpParams params = new BasicHttpParams();  
        ConnManagerParams.setMaxTotalConnections(params, 100);  
        ConnManagerParams.setTimeout(params, 10000);  
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);  
        // HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_0);  
        SchemeRegistry schemeRegistry = new SchemeRegistry();  
        schemeRegistry.register(  
                new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));  
        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);  
          
        httpclient = new DefaultHttpClient(cm, params);  
//      httpclient = new DefaultHttpClient();  
        this.action = action;  
        this.username = username;  
        this.password = passwork;  
        if (map == null) {  
            map = new HashMap<String, String>();  
        }  
        this.map = map;  
    }  
      
    /** 
     * 初始化 
     *  
     * @param httpclient 
     */ 
    public DwrTest(DefaultHttpClient httpclient) {  
        this.httpclient = httpclient;  
    }  
      
    /** 
     * 初始化 
     *  
     * @param httpclient 
     * @param username 
     * @param passwork 
     */ 
    public DwrTest(DefaultHttpClient httpclient, String action,   
            String username, String passwork, Map<String, String> map) {  
        this.httpclient = httpclient;  
        this.action = action;  
        this.username = username;  
        this.password = passwork;  
        if (map == null) {  
            map = new HashMap<String, String>();  
        }  
        this.map = map;  
    }  
      
    /** 
     * 登录 
     *  
     * @param httpclient 
     * @return 
     * @throws Exception 
     */ 
    public String login() throws Exception {  
        HttpPost httpost = new HttpPost(action);  // 初始化Post  
 
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();  // 构建参数  
        nvps.add(new BasicNameValuePair("yourName", username));  
       // nvps.add(new BasicNameValuePair("password", password));  
        // 增加其他的参数  
        Set<String> set = map.keySet();  
        for (String string : set) {  
            nvps.add(new BasicNameValuePair(string, map.get(string)));  
        }  
          
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));  
        httpost.setHeaders(headers);  
          
        HttpResponse response = httpclient.execute(httpost);  // 运行action  
        HttpEntity entity = response.getEntity();   // 获得实体  
 
        InputStream in = entity.getContent();  // 获得实体的内容  
        StringBuffer   out   =   new   StringBuffer();  
        byte[] b = new byte[4096];  
        for(int n; (n = in.read(b)) != -1;) {  
            out.append(new   String(b, 0, n));  
        }  
 
        if (entity != null) {  
            entity.consumeContent();  
        }  
          
//      取出页面上返回的LtpaToken Cookie值。  
        String html = out.toString();  
        String regEx = "(\"LtpaToken=(.+)\")";  
        String ltpaToken = null;  
        Pattern p = Pattern.compile(regEx);  
        Matcher m = p.matcher(html);  
        while (m.find()) {  
            ltpaToken = m.group(1);  
            if (ltpaToken.length() > 11) {  
                ltpaToken = ltpaToken.substring(11);  
                ltpaToken = ltpaToken.substring(0, ltpaToken.lastIndexOf("\""));  
            }  
        }  
        BasicClientCookie cookie = new BasicClientCookie("LtpaToken", ltpaToken);  
        cookie.setDomain(".tj.unicom.local");  
        cookie.setPath("/");  
        CookieStore cookies = httpclient.getCookieStore();  
        cookies.addCookie(cookie);  
        httpclient.setCookieStore(cookies);  
          
        return html;  
    }  
      
    public String execute() throws Exception {  
        HttpPost httpost = new HttpPost(action);  // 初始化Post  
 
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();  // 构建参数  
        // 增加其他的参数  
        Set<String> set = map.keySet();  
        for (String string : set) {  
            nvps.add(new BasicNameValuePair(string, map.get(string)));  
        }  
          
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));  
        httpost.setHeaders(headers);  
          
        HttpResponse response = httpclient.execute(httpost);  // 运行action  
        HttpEntity entity = response.getEntity();   // 获得实体  
 
        InputStream in = entity.getContent();  // 获得实体的内容  
        StringBuffer   out   =   new   StringBuffer();  
        byte[] b = new byte[4096];  
        for(int n; (n = in.read(b)) != -1;) {  
            out.append(new   String(b, 0, n));  
        }  
 
        if (entity != null) {  
            entity.consumeContent();  
        }  
          
        String html = out.toString();  
        return html;  
    }  
      
    @SuppressWarnings("unused")  
    private void print(InputStream in) throws Exception {  
        StringBuffer   out   =   new   StringBuffer();  
        byte[] b = new byte[4096];  
        for(int n; (n = in.read(b)) != -1;) {  
            out.append(new   String(b, 0, n));  
        }  
        System.out.println("-------------------------------------------");  
        System.out.println(out.toString());  
        System.out.println("-------------------------------------------");  
          
    }  
      
    @SuppressWarnings("unused")  
    private String toStr(InputStream in) throws Exception {  
        StringBuffer   out   =   new   StringBuffer();  
        byte[] b = new byte[4096];  
        for(int n; (n = in.read(b)) != -1;) {  
            out.append(new   String(b, 0, n));  
        }  
        String str = out.toString();  
        return str;  
    }  
 
    public String getAction() {  
        return action;  
    }  
 
    public String getUsername() {  
        return username;  
    }  
 
    public String getPassword() {  
        return password;  
    }  
      
    public Map<String, String> getMap() {  
        return map;  
    }  
 
    public void setAction(String action) {  
        this.action = action;  
    }  
 
    public void setUsername(String username) {  
        this.username = username;  
    }  
 
    public void setPassword(String password) {  
        this.password = password;  
    }  
      
    public void setMap(Map<String, String> map) {  
        this.map = map;  
    }  
 
    public DefaultHttpClient getHttpclient() {  
        return httpclient;  
    }  
 
    public Cookie getCookie() {  
        return cookie;  
    }  
 
    public void setHttpclient(DefaultHttpClient httpclient) {  
        this.httpclient = httpclient;  
    }  
 
    public void setCookie(Cookie cookie) {  
        this.cookie = cookie;  
    }  
      
    public void setHeaders(Header[] headers) {  
        this.headers = headers;  
    }  
      
    public Header[] getHeaders() {  
        return headers;  
    }  

DWR 
2009年7月18日 11:15

2个答案 按时间排序 按投票排序

0 0

'The specified call count is not a number'

少发送了count参数吧,或count参数不是数字,

count不是字符串类型的,要是数字

2009年7月18日 12:29
0 0

看明白了吗?最关键的一点,请求的时候Content-Type 一定写成:text/plain,千万不能是:application/x-www-form-urlencoded

http://hk109.ycool.com/post.3008834.html

new BasicHeader("Content-Type", "application/x-www-form-urlencoded"),   

遇到问题先Google

2009年7月18日 11:48

相关推荐

    HttpClient 调用WebService示例

    在本文中,我们将深入探讨如何使用HttpClient调用WebService。 首先,调用WebService通常涉及SOAP(Simple Object Access Protocol)或RESTful API。HttpClient可以处理这两种类型的Web服务。在本示例中,我们假设...

    httpClient调用webservice接口

    ### 使用HttpClient调用WebService接口详解 #### 一、概述 在现代软件开发中,Web服务(尤其是WebService)作为实现系统间交互的一种重要手段被广泛采用。WebService提供了通过HTTP协议进行远程过程调用的能力,...

    如何在WPF应用程序中通过HttpClient调用Web API

    在WPF(Windows Presentation Foundation)应用程序中,使用HttpClient调用Web API是一项常见的任务,尤其是在进行数据交互时。本文将深入探讨如何在WPF应用中利用HttpClient进行异步操作,以避免阻塞主线程,提高...

    httpClient调用wcf

    6. **错误处理**:在调用过程中,可能出现网络问题、服务异常等错误,需要适当地捕获和处理这些异常。 7. **依赖库**:压缩包中包含的其他JAR文件(如`commons-collections.jar`、`commons-lang.jar`等)是Apache ...

    HttpClient调用第三方接口

    使用httpclient调用公安部接口 并接受返回的字符串转换为图片

    webservice调用实例,通过HttpClient调用

    Web服务调用是软件开发中的常见任务,尤其是当我们需要跨系统、跨平台集成不同应用程序时。本示例将深入探讨如何使用Apache HttpClient库在Java环境中调用Web服务,特别是通过Maven构建项目的方式进行。HttpClient是...

    一个使用HttpClient调用天气预报接口的例程

    在这个例程中,我们将深入探讨如何利用HttpClient来调用一个天气预报接口,并解析返回的JSON数据。以下是一些关键知识点: 1. **HttpClient库**:HttpClient是Apache提供的一个开源库,它允许开发者构建HTTP客户端...

    httpclient调用接口demo

    本demo有两个例子test是作为服务端开启的,httpclient是客户端调用的,模拟服务端的test在启动后是没有界面的,要加上id才能看到json数据,httpclient的HttpClient33.java是测试类,最后的结果是接受到一个对象。...

    httpclient调用webservice.txt

    httpclient调用webservice.txt

    httpClient调用javaeye API验证用户

    这个库在处理网络通信,特别是进行RESTful API调用时非常有用。在这个场景中,我们使用HttpClient来调用JavaEye API来验证用户。JavaEye可能是一个提供用户认证服务的平台,而API则提供了验证用户身份的功能。 以下...

    WebApi系列-通过HttpClient来调用WebApi接口.doc

    ### WebApi系列-通过HttpClient来调用WebApi接口 #### 一、概述 在现代Web开发中,Web API已经成为一种非常流行的模式,用于提供服务端与客户端之间的数据交互。随着.NET Framework以及.NET Core的发展,微软提供...

    Httpclient调用WebService所需jar包

    在实际应用中,导入这些jar包后,你可以按照以下步骤使用HttpClient调用WebService: 1. **创建HttpClient实例**:首先,需要创建一个HttpClient对象,这将是所有HTTP请求的起点。 2. **设置HTTP参数**:你可以配置...

    【ASP.NET编程知识】通过HttpClient 调用ASP.NET Web API示例.docx

    ASP.NET 编程知识 - 通过 HttpClient 调用 ASP.NET Web API 示例 在本文中,我们将学习如何使用 HttpClient 调用 ASP.NET Web API。HttpClient 是一个强大的 HTTP 客户端库,能够发送 HTTP 请求并接收响应。在 ASP...

    httpClient实例httpClient调用 http/https实例 忽略SSL验证

    这个实例主要涉及如何配置HttpClient来忽略SSL(Secure Socket Layer)验证,这对于在开发和测试环境中处理自签名证书或未认证的服务器非常有用。以下将详细介绍HttpClient的使用以及如何进行SSL验证的忽略。 首先...

    JAVA利用HttpClient进行HTTPS接口调用

    当涉及到HTTPS接口调用时,HttpClient可以处理加密通信,确保数据传输的安全性。在这个主题中,我们将深入探讨如何在Java中使用HttpClient来实现HTTPS接口调用。 首先,我们需要理解HTTPS协议。HTTPS是HTTP(超文本...

    HttpClient问题:The server failed to respond with a valid HTTP resp

    在使用Apache HttpClient进行HTTP通信时,可能会遇到"HttpClient问题:The server failed to respond with a valid HTTP resp"这样的异常。这个错误通常表示服务器未能返回一个有效的HTTP响应,这可能是由多种原因...

    springmvc实现httpclient调用服务

    本篇文章将详细介绍如何在Spring MVC应用中利用HttpClient来调用外部服务。 首先,让我们理解Spring MVC的基本工作流程。Spring MVC接收HTTP请求,通过DispatcherServlet进行分发,然后由Controller层处理业务逻辑...

    httpClient调用远程接口获取数据到本地文件夹

    在这个例子中,我们使用HttpClient来调用远程接口,从电信公司的网站获取可用手机号码的数据,并将其存储到本地文件系统。以下是对这个过程的详细解释: 1. **HttpClient的引入与配置**: 首先,我们需要在项目中...

Global site tag (gtag.js) - Google Analytics