本文旨在实现如何在.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实例的更多相关文章

  1. 图文解说:Nginx+tomcat配置集群负载均衡

    图文解说:Nginx+tomcat配置集群负载均衡 博客分类: appserver nginxTomcatUbuntuLinux网络应用  作者:niumd Blog:http://ari.iteye ...

  2. Android NDK r8 Cygwin CDT 在window下开发环境搭建 安装配置与使用 具体图文解说

    版权声明:本博客全部文章均为原创.欢迎交流.欢迎转载:转载请勿篡改内容,而且注明出处,谢谢! https://blog.csdn.net/waldmer/article/details/3272500 ...

  3. (转)xcode5.0.2下国际化图文解说

    原文:http://blog.csdn.net/dragoncheng/article/details/6703311 xcode5.0.2下国际化图文解说         分类:           ...

  4. Don’t Use Accessor Methods in Initializer Methods and dealloc 【初始化和dealloc方法中不要调用属性的存取方法,而要直接调用 _实例变量】

    1.问题:    在dealloc方法中使用[self.xxx release]和[xxx release]的区别? 用Xcode的Analyze分析我的Project,会列出一堆如下的提示:Inco ...

  5. Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例

    引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...

  6. NET异步调用Webserver

    之前,有个同事跑来问我一堆的什么多线程异步进行调用Sap的服务再突然把进程关闭,还说要设置一个循环判断调用的结果,搞得我听的一头雾水,但是我明显感觉到他的设计思路已经渐行渐远了...已经再偏远的山区中 ...

  7. PHP调用Webservice实例

    原文 PHP调用Webservice实例 NuSoap是PHP环境下的WebService编程工具,用于创建或调用WebService.它是一个开源软件,是完全采用PHP语言编写的.通过HTTP收发S ...

  8. 转载 基于JAVA每月运势api调用代码实例

    代码描述:基于JAVA每月运势api调用代码实例 接口地址:http://www.juhe.cn/docs/api/id/58 原文链接:http://outofmemory.cn/code-snip ...

  9. 调用webserver时出现:请求因 HTTP 状态 401 失败: Unauthorized。

    请求因 HTTP 状态 401 失败: Unauthorized 今天在调用webserver时出现了上述标题的错误,开始认为是由于端口的问题,我把端口恢复80默认端口后,但是问题并没有解决!后来我自 ...

随机推荐

  1. TeamCity实战(1):准备工作

    环境: Windows Server 2008 R2 SP1(不会再有SP2,参考这里:http://www.techspot.com/news/50599-microsoft-wont-releas ...

  2. XtraBackup出现 Can't connect to local MySQL server through socket '/tmp/mysql.sock'

    Xtrabackup做备份时遇到下面错误信息MySQL server: Can't connect to local MySQL server through socket '/tmp/mysql.s ...

  3. MS SQL 2008 发布订阅配置错误总结

          最近在配置SQL 2008的发布订阅功能时,遇到了几个小错误,顺便归纳总结一下(以后碰到各类关于发布订阅的错误都将收录.更新到这篇文章),方便自己在以后碰到这类问题时,能够迅速解决问题.毕 ...

  4. SQL SERVER Transactional Replication中添加新表如何不初始化整个快照

    在SQL SERVER的复制(Replication)中,有可能出现由于业务需求变更,需要新增一张表或一些表到已有的复制(发布订阅)当中,这种需求应该是很正常,也很常见的.但是在已有的复制(发布订阅) ...

  5. .NET应用架构设计—用户端的防腐层作用及设计

    阅读目录: 1.背景介绍 2.SOA架构下的显示端架构腐化 3.有效使用防腐层来隔离碎片服务导致显示端逻辑腐烂 4.剥离服务调用的技术组件让其依赖接口 5.将服务的DTO与显示端的ViewModel之 ...

  6. Python魔术方法-Magic Method

    介绍 在Python中,所有以"__"双下划线包起来的方法,都统称为"Magic Method",例如类的初始化方法 __init__ ,Python中所有的魔 ...

  7. linux黄金命令[积累中]

    /usr/share/fonts/ fc-cache -v -f sudo chmod 777 ./* sudo mkfontscale sudo mkfontdir sudo fc-cache -f ...

  8. Source Insight常用功能设置

    熟悉工具的使用能在一定程度上提高工作效率,但工具永远只是工具,大家要把重点放在内功的修炼上. 符号导航 符号(变量.宏定义.结构定义.枚举.函数等等)在SI 中的检索非常方便.Ctrl+鼠标左键或Ct ...

  9. Maven设置代理服务器

    在setting.xml中设置以下信息即可,请修正对应信息 <proxies> <proxy> <id>optional</id> <active ...

  10. 框架基础之Hibernate简介

    框架基础之Hibernate简介 1.什么是Hibernate? Hibernate是一个开发源代码的对象关系映射框架,它对JDBC进行非常轻量级的对象封装,使得程序员可以随心所欲地使用对象编程思维来 ...