做英文网站的公司,网站点击换图片的效果怎么做,重庆专业网站推广报价,wordpress xmlseoWCF学习之旅—实现REST服务#xff08;二十二#xff09; WCF学习之旅—实现支持REST服务端应用#xff08;二十三#xff09; 在上二篇文章中简单介绍了一下RestFul与WCF支持RestFul所提供的方法#xff0c;及创建一个支持REST的WCF服务端程序#xff0c;本文介绍如何调… WCF学习之旅—实现REST服务二十二 WCF学习之旅—实现支持REST服务端应用二十三 在上二篇文章中简单介绍了一下RestFul与WCF支持RestFul所提供的方法及创建一个支持REST的WCF服务端程序本文介绍如何调用上一篇文章介绍的RestFul服务端。 五、Windows客户端调用 为了强调REST的通用性客户端不用WCF的形式调用服务而是采用HttpWebResponse通过编程方式直接访问消息格式我们选XML。 首先我们使用C#来封装一个RestHelper类实现HTTP的GET和POST的请求方法代码如下。 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;namespace WinClient
{public class RestHelper{ /// summary/// 构造函数/// /summary/// param namebaseUrl/parampublic RestHelper(string baseUri){this.BaseUri baseUri;} /// summary/// 基地址/// /summaryprivate string BaseUri; /// summary/// Post调用/// /summary/// param namedata/param/// param nameuri/param/// returns/returnspublic string Post(string data, string uri){//Web访问对象string serviceUrl string.Format({0}/{1}, this.BaseUri, uri);HttpWebRequest myRequest (HttpWebRequest)WebRequest.Create(serviceUrl);//转成网络流byte[] buf UnicodeEncoding.UTF8.GetBytes(data); //设置myRequest.Method POST;myRequest.ContentLength buf.Length;myRequest.ContentType text/html;// 发送请求Stream newStream myRequest.GetRequestStream();newStream.Write(buf, 0, buf.Length);newStream.Close(); // 获得接口返回值HttpWebResponse myResponse (HttpWebResponse)myRequest.GetResponse();StreamReader reader new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);string ReturnXml HttpUtility.HtmlDecode(reader.ReadToEnd());reader.Close();myResponse.Close();return ReturnXml;}/// summary/// Get调用/// /summary/// param nameuri/param/// returns/returnspublic string Get(string uri){//Web访问对象string serviceUrl string.Format({0}/{1}, this.BaseUri, uri);HttpWebRequest myRequest (HttpWebRequest)WebRequest.Create(serviceUrl);// 获得接口返回值HttpWebResponse myResponse (HttpWebResponse)myRequest.GetResponse();StreamReader reader new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);string ReturnXml HttpUtility.UrlDecode(reader.ReadToEnd());reader.Close();myResponse.Close();return ReturnXml;}}} 其次我们来实现主函数按顺序调用两个接口并显示返回值。需要注意XML约定的命名空间。 我们在visual studio 2015中创建一个Windows窗体名称为Form1在Form1中放两个按钮一个是“Rest Get”另一个是Rest Post。 1在“Rest Get”按钮中实现Get方法代码如下 private void buttonRest_Click(object sender, EventArgs e){RestHelper client new RestHelper(http://127.0.0.1:8888/);//Getstring uriGet string.Format(Books/Get/{0}, 2);string getData client.Get(uriGet);textBoxMsg.Text getData;} 2) 在visual studio 2015中启动客户端应用程序然后使用鼠标点击“Rest Get”按钮结果如下图。 3在“Rest Post”按钮中实现Post方法代码如下 private void buttonRestPost_Click(object sender, EventArgs e){RestHelper client new RestHelper(http://127.0.0.1:8888/);//Poststring uriPost Books/Add;string data Books xmlns\http://tempuri.org/\AuthorID1/AuthorIDCategoryMS/CategoryName数学之美(第二版) /NameNumberofcopies12/NumberofcopiesPrice37.99/PricePublishDate2009-01-11T00:00:00/PublishDateRatingA/Rating/Books;string postResult client.Post(data, uriPost);textBoxMsg.Text \r\n\r\n\r\n postResult;} 4) 在visual studio 2015中启动客户端应用程序然后使用鼠标点击“Rest Post”按钮结果如下图。 六、通过浏览器来访问WCF服务 通过浏览器来访问WCF服务主要是用jquery实现GET和POST访问采用jquery访问REST服务消息格式选择Xml。 1) 我们在项目目录下面创建一个testRest.html文件文件中的内容如下 htmlheadscript src../../Scripts/jquery-2.2.3.min.js typetext/javascript/scriptscript typetext/javascript function AjaxGet() {$.ajax({type: GET,contentType: text/xml,url: http://127.0.0.1:8888/Books/Get/5, success: function (data) {alert(data);$(#TextGet).val(data);},complete:function(XMLHttpRequest,textStatus){alert(XMLHttpRequest.responseText);alert(textStatus); },error: function (data) {alert(data);}});}function HttpPost() {var str Books xmlns\http://tempuri.org/\AuthorID1/AuthorIDCategoryMS/CategoryNamemath book ver 1 /NameNumberofcopies12/NumberofcopiesPrice47.99/PricePublishDate2012-01-11T00:00:00/PublishDateRatingA/Rating/Books;$.ajax({type: POST,contentType: text/xml,
// datatype:xml,url: http://127.0.0.1:8888/Books/Add,data: str,success: function (data) {alert(data);$(#TextPost).val(data);},complete:function(XMLHttpRequest,textStatus){alert(XMLHttpRequest.responseText);alert(textStatus); },error: function (data) {alert(data);}});}/scriptstyle typetext/css#TextGet{width: 700px;}#TextPost{width: 700px;}/style/headbodyinput idButtonGet typebutton valueGET onclickAjaxGet() /input idTextGet typetext /p/ input idButtonPost typebutton valuePOST onclickHttpPost() /input idTextPost typetext //body/html 2)使用浏览器IE打开testRest.html然后点击“ GET” 按钮结果如下图。 3)使用浏览器IE打开testRest.html然后点击“ POST” 按钮结果如下图。 备注 在firefox下面怎么访问都不成功都是报405Method not allowed错误信息在IE下面访问正常具体原因没找到如果有知道解决方案的请留言。 转载于:https://www.cnblogs.com/chillsrc/p/5874703.html