[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默认端口后,但是问题并没有解决!后来我自 ...
随机推荐
- 1、软件工程师要阅读的书籍 - IT软件人员书籍系列文章
软件工程师要阅读的书籍估计是项目组内最多的.软件工程师处于项目组中最基础的人员储备阶层,与项目的关系最密切.当然,现在是大数据时代,我们无法全部看完所有相关的书籍,只能够先学习工作需要的知识,然后在项 ...
- asp.net获取服务端和客户端信息
asp.net获取服务端和客户端信息 获取服务器名:Page.Server.ManchineName获取用户信息:Page.User 获取客户端电脑名:Page.Request.UserHostNam ...
- Linux From Scratch(从零开始构建Linux系统,简称LFS)- Version 7.7(二)
七. 构建临时系统 1. 通用编译指南 a. 确认是否正确设置了 LFS 环境变量 echo $LFS b. 假定你已经正确地设置了宿主系统的符号链接: 1)shell 使用的是 bash. 2)sh ...
- Ajax的笔记
Ajax指异步JavaScript及XML(Asynchronous JavaScipt And XML),是一种异步交互式网页开发技术,用于创建快速动态网页.与服务器进行少量数据交换中,Ajax可以 ...
- C语言猜拳游戏
2016年最后一篇文章 今天闲来无事Google了一段C语言写的猜拳游戏的代码(本人水平比较低,几乎是刚入门),我没做什么修改.这个switch语句里面对result的处理让我眼前一新,原来是这么玩的 ...
- android break 与 return 的区别
break 的含义是中断,return 的含义是结束整个方法的执行. 区别. public static void main(String agrs[]){ int i; for(i=0;i<1 ...
- debian/ubuntu安装桌面环境
apt-get install xorg apt-get install gnome 然后startx ubuntu 安装Gnome桌面 1.安装全部桌面环境,其实Ubuntu系列桌面实际上有几种桌面 ...
- aa
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- Struts2 自定义MVC框架
一.Model1与Model2: Model1:就是一种纯jsp开发技术,将业务逻辑代码和视图渲染代码杂糅在一起. Model2:Model2是在Model1的基础上,将业务逻辑的代码分离开来,单独形 ...
- Winform(DataGridView)控件及通过此控件中实现增删改查
DataGridView:显示数据表,通过此控件中可以实现连接数据库,实现数据的增删改查 一.后台数据绑定: List<xxx> list = new List<xxx> ...