silverlight json获取 Java发布的服务传递过来的JsonObject数据,怎么解析啊

{1}", typeof(Z).FullName, jsonResault));
//using (MemoryStream ms = new MemoryStream())
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Z));
serializer.WriteObject(ms, obj_parameter);
ms.Position = 0;
using (StreamReader reader = new StreamReader(ms, Encoding.UTF8))
jsonResault = reader.ReadToEnd();
DebugLog(String.Format("BxuHttpUtil 序列化{0}对象为Json-->{1}", typeof(Z).FullName, jsonResault));
return jsonR
/// 反序列化JSON字符串为C#的类,使用silverlight原生的DataContractJsonSerializer,需要引用System.ServiceModel.Web
public K getObjectFromJsonStr(string jsonStr)
K obj = default(K);
DebugLog(String.Format("BxuHttpUtil 反序列化{0}的Json字串-->{1}", typeof(K).FullName, jsonStr));
Debug.WriteLine("序列换开始时间:->" + DateTime.Now);
obj = JsonConvert.DeserializeObject(jsonStr, jsSetting);
Debug.WriteLine("序列换结束时间:->" + DateTime.Now);
//using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(K));
obj = (K)serializer.ReadObject(ms);
#endregion
#region 返回经过JSON为桥梁的对象结果的调用实现
public void postForm(Uri uri, string form_parameter, HttpObjectResultCallBack callBackMethod)
//_resultCallBack = callBackM
webClient.Headers["Content-Type"] = "application/x-www-form-charset=UTF-8";
UserToken userToken = new UserToken();
userToken.eventHandle = new UploadStringCompletedEventHandler(callRemotMethodCompleted);
userToken.callBackHandle = callBackM
webClient.UploadStringCompleted += userToken.eventH //注册回调监听
webClient.UploadStringAsync(uri, "POST", form_parameter, userToken);
public void postJson(Uri uri, string json_parameter, HttpObjectResultCallBack callBackMethod)
//_resultCallBack = callBackM
webClient.Headers["Content-Type"] = "application/charset=UTF-8";
UserToken userToken = new UserToken();
userToken.eventHandle = new UploadStringCompletedEventHandler(callRemotMethodCompleted);
userToken.callBackHandle = callBackM
webClient.UploadStringCompleted += userToken.eventH//注册回调监听
webClient.UploadStringAsync(uri, "POST", json_parameter, userToken);
//public void getObject(Uri uri, BxuHttpUtil.HttpObjectResultCallBack callBackMethod)
webClient.Headers["Content-Type"] = "application/x-www-form-charset=UTF-8";
UserToken userToken = new UserToken();
userToken.callBackHandle = callBackM
userToken.eventHandle = new UploadStringCompletedEventHandler(callOriginalRemotMethodCompleted);
webClient.UploadStringCompleted += userToken.eventH //注册回调监听
webClient.UploadStringAsync(uri, "GET", "", userToken);
private void callRemotMethodCompleted(object sender, UploadStringCompletedEventArgs e)
Exception ex = e.E
string originalResponseString = "";
UserToken userToken = e.UserState as UserToken;
T resultObj = default(T);//泛型默认值,对象给null,基础值类型给0
//上传调用完毕
if (ex != null)
DebugLog("BxuHttpUtil callRemotMethodUseForm 调用失败异常-->" + e.Error.Message);#if DEBUG#else
//实网运行的时候,不抛出任何异常。把异常直接拦截掉。界面清净
webClient.UploadStringCompleted -= userToken.eventH
originalResponseString = e.R
DebugLog("BxuHttpUtil callRemotMethodUseForm 调用成功结果-->" + e.Result.ToString());
resultObj = getObjectFromJsonStr(e.Result.ToString());
catch (Exception e1)
DebugLog(String.Format("BxuHttpUtil callRemotMethodUseForm 结果反序列化失败原始字串通过originalResponseString返回,错误原因-->{0}\n\t\t类型-->{1}\n\t\t原始字串-->{2} -->", e1.Message, typeof(T).FullName, originalResponseString));
//以界面UI线程同步的方式去调用,这样callback方法里更新界面内容就不会报错了
Deployment.Current.Dispatcher.BeginInvoke(() =>
userToken.callBackHandle(resultObj, ex, originalResponseString);
//SynchronizationContext _syncContext = SynchronizationContext.C//线程同步回调
//_syncContext.Post(syncCallBack, e.Result.ToString());
webClient.UploadStringCompleted -= userToken.eventH//注销回调,否则连续调用的话。会出问题的!
//private void syncCallBack(Object resultInfo) {
_resultCallBack(true, true, "ok", resobj);
#endregion
#region 原始字符串方式返回结果的调用实现
public void postForm(Uri uri, string form_parameter, HttpResultCallBack callBackMethod)
//_originalResultCallBack = callBackM
webClient.Headers["Content-Type"] = "application/x-www-form-charset=UTF-8";
UserToken userToken = new UserToken();
userToken.callBackHandle = callBackM
userToken.eventHandle = new UploadStringCompletedEventHandler(callOriginalRemotMethodCompleted);
webClient.UploadStringCompleted += userToken.eventH //注册回调监听
webClient.UploadStringAsync(uri, "POST", form_parameter, userToken);
public void postJson(Uri uri, string json_parameter, HttpResultCallBack callBackMethod)
//_originalResultCallBack = callBackM
webClient.Headers["Content-Type"] = "application/charset=UTF-8";
UserToken userToken = new UserToken();
userToken.callBackHandle = callBackM
userToken.eventHandle = new UploadStringCompletedEventHandler(callOriginalRemotMethodCompleted);
webClient.UploadStringCompleted += userToken.eventH //注册回调监听
webClient.UploadStringAsync(uri, "POST", json_parameter, userToken);
//public void getOriginal(Uri uri, BxuHttpUtil.HttpResultCallBack callBackMethod)
webClient.Headers["Content-Type"] = "application/x-www-form-charset=UTF-8";
UserToken userToken = new UserToken();
userToken.callBackHandle = callBackM
userToken.eventHandle = new UploadStringCompletedEventHandler(callOriginalRemotMethodCompleted);
webClient.UploadStringCompleted += userToken.eventH //注册回调监听
webClient.UploadStringAsync(uri, "GET", "", userToken);
private void callOriginalRemotMethodCompleted(object sender, UploadStringCompletedEventArgs e)
Exception ex = e.E
string originalResponseString = "";
UserToken userToken = e.UserState as UserToken;
//上传调用完毕
if (ex != null)
DebugLog("BxuHttpUtil callRemotMethodUseForm 调用失败异常-->" + e.Error.Message);#if DEBUG#else
//实网运行的时候,不抛出任何异常。把异常直接拦截掉。界面清净
webClient.UploadStringCompleted -= userToken.eventH
originalResponseString = e.R
DebugLog("BxuHttpUtil callRemotMethodUseForm 调用成功结果-->" + e.Result.ToString());
//以界面UI线程同步的方式去调用,这样callback方法里更新界面内容就不会报错了
Deployment.Current.Dispatcher.BeginInvoke(() =>
userToken.callBackHandle(ex, originalResponseString);
webClient.UploadStringCompleted -= userToken.eventH//注销回调,否则连续调用的话。会出问题的!
#endregion
#region DEBUG LOG 控制区
/// 输出DEBUG的log,怀疑模块有问题的时候,调试可以打开。关闭方法到类的最前端注释掉DEBUG_LOG_BXU_ON的声明
/// 打开的方法,放开DEBUG_LOG_BXU_ON宏的注释
private void DebugLog(string loginfo)
{#if DEBUG_LOG_BXU_ON
Debug.WriteLine(loginfo);#endif
#endregion
2.[代码]RMIClient.cs
#if DEBUG//调试的时候如果不需要此类日志输出,请注释该宏#define DEBUG_LOG_BXU_ON#endifusing Susing System.Nusing System.Wusing System.Windows.Cusing System.Windows.Dusing System.Windows.Iusing System.Windows.Iusing System.Windows.Musing System.Windows.Media.Ausing System.Windows.Susing System.Windows.Busing System.Dusing System.Tusing System.Collections.Gusing System.Runtime.Serialization.Jusing System.IO;using System.Tnamespace AIMSBSUI.rmi{
/// Silverlight对Java通过HTTP协议进行远程方法调用的封装。内部通信使用JSON字符串作为交换协议。
public class RMIClient
静态变量、方法区
private static String WEB_CONTENT = "spring3Rest", RMI_ROOTPATH = "restrmi", HOST = "localhost";
private static int PORT = 8080;
//耦合性变量,不太想加。不过系统有使用要求,每个调用的方法都要传递一些会有所变化的环境参数。就用此变量!为防止报错,默认给个空容器
private static IDictionary xPram = new Dictionary();
#region 序列化与反序列化静态(JSON)
/// 序列化对象为JSON字符串,使用silverlight原生的DataContractJsonSerializer,需要引用System.ServiceModel.Web
/// 不建议在程序中使用该静态方法,该方法仅为便于测试调用
/// 需要序列化的对象类型
/// 需要序列化的对象
/// Json字符串
public static string getJosnStringFromObject(Z obj)
return BxuHttpUtil.SerializeObject(obj);
//string jsonResault = "";
//using (MemoryStream ms = new MemoryStream())
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Z));
serializer.WriteObject(ms, obj);
ms.Position = 0;
using (StreamReader reader = new StreamReader(ms, Encoding.UTF8))
jsonResault = reader.ReadToEnd();
//return jsonR
/// 反序列化JSON字符串为C#的类,使用silverlight原生的DataContractJsonSerializer,需要引用System.ServiceModel.Web
/// 不建议在程序中使用该静态方法,该方法仅为便于测试调用
/// 反序列化的对象类型
/// json字符串
/// 反序列化的对象实例
public static T getObjectFromJsonStr(string jsonStr)
return BxuHttpUtil.DeserializeObject(jsonStr);
//using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
obj = (T)serializer.ReadObject(ms);
#endregion
#endregion
#region 实例变量声明区
private string webContent = "spring3Rest";
private string rmiRootPath = "restrmi";
private string rootU
#endregion
#region 默认配置初始化区
/// 设置通用URL参数,这个静态字典里的属性会加在所有的调用里。因此在给参数名起名字时,最好十分特殊!不要和其它传入参数发生覆盖冲突
public static void setXParam(IDictionary glob_parameter) {
xPram = glob_
/// 设置WEB的名称和RMI调用的根,实网运行最好使用该方法初始化,
/// 其通过HtmlPage对象引用到host和port,自动访问部署所在的服务器的web容器ip和端口
/// 建议如下方式初始化使用
/// #if DEBUG
RMIClient.setDefault("webContent_", "rmiRootPath_", "host", port);
RMIClient.setDefault("webContent_", "rmiRootPath_");
/// #endif
/// 服务端的根名称。
/// 例如:Http://localhost:8080/xman/ 中的xman
/// rmi服务封装的Uri根。
/// 例如:Http://localhost:8080/xman/rmi/ 中的rmi
public static void setDefault(string webContent_, string rmiRootPath_)
WEB_CONTENT = webContent_;
RMI_ROOTPATH = rmiRootPath_;
HOST = HtmlPage.Document.DocumentUri.H
PORT = HtmlPage.Document.DocumentUri.P
/// 设置全部的默认参数。适用于需要强制指定访问非部署web服务所在主机和端口的情况。
/// 另外DEBUG模式下建议使用该配置指定WEB服务
/// 建议如下方式初始化使用
/// #if DEBUG
RMIClient.setDefault("webContent_", "rmiRootPath_", "host", port);
RMIClient.setDefault("webContent_", "rmiRootPath_");
/// #endif
/// 服务端的根名称。
/// 例如:Http://localhost:8080/xman/ 中的xman
/// rmi服务封装的Uri根。
/// 例如:Http://localhost:8080/xman/rmi/ 中的rmi
/// 服务端主机域名或IP。
/// 例如:Http://localhost:8080/xman/ 中的localhost
/// 服务端的端口。
/// 例如:Http://localhost:8080/xman/ 中的8080
public static void setDefault(string webContent_, string rmiRootPath_, string host_, int port_)
WEB_CONTENT = webContent_;
RMI_ROOTPATH = rmiRootPath_;
HOST = host_;
PORT = port_;
#endregion
#region 构造方法
/// 无视静态初始化的默认参数,用自定义参数构建RMIClient
/// 服务端主机域名或IP。
/// 例如:Http://localhost:8080/xman/ 中的localhost
/// 服务端的端口。
/// 例如:Http://localhost:8080/xman/ 中的8080
/// 服务端的根名称。
/// 例如:Http://localhost:8080/xman/ 中的xman
/// rmi服务封装的Uri根。
/// 例如:Http://localhost:8080/xman/rmi/ 中的rmi
public RMIClient(string host_, int port_, string webContent_, string rmiRootPath_)
rootUrl = String.Format("http://{0}:{1}/{2}/{3}", host_, port_, webContent_, rmiRootPath_);
DebugLog("RMIClietn 自定构造 rootUrl=" + rootUrl);
/// 使用静态初始化的默认参数(RMIClient.setDefault方法设置)直接构建RMIClient
public RMIClient()
host = HOST;
port = PORT;
webContent = WEB_CONTENT;
rmiRootPath = RMI_ROOTPATH;
rootUrl = String.Format("http://{0}:{1}/{2}/{3}", host, port, webContent, rmiRootPath);
DebugLog("RMIClietn 默认构造 rootUrl=" + rootUrl);
#endregion
#region Uri合成与参数转义
private Uri getMethodUri(string methodUri)
return getMethodUri(methodUri, null, false);
private Uri getMethodUri(string methodUri, IDictionary url_parameter, Boolean isEncode)
if (url_parameter == null || url_parameter.Count " + url);
Uri endPoint = new Uri(@url, UriKind.Absolute);
return endP
/// 构建url或form形成的参数串,用于传递传统参数。
/// 返回例如:name=aaaa&age=199&max=0
/// 需要以单值方式传送的调用参数
/// 是否对url_parameter的Value进行URLEncoding。当Value内有特殊字符时建议为true
true为编码
false为不进行编码
public string buildUrlParameterStr(IDictionary url_parameter, Boolean isEncode)
string parameter = "";
if (url_parameter != null && url_parameter.Count != 0)
foreach (KeyValuePair keyAndValue in url_parameter)
if (isEncode)
parameter = String.Format("{0}{1}={2}&", parameter, keyAndValue.Key, HttpUtility.UrlEncode(keyAndValue.Value));
parameter = String.Format("{0}{1}={2}&", parameter, keyAndValue.Key, keyAndValue.Value);
if (parameter.EndsWith("&"))
parameter = parameter.Substring(0, parameter.Length - 1);
DebugLog("RMIClient 最终参数字符串 -->" + parameter);
#endregion
#region 远程方法调用呈现区
#region POST方式调用返回结果为对象的实现
/// 使用POST模式,远程调用服务端REST方法,对应的resturi为rmiRootPath_/methodUri
/// URL中不带参数。数据采用Form方式,隐式传递。(显式从URL传递由于浏览器长度限制无法成功时,可以使用该方法隐式调用)
/// 返回对象结果
/// Content-Type:application/x-www-form-charset=UTF-8
/// 远程调用返回对象的类型
/// 远程调用的REST方法名称,注意对应的resturi为rmiRootPath_/methodUri,
/// 会自动拼装,如果你有分层要注意增加分层信息
/// 服务端@RequestMapping(value ="rmiRootPath_/methodUri"指定
/// 使用POST方式以隐式传递模式(Form)上报参数,参数的键值对信息。
/// 本方法自动会对值内容进行URLEncoding操作,以防止特殊字符导致调用失败
/// 服务端通过@RequestParam("keyName")得到参数值
/// 回调方法,请声明一个参数为(T responseObject, Exception e, String originalResponseString);的方法,注意T是返回对象类型。
/// 将方法名传递到此参数即可。
/// 远程调用成功后,您传入的方法将被调用。结果在responseObject中。
/// responseObject Json字符串反序列化的结果对象
/// e 异常信息,为null时调用正常,非null时调用发生异常。Json反序列化异常也会放在里面
/// originalResponseString 调用返回的原始信息字符串
public void callRemotMethodUseForm(String methodUri, IDictionary form_parameter, BxuHttpUtil.HttpObjectResultCallBack httpResultCallBackMethod)
AddGlobParam(form_parameter);
BxuHttpUtil httpUtil = new BxuHttpUtil();
Uri uri = getMethodUri(methodUri);
string parameter = buildUrlParameterStr(form_parameter, true);
//注意Form方式提交,"Content-Type" = "application/x-www-form-charset=UTF-8";
DebugLog(String.Format("===发起调用信息开始===\n\turi -->{0}\n\tform参数 -->{1}\n\t回调方法名称 -->{2}\n\t返回对象类型 -->{3}\n===发起调用信息结束===\n", uri.ToString(), parameter, httpResultCallBackMethod.Method.Name, typeof(T).FullName));
httpUtil.postForm(uri, parameter, httpResultCallBackMethod);//发起调用
private static void AddGlobParam(IDictionary form_parameter)
if (form_parameter == null) {
form_parameter = new Dictionary();
foreach (KeyValuePair kv in xPram)
if (form_parameter.ContainsKey(kv.Key))
form_parameter[kv.Key] = kv.V
form_parameter.Add(kv);
/// 使用POST模式,远程调用服务端REST方法,对应的resturi为rmiRootPath_/methodUri
/// 参数通过URL字符串方式显式传递,请注意:部分浏览器会有长度限制。
/// 返回对象结果
/// Content-Type:application/x-www-form-charset=UTF-8
/// 远程调用返回对象的类型
/// 远程调用的REST方法名称,注意对应的resturi为rmiRootPath_/methodUri,
/// 会自动拼装,如果你有分层要注意增加分层信息
/// 服务端@RequestMapping(value ="rmiRootPath_/methodUri"指定
/// 使用POST方式以显式式传递模式(URL)上报参数,参数的键值对信息。
/// 本方法自动会对Value值内容进行URLEncoding操作,以防止特殊字符导致调用失败
/// 服务端通过@RequestParam("keyName")得到参数值
/// 回调方法,请声明一个参数为(T responseObject, Exception e, String originalResponseString);的方法,注意T是返回对象类型。
/// 将方法名传递到此参数即可。
/// 远程调用成功后,您传入的方法将被调用。结果在responseObject中。
/// responseObject Json字符串反序列化的结果对象
/// e 异常信息,为null时调用正常,非null时调用发生异常。Json反序列化异常也会放在里面
/// originalResponseString 调用返回的原始信息字符串
public void callRemotMethod(String methodUri, IDictionary url_parameter, BxuHttpUtil.HttpObjectResultCallBack httpResultCallBackMethod)
AddGlobParam(url_parameter);
BxuHttpUtil httpUtil = new BxuHttpUtil();
Uri uri = getMethodUri(methodUri, url_parameter, true);
//注意Form方式提交,"Content-Type" = "application/x-www-form-charset=UTF-8";
DebugLog(String.Format("===发起调用信息开始===\n\turi -->{0}\n\t回调方法名称 -->{1}\n===发起调用信息结束===\n", uri.OriginalString, httpResultCallBackMethod.Method.Name));
httpUtil.postForm(uri, "", httpResultCallBackMethod);//发起调用
/// 使用POST模式,远程调用服务端REST方法,对应的resturi为rmiRootPath_/methodUri
/// URL字符串中不带参数。参数为对象序列化后的Json字符串,由Request Body传送。
/// 返回对象结果
/// Content-Type:application/charset=UTF-8
/// 远程调用返回对象的类型
/// 传入方法的对象参数的类型
/// 远程调用的REST方法名称,注意对应的resturi为rmiRootPath_/methodUri,
/// 会自动拼装,如果你有分层要注意增加分层信息
/// 服务端@RequestMapping(value ="rmiRootPath_/methodUri"指定
/// 使用POST方式以application/json方式传递上报参数,参数为一个对象(K类型)。
/// 本方法使用silverlight原生DataContractJsonSerializer的Json序列化实现对对象进行反序列化。
/// 可调用RMIClient.getJosnStringFromObject方法得到相同的序列化后字符串。
/// 服务端通过@@RequestBody 形参注解方式得到参数反序列化后的对象
/// 回调方法,请声明一个参数为(T responseObject, Exception e, String originalResponseString);的方法,注意T是返回对象类型。
/// 将方法名传递到此参数即可。
/// 远程调用成功后,您传入的方法将被调用。结果在responseObject中。
/// responseObject Json字符串反序列化的结果对象
/// e 异常信息,为null时调用正常,非null时调用发生异常。Json反序列化异常也会放在里面
/// originalResponseString 调用返回的原始信息字符串
public void callRemotMethod(String methodUri, K obj_parameter, BxuHttpUtil.HttpObjectResultCallBack httpResultCallBackMethod)
BxuHttpUtil httpUtil = new BxuHttpUtil();
Uri uri = getMethodUri(methodUri);
string jsonParame = httpUtil.getJosnStringFromObject(obj_parameter);
//注意Json方式提交,"Content-Type" = "application/charset=UTF-8";
DebugLog(String.Format("===发起调用信息开始===\n\turi -->{0}\n\t对象参数Json字串 -->{1}\n\t回调方法名称 -->{2}\n\t返回对象类型 -->{3}\n===发起调用信息结束===\n", uri.ToString(), jsonParame, httpResultCallBackMethod.Method.Name, typeof(T).FullName));
httpUtil.postJson(uri, jsonParame, httpResultCallBackMethod);
/// 使用POST模式,远程调用服务端REST方法,对应的resturi为rmiRootPath_/methodUri
/// URL字符串中传递url_parameter键值对参数。
/// obj_parameter对象参数为对象序列化后的Json字符串,由Request Body传送。
/// 返回对象结果
/// Content-Type:application/charset=UTF-8
/// 远程调用返回对象的类型
/// 传入方法的对象参数的类型
/// 远程调用的REST方法名称,注意对应的resturi为rmiRootPath_/methodUri,
/// 会自动拼装,如果你有分层要注意增加分层信息
/// 服务端@RequestMapping(value ="rmiRootPath_/methodUri"指定
/// 使用POST方式以显式式传递模式(URL)上报参数,参数的键值对信息。
/// 本方法自动会对Value值内容进行URLEncoding操作,以防止特殊字符导致调用失败。
/// 部分浏览器会有长度限制问题,过长的参数字符串可能被截断。
/// 服务端通过@RequestParam("keyName")得到参数值
/// 使用POST方式以application/json方式传递上报参数,参数为一个对象(K类型)。
/// 本方法使用silverlight原生DataContractJsonSerializer的Json序列化实现对对象进行反序列化。
/// 可调用RMIClient.getJosnStringFromObject方法得到相同的序列化后字符串。
/// 服务端通过@RequestBody 形参注解方式得到参数反序列化后的对象
/// 回调方法,请声明一个参数为(T responseObject, Exception e, String originalResponseString);的方法,注意T是返回对象类型。
/// 将方法名传递到此参数即可。
/// 远程调用成功后,您传入的方法将被调用。结果在responseObject中。
/// responseObject Json字符串反序列化的结果对象
/// e 异常信息,为null时调用正常,非null时调用发生异常。Json反序列化异常也会放在里面
/// originalResponseString 调用返回的原始信息字符串
public void callRemotMethod(String methodUri, IDictionary url_parameter, K obj_parameter, BxuHttpUtil.HttpObjectResultCallBack httpResultCallBackMethod)
AddGlobParam(url_parameter);
BxuHttpUtil httpUtil = new BxuHttpUtil();
Uri uri = getMethodUri(methodUri, url_parameter, true);
string jsonParame = httpUtil.getJosnStringFromObject(obj_parameter);
//注意Json方式提交,"Content-Type" = "application/charset=UTF-8";
DebugLog(String.Format("===发起调用信息开始===\n\turi -->{0}\n\t对象参数Json字串 -->{1}\n\t回调方法名称 -->{2}\n\t返回对象类型 -->{3}\n===发起调用信息结束===\n", uri.OriginalString, jsonParame, httpResultCallBackMethod.Method.Name, typeof(T).FullName));
httpUtil.postJson(uri, jsonParame, httpResultCallBackMethod);
#endregion
#region POST方式调用返回结果是原始字符串的实现
/// 使用POST模式,远程调用服务端REST方法或JSP页,对应的resturi为rmiRootPath_/methodUri
/// URL中不带参数。数据采用Form方式,隐式传递。(显式从URL传递由于浏览器长度限制无法成功时,可以使用该方法隐式调用)
/// 返回原始字符串结果
/// Content-Type:application/x-www-form-charset=UTF-8
/// 远程调用的REST方法名称,注意对应的resturi为rmiRootPath_/methodUri,
/// 会自动拼装,如果你有分层要注意增加分层信息
/// 使用POST方式以隐式传递模式(Form)上报参数,参数的键值对信息。
/// 本方法自动会对值内容进行URLEncoding操作,以防止特殊字符导致调用失败
/// 回调方法,请声明一个参数为(Exception e, String originalResponseString);的方法。
/// 将方法名传递到此参数即可。
/// e 异常信息,为null时调用正常,非null时调用发生异常。
/// originalResponseString 调用返回的原始信息字符串
public void callRemotMethodUseForm(String methodUri, IDictionary form_parameter, BxuHttpUtil.HttpResultCallBack httpResultCallBackMethod)
AddGlobParam(form_parameter);
BxuHttpUtil httpUtil = new BxuHttpUtil();
Uri uri = getMethodUri(methodUri);
string parameter = buildUrlParameterStr(form_parameter, true);
//注意Form方式提交,"Content-Type" = "application/x-www-form-charset=UTF-8";
DebugLog(String.Format("===发起调用信息开始===\n\turi -->{0}\n\tform参数 -->{1}\n\t回调方法名称 -->{2}\n===发起调用信息结束===\n", uri.ToString(), parameter, httpResultCallBackMethod.Method.Name));
httpUtil.postForm(uri, parameter, httpResultCallBackMethod);//发起调用
/// 使用POST模式,远程调用服务端REST方法或JSP页,对应的resturi为rmiRootPath_/methodUri
/// 参数通过URL字符串方式显式传递,请注意:部分浏览器会有长度限制。
/// 返回原始字符串结果
/// Content-Type:application/x-www-form-charset=UTF-8
/// 远程调用的REST方法名称,注意对应的resturi为rmiRootPath_/methodUri,
/// 会自动拼装,如果你有分层要注意增加分层信息
/// 使用POST方式以显式式传递模式(URL)上报参数,参数的键值对信息。
/// 本方法自动会对Value值内容进行URLEncoding操作,以防止特殊字符导致调用失败
/// 回调方法,请声明一个参数为(Exception e, String originalResponseString);的方法。
/// 将方法名传递到此参数即可。
/// e 异常信息,为null时调用正常,非null时调用发生异常。
/// originalResponseString 调用返回的原始信息字符串
public void callRemotMethodUseUrl(String methodUri, IDictionary url_parameter, BxuHttpUtil.HttpResultCallBack httpResultCallBackMethod)
AddGlobParam(url_parameter);
BxuHttpUtil httpUtil = new BxuHttpUtil();
Uri uri = getMethodUri(methodUri, url_parameter, true);
//注意Form方式提交,"Content-Type" = "application/x-www-form-charset=UTF-8";
DebugLog(String.Format("===发起调用信息开始===\n\turi -->{0}\n\t回调方法名称 -->{1}\n===发起调用信息结束===\n", uri.OriginalString, httpResultCallBackMethod.Method.Name));
httpUtil.postForm(uri, "", httpResultCallBackMethod);//发起调用
/// 使用POST模式,远程调用服务端REST方法或JSP页,对应的resturi为rmiRootPath_/methodUri
/// URL字符串中不带参数。参数为对象序列化后的Json字符串,由Request Body传送。
/// 返回原始字符串结果
/// Content-Type:application/charset=UTF-8
/// 传入方法的对象参数的类型
/// 远程调用的REST方法名称,注意对应的resturi为rmiRootPath_/methodUri,
/// 会自动拼装,如果你有分层要注意增加分层信息
/// 使用POST方式以application/json方式传递上报参数,参数为一个对象(K类型)。
/// 本方法使用silverlight原生DataContractJsonSerializer的Json序列化实现对对象进行反序列化。
/// 可调用RMIClient.getJosnStringFromObject方法得到相同的序列化后字符串。
/// 回调方法,请声明一个参数为(Exception e, String originalResponseString);的方法。
/// 将方法名传递到此参数即可。
/// e 异常信息,为null时调用正常,非null时调用发生异常。
/// originalResponseString 调用返回的原始信息字符串
public void callRemotMethod(String methodUri, K obj_parameter, BxuHttpUtil.HttpResultCallBack httpResultCallBackMethod)
BxuHttpUtil httpUtil = new BxuHttpUtil();
Uri uri = getMethodUri(methodUri);
string jsonParame = httpUtil.getJosnStringFromObject(obj_parameter);
//注意Json方式提交,"Content-Type" = "application/charset=UTF-8";
DebugLog(String.Format("===发起调用信息开始===\n\turi -->{0}\n\t对象参数Json字串 -->{1}\n\t回调方法名称 -->{2}\n===发起调用信息结束===\n", uri.ToString(), jsonParame, httpResultCallBackMethod.Method.Name));
httpUtil.postJson(uri, jsonParame, httpResultCallBackMethod);
/// 使用POST模式,远程调用服务端REST方法或JSP页,对应的resturi为rmiRootPath_/methodUri
/// URL字符串中传递url_parameter键值对参数。
/// obj_parameter对象参数为对象序列化后的Json字符串,由Request Body传送。
/// 返回原始字符串结果
/// Content-Type:application/charset=UTF-8
/// 传入方法的对象参数的类型
/// 远程调用的REST方法名称,注意对应的resturi为rmiRootPath_/methodUri,
/// 会自动拼装,如果你有分层要注意增加分层信息
/// 使用POST方式以显式式传递模式(URL)上报参数,参数的键值对信息。
/// 本方法自动会对Value值内容进行URLEncoding操作,以防止特殊字符导致调用失败。
/// 部分浏览器会有长度限制问题,过长的参数字符串可能被截断。
/// 使用POST方式以application/json方式传递上报参数,参数为一个对象(K类型)。
/// 本方法使用silverlight原生DataContractJsonSerializer的Json序列化实现对对象进行反序列化。
/// 可调用RMIClient.getJosnStringFromObject方法得到相同的序列化后字符串。
/// 回调方法,请声明一个参数为(Exception e, String originalResponseString);的方法,注意T是返回对象类型。
/// 将方法名传递到此参数即可。
/// e 异常信息,为null时调用正常,非null时调用发生异常。Json反序列化异常也会放在里面
/// originalResponseString 调用返回的原始信息字符串
public void callRemotMethod(String methodUri, IDictionary url_parameter, K obj_parameter, BxuHttpUtil.HttpResultCallBack httpResultCallBackMethod)
AddGlobParam(url_parameter);
BxuHttpUtil httpUtil = new BxuHttpUtil();
Uri uri = getMethodUri(methodUri, url_parameter, true);
string jsonParame = httpUtil.getJosnStringFromObject(obj_parameter);
//注意Json方式提交,"Content-Type" = "application/charset=UTF-8";
DebugLog(String.Format("===发起调用信息开始===\n\turi -->{0}\n\t对象参数Json字串 -->{1}\n\t回调方法名称 -->{2}\n===发起调用信息结束===\n", uri.OriginalString, jsonParame, httpResultCallBackMethod.Method.Name));
httpUtil.postJson(uri, jsonParame, httpResultCallBackMethod);
#endregion
//#region GET方式调用返回结果是原始字符串
///// 使用GET模式,远程调用服务端REST方法或JSP页,对应的resturi为rmiRootPath_/methodUri
///// 参数通过URL字符串方式显式传递,请注意:部分浏览器会有长度限制。
///// 返回原始字符串结果
///// 远程调用的REST方法名称,注意对应的resturi为rmiRootPath_/methodUri,
///// 会自动拼装,如果你有分层要注意增加分层信息
///// 使用POST方式以显式式传递模式(URL)上报参数,参数的键值对信息。
///// 本方法自动会对Value值内容进行URLEncoding操作,以防止特殊字符导致调用失败
///// 回调方法,请声明一个参数为(Exception e, String originalResponseString);的方法。
///// 将方法名传递到此参数即可。
///// e 异常信息,为null时调用正常,非null时调用发生异常。
///// originalResponseString 调用返回的原始信息字符串
//public void callGETMethod(String methodUri, IDictionary url_parameter, BxuHttpUtil.HttpResultCallBack httpResultCallBackMethod)
BxuHttpUtil httpUtil = new BxuHttpUtil();
Uri uri = getMethodUri(methodUri, url_parameter, true);
//注意Form方式提交,"Content-Type" = "application/x-www-form-charset=UTF-8";
DebugLog(String.Format("===发起调用信息开始===\n\turi -->{0}\n\t回调方法名称 -->{1}\n===发起调用信息结束===\n", uri.ToString(), httpResultCallBackMethod.Method.Name));
httpUtil.getOriginal(uri, httpResultCallBackMethod);//发起调用
//#endregion
#endregion
#region 调试代码(无关代码区)
private void testPostString()
string urlPagm = HttpUtility.UrlEncode("阿=\"&!\r\n李郎");
Debug.WriteLine("urlPagmEncode-->" + urlPagm);
string url = @"http://localhost:8080/spring3Rest/json/PostBody.json?name=" + urlP
//url = HttpUtility.UrlEncode(url);
Debug.WriteLine("urlEncode-->" + url);
Uri endPoint = new Uri(@url, UriKind.Absolute);
//string pagm = "uid=test&t=873604&loginuserid=1&loginusername=yili&userip=218.31.158.208";
string pagm = "{\"name\":\"阿=&!\\r\\n李郎\",\"age\":19,\"info\":{\"2\":\"5\",\"1\":\"9\"}}";
//&doAction=&createTime= 00:00:00.0&filterDescription=本条过滤条件,名称为:测试过滤 告警所属城市为: 告警时间开始时间: 0:00:00
结束时间: 0:00:00过滤状态:过滤↓
告警类型包含:
告警级别包含:
周期时间:[{\"startTime\":00:00:00,\"endTime\":12:59:59},{\"startTime\":12:59:59,\"endTime\":23:59:59}&cycleDateTimes=[{\"startTime\":00:00:00,\"endTime\":12:59:59},{\"startTime\":12:59:59,\"endTime\":23:59:59}]&cycleWeekDates=[]&cycleMonthDates=[]&propertyName=[]&propertyValue=[]&propertyIsreguler=1&propertyIsFuzzy=-1&allowDeny=0&alarmLevelIsInclude=1&alarmLevel=[\"WARNING\"]&alarmTypeIsInclude=1&alarmType=[\"NODE\",\"SLOT\",\"PORT\",\"CIRCUIT\",\"LOGIC_PORT\"]&isEnable=1&additionalIsFuzzy=-1&additionalIsreguler=1&additionalValue=[]&additionalName=[]&alarmKeyword=[]&keywordIsreguler=1&keywordIsFuzzy=-1&startTime= 0:00:00&endTime= 0:00:00&resourceType=[]&resourceId=[]&cityId=-1&emsId=-1&filterNo=1&id=2&userId=1&userName=yili&usergroupId=1&usergroupName=监控中心管理员&name=测试过滤
WebClient webClient = new WebClient();
//webClient.AllowReadStreamBuffering =
//webClient.AllowWriteStreamBuffering =
webClient.Encoding = Encoding.UTF8;
//webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
webClient.Headers["Content-Type"] = "application/charset=UTF-8";
//"multipart/form-data";
//webClient.Headers["Content-Disposition"] = "form- loginusername=userip=218.31.158.208";
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
webClient.UploadProgressChanged += new UploadProgressChangedEventHandler(webClient_UploadProgressChanged);
webClient.UploadStringAsync(endPoint, "POST", pagm);
private void webClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
Debug.WriteLine(e.ProgressPercentage + "/" + e.TotalBytesToReceive + "/" + e.TotalBytesToSend);
//SynchronizationContext _syncContext = SynchronizationContext.C//线程同步回调
//_syncContext.Post(
private void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
Debug.WriteLine(e.Result.ToString());
//上传调用完毕
#endregion
#region DEBUG LOG 控制区
/// 输出DEBUG的log,怀疑模块有问题的时候,调试可以打开。关闭方法到类的最前端注释掉DEBUG_LOG_BXU_ON的声明
/// 打开的方法,放开DEBUG_LOG_BXU_ON宏的注释
private void DebugLog(string loginfo)
{#if DEBUG_LOG_BXU_ON
Debug.WriteLine(loginfo);#endif
#endregion
上一集:没有了 下一集:
相关文章:&&&&&&&&&&
最新添加资讯
24小时热门资讯
附近好友搜索

我要回帖

更多关于 silverlight json 的文章

 

随机推荐