vs2015开发webserver 返回json格式 实现图书编目数据接口
来源:互联网 发布:linux 文件内容排序 编辑:程序博客网 时间:2024/05/22 06:51
开发工具:vs2015社区版(免费哦)
需求:客户开发网站或者APP想通过isbn查询图书信息 需要返回json格式 。
因为本人专业搞图书编目marc数据,有专门的批量下载客户端 但这个是 delphi开发的 cs产品并不能满足客户需求。所以打算用vs2015来做个webserver实现。
接口类似这种应用聚合api,这也是客户提供给我参考的当时。我发现虽然这个公司融资2亿多 但命中率实在不高 。我的接口也开发完了命中率99%,测试地址
http://221.204.187.218:1605/WebService1.asmx/GetBookInfo?isbn=978-7-301-26746-2 参数isbn支持isbn 10/13位格式。
返回格式:
上代码
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Script.Serialization;using System.Data;using System.Data.SqlClient;using System.Text;namespace WebApplication1{ /// <summary> /// WebService1 的摘要说明 /// </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 WebService1 : System.Web.Services.WebService { //声明Soap头实例 public MySoapHeader myHeader = new MySoapHeader(); [System.Web.Services.Protocols.SoapHeader("myHeader")] [WebMethod] public string GetBookInfo(string isbn) { if (isbn.Trim() == "") { Message re = new Message(); re.bingo = "0"; return new JavaScriptSerializer().Serialize(re); } if ((isbn.IndexOf("-") > 0) && ((isbn.Length) == 17)) { isbn = isbn.Replace("-", ""); } else if ((isbn.IndexOf("-") > 0) && ((isbn.Length) == 13)) { isbn = isbn.Replace("-", ""); isbn = isbn10toean(isbn); } else if ((isbn.IndexOf("-") < 0) && ((isbn.Length) == 10)) { isbn = isbn10toean(isbn); } if (myHeader.UserName.Equals("taide") & myHeader.PassWord.Equals("348007237")) { //指定Sql Server提供者的连接字符串 string connString = "server=WIN-4864693DQZ7;database =marcstore;uid =sa;pwd=xxx"; //建立连接对象 SqlConnection Sqlconn = new SqlConnection(connString); //打开连接 Sqlconn.Open(); ////为上面的连接指定Command对象 SqlCommand thiscommand = Sqlconn.CreateCommand(); thiscommand.CommandText = "select * from nlc_api where [010a]='" + isbn + "'"; ////为指定的command对象执行DataReader SqlDataReader thisSqlDataReader = thiscommand.ExecuteReader(); if (thisSqlDataReader.HasRows) { Message re = new Message(); re.bingo = "1"; List<BookInfo> mybook = new List<BookInfo>(); //Booklist bl = new Booklist(); while (thisSqlDataReader.Read()) { BookInfo book1 = new BookInfo(); book1.f010a = thisSqlDataReader["010a"].ToString(); book1.f010b = thisSqlDataReader["010b"].ToString(); book1.f010d = thisSqlDataReader["010d"].ToString(); book1.f101a = thisSqlDataReader["101a"].ToString(); book1.f102a = thisSqlDataReader["102a"].ToString(); book1.f200a = thisSqlDataReader["200a"].ToString(); book1.f200d = thisSqlDataReader["200d"].ToString(); book1.f200e = thisSqlDataReader["200e"].ToString(); book1.f200f = thisSqlDataReader["200f"].ToString(); book1.f200g = thisSqlDataReader["200g"].ToString(); book1.f200h = thisSqlDataReader["200h"].ToString(); book1.f200i = thisSqlDataReader["200i"].ToString(); book1.f200v = thisSqlDataReader["200v"].ToString(); book1.f210a = thisSqlDataReader["210a"].ToString(); book1.f210c = thisSqlDataReader["210c"].ToString(); book1.f210d = thisSqlDataReader["210d"].ToString(); book1.f215a = thisSqlDataReader["215a"].ToString(); book1.f215d = thisSqlDataReader["215d"].ToString(); book1.f330a = thisSqlDataReader["330a"].ToString(); book1.f606a = thisSqlDataReader["606a"].ToString(); book1.f690a = thisSqlDataReader["690a"].ToString(); mybook.Add(book1); } re.books = mybook; thisSqlDataReader.Close(); ////关闭连接 Sqlconn.Close(); return new JavaScriptSerializer().Serialize(re); } else { //国图未命中 查询cip库 thisSqlDataReader.Close(); ////关闭连接 //Sqlconn.Close(); thiscommand.CommandText = "select * from cip_api where [010a]='" + isbn + "'"; ////为指定的command对象执行DataReader SqlDataReader dr_cip = thiscommand.ExecuteReader(); if (dr_cip.HasRows) { Message re = new Message(); re.bingo = "1"; List<BookInfo> mybook = new List<BookInfo>(); //Booklist bl = new Booklist(); while (dr_cip.Read()) { BookInfo book1 = new BookInfo(); book1.f010a = dr_cip["010a"].ToString(); book1.f010b = dr_cip["010b"].ToString(); book1.f010d = dr_cip["010d"].ToString(); book1.f101a = dr_cip["101a"].ToString(); book1.f102a = dr_cip["102a"].ToString(); book1.f200a = dr_cip["200a"].ToString(); book1.f200d = dr_cip["200d"].ToString(); book1.f200e = dr_cip["200e"].ToString(); book1.f200f = dr_cip["200f"].ToString(); book1.f200g = dr_cip["200g"].ToString(); book1.f200h = dr_cip["200h"].ToString(); book1.f200i = dr_cip["200i"].ToString(); book1.f200v = dr_cip["200v"].ToString(); book1.f210a = dr_cip["210a"].ToString(); book1.f210c = dr_cip["210c"].ToString(); book1.f210d = dr_cip["210d"].ToString(); book1.f215a = dr_cip["215a"].ToString(); book1.f215d = dr_cip["215d"].ToString(); book1.f330a = dr_cip["330a"].ToString(); book1.f606a = dr_cip["606a"].ToString(); book1.f690a = dr_cip["690a"].ToString(); mybook.Add(book1); } re.books = mybook; dr_cip.Close(); ////关闭连接 Sqlconn.Close(); return new JavaScriptSerializer().Serialize(re); } else { dr_cip.Close(); ////关闭连接 Sqlconn.Close(); Message re = new Message(); re.bingo = "0"; //Context.Response.Write(new JavaScriptSerializer().Serialize(re)); // Context.Response.End(); return new JavaScriptSerializer().Serialize(re); }; }; } else { return "对不起,您没有权限调用此服务!"; }; } public string isbn10toean(string isbn) { int sumOdd, sumEvEn, tmp; sumOdd = 0; sumEvEn = 0; string s = "978" + isbn.Substring(0, isbn.Length - 1); for (int i = 0; i < s.Length; i++) { if ((i % 2) == 0) { sumEvEn = sumEvEn + int.Parse(s.Substring(i, 1)); } else { sumOdd = sumOdd + int.Parse(s.Substring(i, 1)); } } tmp = sumEvEn + (sumOdd * 3); string tmp1 = tmp.ToString(); tmp1 = tmp1.Substring(tmp1.Length - 1, 1); tmp = 10 - int.Parse(tmp1); //tmp = 10 - (sumEvEn + (sumOdd * 3)).ToString().Substring(length-1,1); if (tmp == 10) { tmp = 0; } return s + tmp.ToString(); } public class MySoapHeader : System.Web.Services.Protocols.SoapHeader { private string userName = string.Empty; private string passWord = string.Empty; /// <summary> /// 构造函数 /// </summary> public MySoapHeader() { } /// <summary> /// 构造函数 /// </summary> /// <param name="userName">用户名</param> /// <param name="passWord">密码</param> public MySoapHeader(string userName, string passWord) { this.userName = userName; this.passWord = passWord; } /// <summary> /// 获取或设置用户用户名 /// </summary> public string UserName { get { return userName; } set { userName = value; } } /// <summary> /// 获取或设置用户密码 /// </summary> public string PassWord { get { return passWord; } set { passWord = value; } } } public class Message { public string bingo; //public Booklist books; public List<BookInfo> books; } public class Booklist { public List<BookInfo> Booklists { get; set; } } public class BookInfo { public string f010a { get; set; } public string f200a { get; set; } public string f200d { get; set; } public string f200e { get; set; } public string f200f { get; set; } public string f200g { get; set; } public string f200h { get; set; } public string f200i { get; set; } public string f200v { get; set; } public string f010d { get; set; } public string f101a { get; set; } public string f102a { get; set; } public string f210a { get; set; } public string f210c { get; set; } public string f210d { get; set; } public string f215a { get; set; } public string f215d { get; set; } public string f330a { get; set; } public string f606a { get; set; } public string f690a { get; set; } public string f010b { get; set; } } }}开始 使用了 json.net来返回json 因为是三方的 后来改用JavaScriptSerializer 方法。
代码里面 包含了 SOAP用户名密码认证 这样 提供给用户wsdl与用户名密码就可以了 。参考 http://www.cnblogs.com/mr-lee1976/p/4660159.html
函数 isbn10toean 是isbn10位转 isbn13位的 函数 主要是计算校验位
有需要图书数据接口的跟我联系 QQ:976809488
1 0
- vs2015开发webserver 返回json格式 实现图书编目数据接口
- 接口返回值 Json格式数据
- 返回json格式数据
- .net http post接口返回json格式数据
- 请求第三方接口返回json格式数据的解析
- SSM编写http接口返回JSON格式数据
- JSON API免费接口 各种提供JSON格式数据返回服务网站的API接口
- spring mvc实现Restful返回json格式数据
- spring mvc实现Restful返回json格式数据
- Spring MVC 实现RESTful 返回JSON格式数据
- spring mvc实现Restful返回json格式数据
- 通过gradle生成SpringMVC实现RESTful返回JSON格式数据
- php返回json格式数据
- Servlet返回JSON格式数据
- Struts返回Json格式数据
- 采用Json格式返回数据
- Servlet返回JSON格式数据
- django 返回json格式数据
- 网络安全-已解密的登录请求,会话标识未更新,跨站点请求伪造
- 欢迎使用CSDN-markdown编辑器
- 样本分类:ELM算法+软最大化算法+模糊积分算法
- Linux进程通讯:信号(signal)
- BHO
- vs2015开发webserver 返回json格式 实现图书编目数据接口
- 更新和迁移phabricator
- Spring Boot实战之配置使用Logback进行日志记录
- 第三方支付网银支付流程
- myeclipse中mybatis generator插件的手动安装即使用
- android]ShareSDK——内容分享和短信验证
- 八大排序算法 Java、Python、C++实现 -- 冒泡排序
- iOS 为什么要用weak修饰代理(delegate)
- iOS常用的第三方