原文 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. cmd正常启动tomcat,而 从eclipse启动出现 404

    设置Tomcat的路径,启动Tomcat,先测试一下环境,在浏览器中输入http://127.0.0.1:8080/ 提示 404找不到网页.出现这种问题然后试了一下,tomcat在外面直接启动  然 ...

  2. Android 拨号器的简单实现

    功能实现:一个EditView 一个拨打按钮,输入号码跳转到拨号界面 界面布局:activity_call.xml //线性垂直布局:一个EditView文本.一个Button按钮 1 <Lin ...

  3. Flink资料(3)-- Flink一般架构和处理模型

    Flink一般架构和处理模型 本文翻译自General Architecture and Process Model ----------------------------------------- ...

  4. java设计模式之 单例模式 Singleton

    static 的应用 单例模式 Singleton 单例:保证一个类在系统中最多只创建一个实例. 好处:由于过多创建对象实例,会产生过多的系统垃圾,需要GC频繁回收,由于GC会占用较大的系统资源,所有 ...

  5. PyCharm 2016.1 for Mac 激活方法分享

    内容如题,需要就参考一下,不需要请绕行!内容来自墙外我只是搬运工! 简单介绍一下步骤: 1.下载下面的压缩包并解压下来. http://files.cnblogs.com/files/korykim/ ...

  6. 刷爆github小绿点

    转载请注明出处:https://ahangchen.gitbooks.io/windy-afternoon/content/kit/git/green_blush.html 工程地址,欢迎star!! ...

  7. XP下Virtualbox虚拟Ubuntu共享文件夹设置

    环境:主机操作系统是Windows XP,虚拟机是Ubuntu 8.10,虚拟机是VirtualBox 2.1.0. 1. 安装增强功能包(Guest Additions) 安装好Ubuntu 8.1 ...

  8. Win32中安全的子类化(翻译)

    关于子类化的话题虽然有些旧,但它至今仍然不失为一种开发Windows的强有力技术,在MFC的内核.甚至.NET的内核中都离不开它,希望本连载能对Windows开发的爱好者有所帮助. 原文标题:Safe ...

  9. 在 Windows Azure 虚拟机中使用 Microsoft SQL Server 安全功能

    编辑人员注释:本文章由 SQL Server 团队高级项目经理 Sung Hsueh 撰写. SQL Server 的最新用法之一是利用 Microsoft 的 Windows Azure 基础结构服 ...

  10. Google Play Services Library update and missing symbol @integer/google_play_services_version

    转自http://stackoverflow.com/questions/19843784/google-play-services-library-update-and-missing-symbol ...