-
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;
}
}
2009年7月18日 11:15
2个答案 按时间排序 按投票排序
-
'The specified call count is not a number'
少发送了count参数吧,或count参数不是数字,
count不是字符串类型的,要是数字2009年7月18日 12:29
-
看明白了吗?最关键的一点,请求的时候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"),
遇到问题先Google2009年7月18日 11:48
相关推荐
一个使用HttpClient访问WS 的例子,包含相关jar包
本demo有两个例子test是作为服务端开启的,httpclient是客户端调用的,模拟服务端的test在启动后是没有界面的,要加上id才能看到json数据,httpclient的HttpClient33.java是测试类,最后的结果是接受到一个对象。...
httpclient调用webservice.txt
希望用http协议的能可以帮到大家!很有使用价值!大家可以看看!
使用httpclient调用公安部接口 并接受返回的字符串转换为图片
webservice调用实例,通过HttpClient调用,包括完整参数调用,项目通过maven搭建
一个使用HttpClient调用接口的例程,接口是从网上找来的,只支持get方式提交,返回json格式,此例程的编码方式为GBK,我写了注释
本教程是继使用HttpClient调用Web API只有的补充教程,主要讲解了如何在WPF中异步调用Web API且不阻塞主线程。
复制进eclipse即可用。main方法测试
WebApi系列-通过HttpClient来调用WebApi接口.doc
JAVA利用HttpClient进行HTTPS接口调用
springmvc实现httpclient调用服务
以上自己测试是可行的,直接拿过去导入即可,直接用main
httpclient方式调用url
【ASP.NET编程知识】通过HttpClient 调用ASP.NET Web API示例.docx
HTTPClient组件的应用
HttpClient接口调用工具类 (Post请求 get请求 put请求 delete请求 下载就可以用 )
ETL KETTLE 中利用Httpclient 调用webservice接口获取XML数据,并解析XML 数据。 完整的KTR实例