原文 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#的更多相关文章

  1. Webservice客户端动态调用服务端功能方法

    一.发布WebService服务 方式一:在服务端生成wsdl文件,下方客户端直接引用即可     优点:针对要发布的方法生成一个wsdl文件即可,无需多余配置.   缺点:每次服务端方法发生改变都需 ...

  2. C++客户端访问Java服务端发布的SOAP模式的WebService接口

    gSOAP是一个绑定SOAP/XML到C/C++语言的工具,使用它可以 简单快速地开发出SOAP/XML的服务器端和客户端 Step1 使用gsoap-2.8\gsoap\bin\win32\wsdl ...

  3. 客户端使用自定义代理类访问WCF服务 z

    通常在客户端访问WCF服务时,都需要添加服务引用,然后在客户端app.config或 web.config文件中产生WCF服务的客户端配置信息.若是每添加一个服务都是这样做,这样势必会将比较麻烦,能否 ...

  4. 客户端使用自定义代理类访问WCF服务

    通常在客户端访问WCF服务时,都需要添加服务引用,然后在客户端app.config或web.config文件中产生WCF服务的客户端配置信息.若是每添加一个服务都是这样做,这样势必会将比较麻烦,能否简 ...

  5. 学习之路十四:客户端调用WCF服务的几种方法小议

    最近项目中接触了一点WCF的知识,也就是怎么调用WCF服务,上网查了一些资料,很快就搞出来,可是不符合头的要求,主要有以下几个方面: ①WCF的地址会变动,地址虽变,但是里面的逻辑不变! ②不要引用W ...

  6. WCF服务端开发和客户端引用小结

    1.服务端开发 1.1 WCF服务创建方式 创建一个WCF服务,总是会创建一个服务接口和一个服务接口实现.通常根据服务宿主的不同,有两种创建方式. (1)创建WCF应用程序 通过创建WCF服务应用程序 ...

  7. WCF开发实战系列三:自运行WCF服务

    WCF开发实战系列三:自运行WCF服务 (原创:灰灰虫的家 http://hi.baidu.com/grayworm)上一篇文章中我们建立了一个WCF服务站点,为WCF服务库运行提供WEB支持,我们把 ...

  8. android客户端app和服务端交互token的作用

    Android客户端和服务端如何使用Token和Session niceheart关注1人评论34644人阅读2014-09-16 16:38:44   对于初学者来说,对Token和Session的 ...

  9. 三种主流的Web服务实现方案(REST+SOAP+XML-RPC)简述及比较

    目前知道的三种主流的Web服务实现方案为:REST:表象化状态转变 (软件架构风格)SOAP:简单对象访问协议 XML-RPC:远程过程调用协议 下面分别作简单介绍: REST:表征状态转移(Repr ...

随机推荐

  1. php使用正则

    $str = '2';echo preg_match('/^\d{1,10}$/i',$str);//匹配数字,最少1位,最多10位,返回1,否则返回0;

  2. xcode7 没有Empty Application

    如果你想创建xcode6.01之前版本提供的空工程,其实很简单.1:选择模板 Single View Application2:选中 Main.storyboard,将其删除3:选择项目的 plist ...

  3. nodeJs入门笔记(二)

    js中window通常是全局变量 global 是node.js里的全局变量 node中能访问的对象一般都是 global的 属性 global 对象属性 process 用于描述当前Node 进程状 ...

  4. Flot chart学习笔记

    背景及相关简介 在最近的BS新项目中需要用到绘图数据显示的功能.在进行充足的选择之后决定才去开源的Flot.Flot是一个jQuery绘图库.主要用于简单的绘制图表功能.具有吸引人的渲染外观和互操作的 ...

  5. JDBC连接数据库概述

    直接介绍JDBC连接数据库的流程及其原理 创建一个以JDBC连接数据库的程序,包含7个步骤 1.加载JDBC数据库驱动 在连接数据库之前,首先要加载想要连接的数据库的驱动,就是数据库厂商提供的jar包 ...

  6. [LeetCode]题解(python):152-Maximum Product Subarray

    题目来源: https://leetcode.com/problems/maximum-product-subarray/ 题意分析: 给定一个数组,这个数组所有子数组都有一个乘积,那么返回最大的乘积 ...

  7. vb socket的使用

    说明:原本在 csdn 博客 写博客的,因为使用的移动宽带,csdn的 博客无法访问,所以先暂时到博客园写博客了 有能解决移动宽带 有部分网站不能访问的问题,请联系我,QQ 809775607 /** ...

  8. 利用RTE创建自定义软件安装包(一)

    说明:鉴于MDK5.0推出的新功能,安富莱电子顺势推出几期MDK5.0新功能的使用方法.MDK5.0提供的RTE功能还是很不错的,这个功能一方面方便用户创建自己常用的驱动文件包,还有一个很重要的功能就 ...

  9. JDBC增强

    JDBC增强 批处理:批量处理sql语句,比如批量添加用户信息. addBatch()  //pstmt.addBatch()  就是替换一条一条执行的execute****** executeBat ...

  10. Android 4.2启动代码分析(一)

    Android系统启动过程分析 Android系统的框架架构图如下(来自网上):   Linux内核启动之后----->就到Android的Init进程 ----->进而启动Android ...