三种客户端访问wcf服务端的方法 C#
原文 http://blog.csdn.net/zlj002/article/details/7914556
string jsonstr = String.Empty;
string url = "http://localhost:7041/Service1/Hello";
#region WebClient 访问Get
WebClient webclient = new WebClient();
Uri uri = new Uri(url, UriKind.Absolute);
if (!webclient.IsBusy)
{
webclient.Encoding = System.Text.Encoding.UTF8;
jsonstr = webclient.DownloadString(url);
dynamic json = JsonHelper.Deserialize(jsonstr);
if (json.Length > 0)
{
this.Label1.Text = json[0]["StringValue"] + ">>>" + json[0]["Id"] + "WebClientGet";
} }
#endregion
#region WebClient 访问Post
url = "http://localhost:7041/Service1/GetList";
IDictionary<string, object> data = new Dictionary<string, object>
{
{"stringValue", "汉字汉字"}
};
byte[] postData = Encoding.UTF8.GetBytes(JsonHelper.Serialize(data));
WebClient clienttt = new WebClient();
clienttt.Headers.Add("Content-Type", "application/json");
clienttt.Headers.Add("ContentLength", postData.Length.ToString());
byte[] responseData = clienttt.UploadData(url, "POST", postData);
string rr = Encoding.UTF8.GetString(responseData);
dynamic jsontt = JsonHelper.Deserialize(rr);
if (jsontt["GetListResult"].Length > 0)
{
this.Label1.Text = jsontt["GetListResult"][0]["StringValue"] + ">>>" + jsontt["GetListResult"][0]["Id"] + "WebClientGetPost";
}
#endregion #region WebRequest Get 访问
url = "http://localhost:7041/Service1/Hello";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
string code = response.ContentType;
code = code.Split('=')[1];
using (Stream stream = response.GetResponseStream())
{
StreamReader sr = new StreamReader(stream, Encoding.GetEncoding
(code));
string retemp = sr.ReadToEnd();
dynamic jsonretemp = JsonHelper.Deserialize(retemp);
if (jsonretemp.Length > 0)
{
this.Label1.Text = jsonretemp[0]["StringValue"] + ">>>" + jsonretemp[0]["Id"] + "WebRequest Get";
} }
response.Close();
#endregion
#region WebRequest Post 访问
url = "http://localhost:7041/Service1/GetList";
WebRequest requestPost = WebRequest.Create(url);
requestPost.Method = "POST";
byte[] postDatarequestPost = Encoding.UTF8.GetBytes(JsonHelper.Serialize(data));
requestPost.ContentType = "application/json";
requestPost.ContentLength = postDatarequestPost.Length;
Stream dataStream = requestPost.GetRequestStream();
dataStream.Write(postDatarequestPost, 0, postDatarequestPost.Length);
dataStream.Close();
WebResponse responsePost = requestPost.GetResponse();
dataStream = responsePost.GetResponseStream();
StreamReader readerPost = new StreamReader(dataStream, Encoding.UTF8);
string ResponseFromServer = readerPost.ReadToEnd();
dynamic jsonttResponseFromServer = JsonHelper.Deserialize(ResponseFromServer);
if (jsonttResponseFromServer["GetListResult"].Length > 0)
{
this.Label1.Text = jsonttResponseFromServer["GetListResult"][0]["StringValue"] + ">>>" + jsonttResponseFromServer["GetListResult"][0]["Id"] + "WebRequestPost";
}
readerPost.Close();
dataStream.Close();
responsePost.Close();
#endregion #region HttpClient Get访问
url = "http://localhost:7041/Service1/Hello";
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage message = client.Get(url))
{
message.EnsureStatusIsSuccessful();
jsonstr = message.Content.ReadAsString();
dynamic json = JsonHelper.Deserialize(jsonstr); if (json.Length > 0)
{
this.Label1.Text = json[0]["StringValue"] + ">>>" + json[0]["Id"] + ">>>>HttpClientGet";
}
}
}
#endregion
#region HttpClient Post 访问
url = "http://localhost:7041/Service1/GetList";
HttpClient clientPost = new HttpClient();
clientPost.DefaultHeaders.Add("ContentType", "application/json");
clientPost.DefaultHeaders.Add("Accept", "application/json"); HttpContent content = HttpContent.Create(JsonHelper.Serialize(data), Encoding.UTF8, "application/json");
HttpResponseMessage responseMessage = clientPost.Post(url, content);
if (responseMessage.StatusCode != HttpStatusCode.OK && responseMessage.StatusCode != HttpStatusCode.Accepted)
{
//出错
}
responseMessage.EnsureStatusIsSuccessful();
string result = responseMessage.Content.ReadAsString();
dynamic jsonpost = JsonHelper.Deserialize(result); if (jsonpost["GetListResult"].Length > 0)
{
this.Label1.Text = jsonpost["GetListResult"][0]["StringValue"] + ">>>" + jsonpost["GetListResult"][0]["Id"] + ">>>>HttpClientPost";
}
#endregion 服务端代码要注意配置属性,比如 [csharp] view plaincopy [WebInvoke(UriTemplate = "GetList", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,BodyStyle =
三种客户端访问wcf服务端的方法 C#的更多相关文章
- Webservice客户端动态调用服务端功能方法
一.发布WebService服务 方式一:在服务端生成wsdl文件,下方客户端直接引用即可 优点:针对要发布的方法生成一个wsdl文件即可,无需多余配置. 缺点:每次服务端方法发生改变都需 ...
- C++客户端访问Java服务端发布的SOAP模式的WebService接口
gSOAP是一个绑定SOAP/XML到C/C++语言的工具,使用它可以 简单快速地开发出SOAP/XML的服务器端和客户端 Step1 使用gsoap-2.8\gsoap\bin\win32\wsdl ...
- 客户端使用自定义代理类访问WCF服务 z
通常在客户端访问WCF服务时,都需要添加服务引用,然后在客户端app.config或 web.config文件中产生WCF服务的客户端配置信息.若是每添加一个服务都是这样做,这样势必会将比较麻烦,能否 ...
- 客户端使用自定义代理类访问WCF服务
通常在客户端访问WCF服务时,都需要添加服务引用,然后在客户端app.config或web.config文件中产生WCF服务的客户端配置信息.若是每添加一个服务都是这样做,这样势必会将比较麻烦,能否简 ...
- 学习之路十四:客户端调用WCF服务的几种方法小议
最近项目中接触了一点WCF的知识,也就是怎么调用WCF服务,上网查了一些资料,很快就搞出来,可是不符合头的要求,主要有以下几个方面: ①WCF的地址会变动,地址虽变,但是里面的逻辑不变! ②不要引用W ...
- WCF服务端开发和客户端引用小结
1.服务端开发 1.1 WCF服务创建方式 创建一个WCF服务,总是会创建一个服务接口和一个服务接口实现.通常根据服务宿主的不同,有两种创建方式. (1)创建WCF应用程序 通过创建WCF服务应用程序 ...
- WCF开发实战系列三:自运行WCF服务
WCF开发实战系列三:自运行WCF服务 (原创:灰灰虫的家 http://hi.baidu.com/grayworm)上一篇文章中我们建立了一个WCF服务站点,为WCF服务库运行提供WEB支持,我们把 ...
- android客户端app和服务端交互token的作用
Android客户端和服务端如何使用Token和Session niceheart关注1人评论34644人阅读2014-09-16 16:38:44 对于初学者来说,对Token和Session的 ...
- 三种主流的Web服务实现方案(REST+SOAP+XML-RPC)简述及比较
目前知道的三种主流的Web服务实现方案为:REST:表象化状态转变 (软件架构风格)SOAP:简单对象访问协议 XML-RPC:远程过程调用协议 下面分别作简单介绍: REST:表征状态转移(Repr ...
随机推荐
- 状态压缩DP------学习小记
状态DP主要用的还是DP思想,顾名思义,加了一个状态,主要是用来求状态个数的. 状态是用二进制数来表示的,也就是用0或1来表示,每一行有一个状态数,就是由这一行的0或1组成的,首先我们要获得每行的状态 ...
- [树结构]平衡二叉树AVL
平衡二叉树是一种二叉排序树,其中每一个节点的左子树和右子树的高度至多等于1,平衡二叉树又称为AVL树. 将二叉树节点的左子树深度减去右子树深度的值称为平衡因子BF,平衡二叉树上所有节点的平衡因子只可能 ...
- C++ 获取UUID
#include <string> #include <stdio.h> #if defined(WIN32)||defined(WINCE)||defined(WIN64) ...
- hadoop笔记之Hive的数据类型
Hive的数据类型 Hive的数据类型 前面说过,Hive是一个数据仓库,相当于一个数据库.既然是数据库,那么就必须能创建表,既然有表,那么当中就有列,列中就有对应的类型 总的来讲,hive的数据类型 ...
- MVC自学第三课
上一课我们已经能够做出简单的HTML显示页面,并懂得了MVC的工作模式,这一课我们讲解一些动态的数据交互. 在MVC中,控制器的工作是构造某些数据,而视图的工作是把它渲染成HTML.数据是从控制器传递 ...
- delphi 实现vip126发邮件
本例是 TSimpleThread , TSimpleList, IdhttpEx 网页模拟(POST)的综合运用. Demo只写了发送,但亦可收取,详见源码. (此源码写于2年前,那时还写得不好,请 ...
- at org.apache.jsp.index_jsp._jspInit(index_jsp.java:23)异常解决
部署项目,启动tomcat一切正常.输入项目地址后 tomcat报例如以下错误: java.lang.NullPointerException at org.apache.jsp.index_jsp. ...
- 从零开始Unity3D游戏开发【4 材质球和渲染纹理】
[创建材质球] 1.Project 面板下 create-Material 然后将材质球拖放到物体,物体的颜色便会和材质球相同: [渲染纹理 RenderTexture]
- 交互设计师谈颠覆式创新 | Think different
作者:Teambition 交互设计师 樊伟 本文由 Teambition 原创.转载请注明出处,附原文链接 题图:by Ed Chao 我们不需要像主流市场的大公司一样做类似相扑的庞大,而是需要像柔 ...
- .cs文件与aspx.cs文件之间的区别是什么???他们的作用是什么???ASPX文件的作用是什么?
一般在vs里面新建一个页面会产生两种文件:一种是后缀名为.cs的,一种是.aspx. 简单的说,.cs文件一般是在里面实现功能的,而.aspx就是实现界面效果的. 区别:.cs文件里面写的是.net的 ...