以下调用方法,以调用苏州天气接口为例。
一、后台请求服务
方法一、C#后台,通过构建Soap请求接口数据

        //获取天气详细信息
        public JsonResult GetWeatherDetails(string dataType,string cityName)
        {
            //构造soap请求信息
            StringBuilder soap = new StringBuilder();
            soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            soap.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
            soap.Append("<soap:Body>");
            if (cityName != null)
            {
                soap.Append("<" + dataType + " xmlns =\"http://www.sz121.com/opapi\">");
                soap.Append("<str>" + cityName + "</str>");
                soap.Append("<sid></sid>");
                soap.Append("</" + dataType + ">");
            }
            else
            {
                soap.Append("<" + dataType + " xmlns =\"http://www.sz121.com/opapi\" />");
            }
            soap.Append("</soap:Body>");
            soap.Append("</soap:Envelope>");

            //发起请求
            Uri uri = new Uri("http://www.sz121.com/qxj/webservice/opapi.asmx");
            WebRequest webRequest = WebRequest.Create(uri);
            webRequest.ContentType = "text/xml; charset=utf-8";
            webRequest.Method = "POST";

            using (Stream requestStream = webRequest.GetRequestStream())
            {
                byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
                requestStream.Write(paramBytes, , paramBytes.Length);
            }

            //响应
            string strWebData = string.Empty;
            WebResponse webResponse = webRequest.GetResponse();
            using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
            {
                strWebData = myStreamReader.ReadToEnd();
            }

            var results = new
            {
                result = strWebData,
            };
            return Json(results, JsonRequestBehavior.AllowGet);

        }
方法二、直接使用VS添加服务引用
① 右击项目 添加服务引用

②点击高级
③添加Web引用
④输入url地址,修改web引用名,点击添加引用
⑤调用代码
        public string getWeather()
        {
            com.sz121.www.WeatherWebService s = new com.sz121.www.WeatherWebService();
            string CityName = "苏州";
            string result = s.getWeatherbyCityName(CityName);
            return result.ToString();
        }
 

二、前端进行解析

function loadXML(xmlString) {
        var xmlDoc = null;
        //判断浏览器的类型
        //支持IE浏览器
        if (!window.DOMParser && window.ActiveXObject) {   //window.DOMParser 判断是否是非ie浏览器
            var xmlDomVersions = ['MSXML.2.DOMDocument.6.0', 'MSXML.2.DOMDocument.3.0', 'Microsoft.XMLDOM'];
            for (var i = 0; i < xmlDomVersions.length; i++) {
                try {
                    xmlDoc = new ActiveXObject(xmlDomVersions[i]);
                    xmlDoc.async = false;
                    xmlDoc.loadXML(xmlString); //loadXML方法载入xml字符串
                    break;
                } catch (e) {
                }
            }
        }
        //支持Mozilla浏览器
        else if (window.DOMParser && document.implementation && document.implementation.createDocument) {
            try {
                /* DOMParser 对象解析 XML 文本并返回一个 XML Document 对象。
                 * 要使用 DOMParser,使用不带参数的构造函数来实例化它,然后调用其 parseFromString() 方法
                 * parseFromString(text, contentType) 参数text:要解析的 XML 标记 参数contentType文本的内容类型
                 * 可能是 "text/xml" 、"application/xml" 或 "application/xhtml+xml" 中的一个。注意,不支持 "text/html"。
                 */
                domParser = new DOMParser();
                xmlDoc = domParser.parseFromString(xmlString, 'text/xml');
            } catch (e) {
            }
        }
        else {
            return null;
        }
        return xmlDoc;
    }, 

调用WebService获取数据的更多相关文章

  1. C#调用WebService获取天气信息

    概述 本文使用C#开发Winform应用程序,通过调用<WebXml/>(URL:http://www.webxml.com.cn)的WebService服务WeatherWS来获取天气预 ...

  2. .NET(C#)调用webService获取客户端IP地址所属区域(非异步)

    功能描述: 此接口用于获取客户端访问的IP的地址所属的区域(国家,城市等).通过输入IP地址查询国家.城市.所有者等信息.没有注明国家的为中国输入参数:IP地址(自动替换 " ." ...

  3. ios下通过webservice获取数据

    经历了两天的摸索,终于成功获取了数据,因为公司要做一个停车入库的信息查询,所以需要访问webservice的接口,由于没有接触过webservice,所以第一天就是各种搜索资料,类库,各种尝试,甚至是 ...

  4. 调用webservice获取电话号码归属地信息

    首先什么是webservice ? 从广义上面讲,任何一个服务器所提供的"数据","内容","方法"等等都可以理解为webservice. ...

  5. c++ 调用 wmi 获取数据

    #define _WIN32_DCOM #include <iostream> using namespace std; #include <comdef.h> #includ ...

  6. SharePoint2013 Online中InfoPath 无法调用WebService

    传说微软office365中国区服务器已经迁移到国内,试了下速度果然比之前快了很多,不过随后测试了个简单的功能,还是直接被打击了. 准备在online版本中做一个简单的报销流程测试测试,于是先用Inf ...

  7. Jquery调用Webservice传递Json数组

    Jquery由于提供的$.ajax强大方法,使得其调用webservice实现异步变得简单起来,可以在页面上传递Json字符串到Webservice中,Webservice方法进行业务处理后,返回Js ...

  8. ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段

    ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...

  9. ASP.net jQuery调用webservice返回json数据的一些问题

    之前寒假时,试着使用jQuery写了几个异步请求demo, 但是那样是使用的webform普通页面,一般应该是用 webservice 居多. 最近写后台管理时,想用异步来实现一些信息的展示和修改, ...

随机推荐

  1. http 500错误怎么解决方法

    出现500错误的原因是很多的,一般来说,如果程序出错,那么在浏览器内会返回给用户一个友好的错误提示,统一称之为服务器500错误. 解决的方法就是您必须在http中能够正确的获得错误信息,方法为:请打开 ...

  2. instanceof 原理

    运行流程 function instance_of(L, R) {                               //L 表示左表达式,R 表示右表达式   var O = R.prot ...

  3. spring boot hello and docker

    主要是想试下spring boot运行在docker里的感觉, 小试牛刀   :) 这是原文,参考一下:  https://spring.io/guides/gs/spring-boot-docker ...

  4. SpringMVC知识一锅烩

    Spring简介 SpringMVC和Struts2一样都是属于表现层的框架,将前段发出的请求分发给对应的后端处理器即Controller 处理流程 用户请求被前端控制前拦截,然后根据对应的拦截路径去 ...

  5. C#3.0中的扩展方法

    在实际应用中,开发者完成代码的编译后,除非重新编译更改后的代码,否则开发者很难在原有代码中添加新的功能. 在C#3.0中,提供了一个扩展方法的新特性,可以使得开发者在编译后的程序集里边添加相关的方法, ...

  6. 运行第一个 Service - 每天5分钟玩转 Docker 容器技术(96)

    上一节我们创建好了 Swarm 集群, 现在部署一个运行 httpd 镜像的 service,执行如下命令: docker service create --name web_server httpd ...

  7. 设计模式-模板方法模式(Head First)

    参考书籍:Head First设计模式 什么是模板方法模式 定义:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤. 怎 ...

  8. JAVA基础面试(一)

    1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致. 2.Java有 ...

  9. 如何用Python写一个计算器软件 附带效果图

    该计算器使用Python  tkinter模块开发 效果如下图 import tkinter #导入tkinter模块 root = tkinter.Tk() root.minsize(280,500 ...

  10. Python crawler access to web pages the get requests a cookie

    Python in the process of accessing the web page,encounter with cookie,so we need to get it. cookie i ...