原文 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. IO库 8.6

    题目:重写7.1.1节的书店程序(P229),从一个文件中读取交易记录.将文件名作为一个参数传递给main. #include <iostream> #include <fstrea ...

  2. 浅谈C中的指针和数组(一)

    本文转载地址:http://www.cnblogs.com/dolphin0520/archive/2011/11/09/2242138.html 在原文的基础上加入自己的想法作为修改. 指针是C/C ...

  3. JVM学习之对象的状态

    堆中存放着几乎所有的对象实例,垃圾收集器在堆堆进行回收前,首先要确定这些对象哪些还“活着”,哪些已经“死去”.方法有如下两种: (1)引用计数法 算法思想:为对象添加一个引用计数器,每当有一个地方引用 ...

  4. jquery解决onmouseover和onmouseout合用的bug问题

    经常会遇到鼠标放到一个元素上显示另外一个元素,这两个元素是父子关系,比如在A上绑定mouseover和mouseout事件来显示或隐藏B元素,A元素包含B元素,当鼠标移到B元素后浏览器认为你移开了A, ...

  5. WARNING:Could not increase the asynch I/O limit to 64 for SQL direct I/O. It is set to 0

    今天是2014-01-07,解决一下hp-unix异步I/O问题. 从trace日志中看: WARNING:Could not increase the asynch I/O limit to 32 ...

  6. javaTemplates-学习笔记三

    Routes入口 后台语言的应用入口都是从routes开始的,想要新建一个页面得学会配置routes. conf/routes 文件定义了全部应用URL的动作(Action),如果当浏览器请求访问ht ...

  7. 九度OJ 题目1534:数组中第K小的数字(二分解)

    题目链接:点击打开链接 题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[3,4].那么由A和B中的元素两两相加得到的数组C为[4,5,5,6 ...

  8. poj2509---抽k根烟就换一支,求能抽烟的总数

    #include <stdio.h> #include <stdlib.h> int main() { int now,k; while(scanf("%d %d&q ...

  9. libvirt C-API

    1,warming-up Specifying URIs to libVirt;name parameter to virConnectOpen or          virConnectOpenR ...

  10. struts2 全局格式化,格式化时间,金钱,数字

    //在前台页面去控制时间,数字,小数,金钱,是极其不明智的选择,除非你是写了良好的 js api 像freemarker , struts 都有良好的标签,我们应该好好利用,才发现的,给大家分享一下 ...