博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
三种客户端访问wcf服务端的方法 C#
阅读量:4349 次
发布时间:2019-06-07

本文共 6521 字,大约阅读时间需要 21 分钟。

原文

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
data = new Dictionary
{ {"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 =
posted on
2013-10-19 16:26 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lonelyxmas/p/3378009.html

你可能感兴趣的文章
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>