[C#] 图文解说调用WebServer实例
本文旨在实现如何在.NET环境下调用WebServer,以天气接口为例进行说明。
WebServer地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
1、创建程序。本例创建的是ASP.NET网站,可以根据个人需求创建不同的项目。

2、添加Web窗体。

3、页面设计采用BootStrap,在项目中引入相应的JS和CSS文件
4、在程序中添加WebServer服务。
①项目中右键,选择添加,找到服务引用,单击进入。如下图

②进入到如下界面,选择高级。

③选择左下角的添加Web引用。

④如图操作

5、添加实体类Weather.cs
using System;
/// <summary>
/// Summary description for weather
/// </summary>
namespace Model
{
public class Weather
{
public String Provinces { get; set; } //省份
public String CityName { get; set; } //城市
public String CityCode { get; set; } //城市代码
public String CityPicture { get; set; } //城市图片
public String UpdateTime { get; set; } //天气预报的更新时间
public String TodayWeather { get; set; } //当天温度
public String TodaySurvey { get; set; }
public String TodayWindDirection { get; set; }
public String TodayStarPic { get; set; }
public String TodayEndPic { get; set; }
public String Detail { get; set; } //天气实况
public String LifeIndex { get; set; } //生活指数
public String TomorrowWeather { get; set; } //第二天的气温
public String TomorrowSurvey { get; set; }//第二天的概况
public String TomorrowWindDirection { get; set; }//第二天的风向和风力
public String TomorrowStarPic { get; set; }//第二天的图标一
public String TomorrowEndPic { get; set; }//第二天的图标二
public String TDATWeather { get; set; }//第三天的气温
public String TDATSurvey { get; set; }//第三天的概况
public String TDATWindDirection { get; set; }//第三天的风向和风力
public String TDATStarPic { get; set; }//第三天的图标一
public String TDATEndPic { get; set; } //第三天的图标二
public String CityDetail { get; set; } //被查询的城市或地区的介绍 }
}
6、页面代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no" />
<link rel="stylesheet" href="/public/bootstrap/3.3.4/css/bootstrap.min.css" />
<link rel="stylesheet" href="/public/bootstrap/3.3.4/css/bootstrap-theme.min.css" />
<script src="public/jquery/jquery.min.js"></script>
<script src="public/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<title>天气预报</title>
<script>
$(document).ready(function () {
$("#BtnSearch").click(function () {
var cityName = document.getElementById("TxtCityName").value;
if (cityName == "" || cityName == null) {
alert("请输入要查询的城市名称!");
return false;
}
$.ajax({
type: "GET",
async: false,
url: "Ajax/GetWeatherInfo.ashx?CityName=" + cityName,
success: function (data) {
var obj = JSON.parse(data);
if (obj.CityName == "" || obj.CityName == null) {
alert("查询结果为空!这城市或区域暂时不支持查询,请检查输入城市是否有误!");
return false;
}
document.getElementById("todayWeatherDetail").style.display = "block";
document.getElementById("tomorrowWeatherDetail").style.display = "block";
document.getElementById("tdatWeatherDetail").style.display = "block";
document.getElementById("cityDetail").style.display = "block"; //当天
document.getElementById("todaySurvey").innerHTML = obj.TodaySurvey;
document.getElementById("todayWeather").innerHTML = obj.TodayWeather;
document.getElementById("todayWindDirection").innerHTML = obj.TodayWindDirection;
document.getElementById("detail").innerHTML = obj.Detail;
document.getElementById("lifeIndex").innerHTML = obj.LifeIndex;
//第二天
document.getElementById("tomorrowSurvey").innerHTML = obj.TomorrowSurvey;
document.getElementById("tomorrowWeather").innerHTML = obj.TomorrowWeather;
document.getElementById("tomorrowWindDirection").innerHTML = obj.TomorrowWindDirection;
//第三天
document.getElementById("taftSurvey").innerHTML = obj.TDATSurvey;
document.getElementById("TDATWeather").innerHTML = obj.TDATWeather;
document.getElementById("TDATWindDirection").innerHTML = obj.TDATWindDirection;
//城市简介
document.getElementById("cityName").innerHTML = obj.CityName + "简介";
document.getElementById("CityDetail").innerHTML = obj.CityDetail;
}
});
});
});
</script>
</head>
<body>
<div class="container">
<div style="margin-top: 10px;" class="container">
<div class="form-group" style="width: 70%; float: left; margin-right: 20px;">
<input class="form-control" id="TxtCityName" placeholder="请输入城市名称" />
</div>
<button id="BtnSearch" class="btn btn-primary">查询</button>
</div>
<div class="container">
<div class="panel panel-success" style="display: none;margin-top:3px;" id="todayWeatherDetail">
<div class="panel-heading">
<a data-toggle="collapse" data-parent="#accordion" href="#todayWeathercollapse" aria-expanded="true" aria-controls="collapse">
<label id="todaySurvey"></label>
</a>
</div>
<div id="todayWeathercollapse" class="panel-collapse collapse in">
<div class="panel-body">
<p><label id="todayWeather"></label></p>
<p><label id="todayWindDirection"></label></p>
<p><label id="detail"></label></p>
<p><label id="lifeIndex"></label></p>
</div>
</div>
</div> <div class="panel panel-warning" style="display: none;" id="tomorrowWeatherDetail">
<div class="panel-heading">
<a data-toggle="collapse" data-parent="#accordion" href="#tomorrowWeathercollapse" aria-expanded="true" aria-controls="collapse">
<label id="tomorrowSurvey"></label>
</a>
</div>
<div id="tomorrowWeathercollapse" class="panel-collapse collapse">
<div class="panel-body">
<p><label id="tomorrowWeather"></label></p>
<p><label id="tomorrowWindDirection"></label></p>
</div>
</div>
</div> <div class="panel panel-danger" style="display: none;" id="tdatWeatherDetail">
<div class="panel-heading">
<a data-toggle="collapse" data-parent="#accordion" href="#tdatWeathercollapse" aria-expanded="true" aria-controls="collapse">
<label id="taftSurvey"></label>
</a>
</div>
<div id="tdatWeathercollapse" class="panel-collapse collapse">
<div class="panel-body">
<p><label id="TDATWeather"></label></p>
<p><label id="TDATWindDirection"></label></p>
</div>
</div>
</div> <div class="panel panel-info" style="display: none;" id="cityDetail">
<div class="panel-heading">
<a data-toggle="collapse" data-parent="#accordion" href="#cityDetailcollapse" aria-expanded="true" aria-controls="collapse">
<label id="cityName"></label>
</a>
</div>
<div id="cityDetailcollapse" class="panel-collapse collapse">
<div class="panel-body">
<label id="CityDetail">
</label>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
7、一般处理程序的代码如下:调用WebServer的代码在此体现,详细请看代码注释。
<%@ WebHandler Language="C#" Class="GetWeatherInfo" %> using System;
using System.Web; public class GetWeatherInfo : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string cityName = context.Request.Params["CityName"]; Model.Weather weather = new Model.Weather(); Weather.WeatherWebService w = new Weather.WeatherWebService(); //调用WebServer,其中Weather是引入WebServer服务是取的引用名
string[] res = new string[];
res = w.getWeatherbyCityName(cityName); //调用getWeatherbyCityName方法 weather.Provinces = res[]; //省份
weather.CityName = res[]; //城市
weather.CityCode = res[]; //城市代码
weather.CityPicture = res[]; //城市图片
weather.UpdateTime = res[]; //天气预报的更新时间
weather.TodayWeather = res[]; //当天温度
weather.TodaySurvey = res[];
weather.TodayWindDirection = res[];
weather.TodayStarPic = res[];
weather.TodayEndPic = res[];
weather.Detail = res[]; //天气实况
weather.LifeIndex = res[]; //生活指数
weather.TomorrowWeather = res[]; //第二天的气温
weather.TomorrowSurvey = res[]; //第二天的概况
weather.TomorrowWindDirection = res[]; //第二天的风向和风力
weather.TomorrowStarPic = res[]; //第二天的图标一
weather.TomorrowEndPic = res[]; //第二天的图标二
weather.TDATWeather = res[]; //第三天的气温
weather.TDATSurvey = res[]; //第三天的概况
weather.TDATWindDirection = res[]; //第三天的风向和风力
weather.TDATStarPic = res[]; //第三天的图标一
weather.TDATEndPic = res[]; //第三天的图标二
weather.CityDetail = res[]; //被查询的城市或地区的介绍 System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); jss.Serialize(weather, stringBuilder); context.Response.Write(stringBuilder);
} public bool IsReusable
{
get
{
return false;
}
} }
8、运行效果如下:

9、项目结构图:

[C#] 图文解说调用WebServer实例的更多相关文章
- 图文解说:Nginx+tomcat配置集群负载均衡
图文解说:Nginx+tomcat配置集群负载均衡 博客分类: appserver nginxTomcatUbuntuLinux网络应用 作者:niumd Blog:http://ari.iteye ...
- Android NDK r8 Cygwin CDT 在window下开发环境搭建 安装配置与使用 具体图文解说
版权声明:本博客全部文章均为原创.欢迎交流.欢迎转载:转载请勿篡改内容,而且注明出处,谢谢! https://blog.csdn.net/waldmer/article/details/3272500 ...
- (转)xcode5.0.2下国际化图文解说
原文:http://blog.csdn.net/dragoncheng/article/details/6703311 xcode5.0.2下国际化图文解说 分类: ...
- Don’t Use Accessor Methods in Initializer Methods and dealloc 【初始化和dealloc方法中不要调用属性的存取方法,而要直接调用 _实例变量】
1.问题: 在dealloc方法中使用[self.xxx release]和[xxx release]的区别? 用Xcode的Analyze分析我的Project,会列出一堆如下的提示:Inco ...
- Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例
引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...
- NET异步调用Webserver
之前,有个同事跑来问我一堆的什么多线程异步进行调用Sap的服务再突然把进程关闭,还说要设置一个循环判断调用的结果,搞得我听的一头雾水,但是我明显感觉到他的设计思路已经渐行渐远了...已经再偏远的山区中 ...
- PHP调用Webservice实例
原文 PHP调用Webservice实例 NuSoap是PHP环境下的WebService编程工具,用于创建或调用WebService.它是一个开源软件,是完全采用PHP语言编写的.通过HTTP收发S ...
- 转载 基于JAVA每月运势api调用代码实例
代码描述:基于JAVA每月运势api调用代码实例 接口地址:http://www.juhe.cn/docs/api/id/58 原文链接:http://outofmemory.cn/code-snip ...
- 调用webserver时出现:请求因 HTTP 状态 401 失败: Unauthorized。
请求因 HTTP 状态 401 失败: Unauthorized 今天在调用webserver时出现了上述标题的错误,开始认为是由于端口的问题,我把端口恢复80默认端口后,但是问题并没有解决!后来我自 ...
随机推荐
- SQL Server删除distribution数据库
在数据库服务器删除复制(发布订阅)后,如何删除掉数据库distribution呢?如果你通过SSMS工具去删除数据库distribution,你会发现根本没有删除选项. 下面介绍一下删除distrib ...
- 聊下 git remote prune origin
在你经常使用的命令当中有一个git branch –a 用来查看所有的分支,包括本地和远程的.但是时间长了你会发现有些分支在远程其实早就被删除了,但是在你本地依然可以看见这些被删除的分支. 你可以通过 ...
- charles 抓取eclipse中的请求
charles抓取eclipse中的请求 有时候,想要监测eclipse中发送get获取post请求,一样可以使用代理方式: 1.eclipse代码设置 代码中添加,可以就写在主函数中,然后再调用请求 ...
- Linux第一天 ssh登录和软件安装详解
Linux学习第一天 操作环境: Ubuntu 16.04 Win10系统,使用putty_V0.63 本身学习Linux就是想在服务器上使用的.实际情况,可能我很难直接到坐在服务器前,使用界面操作系 ...
- ajax缓存问题
默认情况下,请求总会被发出去,但浏览器有可能从它的缓存中调取数据.换句话说,在缓存过期之前,针对相同地址发起的多个Ajax请求,只有第一次会真正发送到服务端.要禁止使用缓存的结果,可以设置 cache ...
- java自带工具-javap使用
javap是JDK自带的反汇编器,可以查看java编译器为我们生成的字节码.通过它,我们可以对照源代码和字节码,从而了解很多编译器内部的工作,有助与我们更加理解java特性. javap(反汇编命令) ...
- log4net不同logger输出日志
4步曲 1.引用log4net.dll(nuget) 2.任意位置的命名空间头部加入下面的代码,web.config可修改为自己定义的.xml [assembly: log4net.Config.Xm ...
- 理解 OpenStack 高可用(HA)(1):OpenStack 高可用和灾备方案 [OpenStack HA and DR]
本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)N ...
- 2016.10.30 NOIP模拟赛 day2 AM 整理
题目+数据:链接:http://pan.baidu.com/s/1gfBg4h1 密码:ho7o 总共得了:130分, 1:100分 2:30分(只会这30分的暴力) 3:0(毫无思路) 虽然不高, ...
- BZOJ1500: [NOI2005]维修数列[splay ***]
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 12278 Solved: 3880[Submit][Statu ...