网站首页 > 博客文章 正文
现在使用webservice开发的项目仍然比较多,但由于json的广泛使用,虽然项目采用webservice,但是实际上很多的返回值格式已经是json。但是webservice最终的返回结果却仍然是xml格式的,而无法再客户端直接获取到json格式。接下来就以.net为例说明一下如何将xml返回的json提取出来,直接在客户端以json格式直接使用。
如WebService有以下方法:
WebService方法列表
使用visual studio新建一个.net web项目。
使用webservice服务,使用Web服务的流程是:添加现有WebService引用->客户端调用。
新建ashx文件:
新建一般处理程序
新建handler部分代码:
/// <summary>
/// 登录接口封装
/// </summary>
public class Login : BaseHandler
{
public override void ProcessRequest(HttpContext context)
{
//传入webservice参数
string username = Request.Params["username"];
string password = Request.Params["password"];
//调用webservice
string result = client.Login(username, password);
Response.Write(result);
}
public override bool IsReusable
{
get
{
return false;
}
}
}
BaseHandler部分主要代码:
public class BaseHandler:IHttpHandler
{
#region attributes
protected HttpRequest Request;
protected HttpResponse Response;
protected HttpServerUtility Server = HttpContext.Current.Server;
//具体类名,请根据实际情况修改
protected WebService1SoapClient client; //webservice引用类
protected Result result = new Result();
protected string param = string.Empty;
#endregion
#region constructor
public BaseHandler()
{
Request = HttpContext.Current.Request;
Response = HttpContext.Current.Response;
Response.ContentType = "application/json";
client = new WebService1SoapClient();
}
#endregion
#region methods
public virtual bool IsReusable
{
get { return true; }
}
public virtual void ProcessRequest(HttpContext context)
{
//throw new NotImplementedException();
}
#endregion
}
这样一来,通过对接口进行二次封装,使返回结果为json格式,可以直接通过js在客户端解析。
对于第三方提供的接口,不管是xml还是json格式,最好还是对接口进行封装后再进行调用。
猜你喜欢
- 2024-10-17 .NET配置文件大揭秘:轻松读取JSON、XML
- 2024-10-17 [NewLife.XCode]反向工程(自动建表建库大杀器)
- 2024-10-17 Java 使用fastjson将json字符串转为泛型对象
- 2024-10-17 json schema(json schema生成工具)
- 2024-10-17 python 实例分析——发送json数据相关实现技巧
- 2024-10-17 比较一下XML, JSON和YAML(xml数据和json数据)
- 2024-10-17 Protobuf的简单介绍、使用和分析(protobuf的作用)
- 2024-10-17 详解电子表格中的json数据:序列化与反序列化
- 2024-10-17 【Qt教程】使用 QJson 处理 JSON(qt线程使用)
- 2024-10-17 引入 jackson-dataformat-xml 后,默认响应结果是 json 还是 xml?
你 发表评论:
欢迎- 374℃手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路
- 369℃用AI Agent治理微服务的复杂性问题|QCon
- 360℃初次使用IntelliJ IDEA新建Maven项目
- 353℃Maven技术方案最全手册(mavena)
- 351℃安利Touch Bar 专属应用,让闲置的Touch Bar活跃起来!
- 348℃InfoQ 2024 年趋势报告:架构篇(infoq+2024+年趋势报告:架构篇分析)
- 346℃IntelliJ IDEA 2018版本和2022版本创建 Maven 项目对比
- 344℃从头搭建 IntelliJ IDEA 环境(intellij idea建包)
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)