对于网站与网站之间数据互动,这是我的说法,不是专家说的,不要相信。应该有专业的说法。

从他人的网站通过一个接口获取数据,这一直是我感到神奇的事,怎么实现的,一直萦绕于心,想要弄过究竟,怎么是实现的啊!不止一次在群里听到同样的提问。可惜每次我都没有深入探究,自己太懒惰了,不想动手实践,想找现成的,可惜也没有去找。

还是要工作压力驱使,不然也许这个一直对我来说是一个疑惑。工作中有这么一个任务就是通过接口去获取供应商的数据,还好已经有现成的了,我只要看懂,然后模仿做一个就可以了,不过也费了我不少时间。发现有两种方式:一是通过xml,二是通过web service。我做的是用xml实现,而另外一种当时就没有深入去探究了。但是在我心里一直是一个结,我要试一试,实现它。于是有了今天的结果。一个小小实验。

我是用asp.net 4.0实验的 两个项目,模拟两个网站。其实也包含了几个知识点:web Service,数据压缩,流数据等,dataset 与流的互转化。

web Service 端代码,从数据库中获取数据,然后使用了压缩,流传递数据

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO.Compression; namespace WebSiteTest
{
/// <summary>
/// WebServiceSend 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebServiceSend : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
} [WebMethod]
public int add( int a, int b)
{
return a + b;
} //用流发送数据
[WebMethod]
public byte[] getdata(int id)
{
byte[] bArrayResult = null;
string sqlstr = "select * from BlogArticle where BlogId>" + id;
DataSet ds=new DataSet();
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = sqlstr;
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
SqlDataAdapter dap = new SqlDataAdapter(cmd);
dap.Fill(ds);
ds.RemotingFormat = SerializationFormat.Binary;
MemoryStream ms = new MemoryStream();
IFormatter bf = new BinaryFormatter();
bf.Serialize(ms, ds);
bArrayResult = ms.ToArray();
ms.Close();
return Compress(bArrayResult);
//return dt.Rows[0]["BlogTitle"].ToString();
}
} } //[WebMethod]
//public byte[] getgzipdata(int id)
//{
// byte[] bArrayResult = null;
// string sqlstr = "select * from BlogArticle where BlogId>" + id;
// DataSet ds = new DataSet();
// using (SqlConnection conn = new SqlConnection(connstr))
// {
// conn.Open();
// using (SqlCommand cmd = new SqlCommand())
// {
// cmd.CommandText = sqlstr;
// cmd.CommandType = CommandType.Text;
// cmd.Connection = conn;
// SqlDataAdapter dap = new SqlDataAdapter(cmd);
// dap.Fill(ds);
// ds.RemotingFormat = SerializationFormat.Binary; // MemoryStream ms = new MemoryStream();
// IFormatter bf = new BinaryFormatter();
// bf.Serialize(ms, ds);
// bArrayResult = ms.ToArray();
// ms.Close();
// return bArrayResult;
// //return dt.Rows[0]["BlogTitle"].ToString();
// }
// }
//}
//压缩流数据
public static byte[] Compress(byte[] bytes)
{
using (MemoryStream ms = new MemoryStream())
{
GZipStream Compress = new GZipStream(ms, CompressionMode.Compress); Compress.Write(bytes, , bytes.Length); Compress.Close(); return ms.ToArray(); }
}
private static string connstr = "Data Source=.;Initial Catalog=mytest;Integrated Security=True";//数据库连接 }
}

压缩前数据大小如图片一

压缩后数据大小如图片二

接收端 代码,需要web引用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression; namespace WebReceivetest
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void btntest_Click(object sender, EventArgs e)
{
MyWebTest.WebServiceSend websend = new MyWebTest.WebServiceSend();
int first = int.Parse(txtfirst.Text);
int second = int.Parse(txtsecond.Text);
int result = websend.add(first, second);
labresult.Text = result.ToString();
//string xx=websend.getdata(10);
//labresult.Text = result.ToString() + "-----------" + xx;
DataSet ds = BinaryToDataset(websend.getdata());//151是数据库中的一个id编号
GridView1.DataSource = ds;
GridView1.DataBind(); }
//流转换为dataset数据
public DataSet BinaryToDataset(byte[] bUserData)
{
if (bUserData == null)
{
//MessageBox.Show("二进制数据流为空");
//err = "";
return null;
}
// 反序列化的过程
MemoryStream ms = new MemoryStream(Decompress(bUserData));
IFormatter bf = new BinaryFormatter();
object obj = bf.Deserialize(ms);
DataSet dsResult = (DataSet)obj;
ms.Close();
return dsResult;
} //解压流数据
public static byte[] Decompress(Byte[] bytes)
{
using (MemoryStream tempMs = new MemoryStream())
{
using (MemoryStream ms = new MemoryStream(bytes))
{
GZipStream Decompress = new GZipStream(ms, CompressionMode.Decompress); Decompress.CopyTo(tempMs); Decompress.Close(); return tempMs.ToArray();
}
}
}
}
}

解压前数据大小如图片三

压缩后数据大小如图片四

通过这一次小实验,心中的疑惑完全消除了。实践——求真——消除疑惑。

参考:http://www.cnblogs.com/zhangzheny/archive/2007/06/16/785734.html

http://www.cnblogs.com/amylis_chen/archive/2012/11/06/2757808.html

http://www.cnblogs.com/qugangf/archive/2011/04/11/2012193.html

Web Service 小练习的更多相关文章

  1. 使用TcpTrace小工具截获Web Service的SOAP报文

    Web Service客户端对服务端进行调用时,请求和响应都使用SOAP报文进行通讯.在开发和测试时,常常查看SOAP报文的内容,以便进行分析和调试.TcpTrace是一款比较小巧的工具,可以让我们截 ...

  2. Web Service测试工具小汇

    1..NET WebService Studio 这款工具出自微软内部,最大的优点是可视化很好,不用去看那些XML文件,WebService的基础内容就有XML,但是测试中Case过多,每次测试结果都 ...

  3. .NET基础拾遗(7)Web Service的开发与应用基础

    Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基 ...

  4. ORACLE存储过程调用Web Service

    1. 概述 最近在ESB项目中,客户在各个系统之间的服务调用大多都是在oracle存储过程中进行的,本文就oracle存储过程调用web service来进行说明.其他主流数据库,比如mysql和sq ...

  5. Web Service 中返回DataSet结果大小改进

    http://www.cnblogs.com/scottckt/archive/2012/11/10/2764496.html Web Service 中返回DataSet结果方法: 1)直接返回Da ...

  6. WCF、Net remoting、Web service概念及区别

    Windows通信基础(Windows Communication Foundation,WCF)是基于Windows平台下开发和部署服务的软件开发包(Software Development Kit ...

  7. Web Service简介 内部资料 请勿转载 谢谢合作

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  8. 什么是web service

    一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...

  9. Web Service 的工作原理

    Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...

随机推荐

  1. I Hate It(线段数组基础题)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. Centos7 配置网络步奏详解

    Centos7 配置网络步奏详解 编辑网卡配置文件 vi /etc/sysconfig/network-script/ifcfg-ens01 备注:这里的ens01不是所有系统都叫这个,有的可能叫其他 ...

  3. Hadoop 文件的数量怎么比block的数量多?

    Total files:    23 Total symlinks:        0 Total blocks (validated):    22 (avg. block size 117723 ...

  4. Elastic search入门

    首先是下载elasticsearch https://www.elastic.co/downloads,解压: 然后下载了中文分析器ik,github上搜索elasticsearch-ik就能找到,h ...

  5. CSS设置滚动条样式

    因为在现在的大部分项目中很多都用到了滚动条,有时候用到模拟的滚动条,现在说下滚动条的CSS也能解决. 比如网易邮箱的滚动条样子很好看,就是利用的CSS来设置的,而且是webkit浏览器的.如图所示: ...

  6. UESTC 885 方老师买表 --状压DP

    将方格的摆放分成两种: 1.水平摆放:此时所占的两个格子都记为1. 2.竖直摆放:此时底下那个格子记为1,上面那个记为0. 这样的话,每行都会有一个状态表示. 定义:dp[i][s]表示考虑已经填到第 ...

  7. 2014 Super Training #1 C Ice-sugar Gourd 模拟,扫描线

    原题 HDU 3363 http://acm.hdu.edu.cn/showproblem.php?pid=3363 给你一个串,串中有H跟T两种字符,然后切任意刀,使得能把H跟T各自分为原来的一半. ...

  8. QC学习二:QC使用中问题点汇总

    QC 使用中问题点汇总,包括以下方面: 1.不兼容IE7,IE8的问题(服务器端设置) 2.无法在Win 7下正常下载页面(客户端设置) 3.在QC中填写中文内容后无法正常提交到数据库(客户端设置) ...

  9. S2小测--索引--视图

    1.  关于Sql server 视图 视图是一个虚拟表,我们在查询视图的时候,实际上是对基础表的查询.视图不仅可以作为SELECT查询的目标,也可以作为修改语句的目 标.理论上它可以像普通的物理表一 ...

  10. RDLC系列之二 子报表

    本文实现简单的子报表 一.效果图