三种客户端访问wcf服务端的方法 C#
2013-10-19 16:26
393 查看
原文 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#
- 三种客户端访问wcf服务端的方法 C#
- C# Winform WCF 调试服务端的程序(三种方法)
- C# Winform WCF 调试服务端的程序(三种方法)
- c#访问类的私有成员的三种方法
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- 利用svcutil.exe 访问windows Server 2003的wcf服务生成客户端代码报错:无法从http://XXX/XXX.svc?wsdl获取元数据”错误的解决方法
- .Net(C#)访问X509证书加密的Webservice(四)--客户端访问WCF
- C# 编写WCF简单的服务端与客户端
- Winform 调用WCF客户端,所有服务端方法在运行的时候均找不到(编译没有问题)
- C++的gsoap客户端与基于WCF的C#服务端Web Service交互的问题
- Winform 调用WCF客户端,所有服务端方法在运行的时候均找不到(编译没有问题)
- WCF RIA Services 客户端、服务端的处理方法和例子
- C#访问SQL——三种更新记录的方法
- Asp.net 使用Ajax 控件访问服务端的方法与,服务端访问客户端的方法
- C#服务端判断客户端socket是否已断开的方法
- 利用svcutil.exe 访问windows Server 2003的wcf服务生成客户端代码报错:无法从http://XXX/XXX.svc?wsdl获取元数据”错误的解决方法
- WCF----双工模式服务端回调客户端方法时无响应,“发送到 http://***/User.svc 的请求操作在配置的超时内未收到回复”
- C#三种WCF网络客户端的实现方式
- C# 编写WCF简单的服务端与客户端