三种客户端访问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 ...
随机推荐
- day9_python学习笔记_chapter12_模块
1. 名称空间加载顺序: 首先加载内建名称空间,他由__builtin模块中的名字构成.然后加载执行模块的全局名称空间,他会在模块开始执行后变为活动名称空间.如 果在执行期间调用了一个函数,那么将创建 ...
- ueditor插件 -- 插入填空题
插入填空题,一个看似小小的需求,但是却是折腾了很9.主要产品那边的要求,空格上面要标有序号,并且再页面当中能够同步空格答案列表. 1.ueditor插件 插件入门,官方的例子还是很简单直接的,对于我们 ...
- IP地址、子网掩码和地址分类
http://blog.csdn.net/bluishglc/article/details/47909593?utm_source=tuicool&utm_medium=referral 实 ...
- 【Windows 8 Store App】学习三:HTTP
原文 http://www.cnblogs.com/java-koma/archive/2013/05/22/3093309.html 1,HttpClient Win 8提供了System.Net. ...
- poj 3259 (Bellman_Ford判断负环)
题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...
- Protection 5 ---- Priviliege Level Checking 2
CPU不仅仅在程序访问数据段和堆栈段的时候进行权限级别检查,当程序控制权转换的时候也会进行权限级别检查.程序控制权转换的情况很多,各种情况下检查的方式以及涉及到的检查项都是不同的.这篇文章主要描述了各 ...
- ocx控件获取使用App的窗口句柄
在CXxxCtrl文件中 HWND hAppWnd = NULL; if (m_pInPlaceSite != NULL) m_pInPlaceSite->GetWindow(&hApp ...
- POJ 1077 HDU 1043 Eight (IDA*)
题意就不用再说明了吧......如此经典 之前想用双向广搜.a*来写,但总觉得无力,现在用IDA*感觉其他的解法都弱爆了..............想法活跃,时间,空间消耗很小,给它跪了 启发式搜索关 ...
- 解密:LL与LR解析 2(译,完结)
由于GFW,我无法联系到作者,所以没有授权,瞎翻译的.原文在这里[http://blog.reverberate.org/2013/07/ll-and-lr-parsing-demystified.h ...
- 关于html5之canvas的那些事
何为canvas <canvas> 标签只是图形容器,您必须使用脚本来绘制图形.默认情况下该矩形区域宽为300像素,高为150像素,设置宽高必须在canvas标签内部,不能加单位px. 大 ...