怎么通用调用WCF服务
1个回答
展开全部
一、WCF REST专用POST方法
1.1、 建立WCF REST 方法
1
2
3
4
5
6
7
8
[ServiceContract]
public interface IBookingBizService
{
[WebInvoke(UriTemplate = "setdeliver", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract()]
OperateResult SetDeliver(string args);
}
1.2、 POST调用WCF REST 方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
private string UseHttpWebApproach(string serviceUrl, string resourceUrl, string method, string requestBody)
{
string responseMessage = null;
var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
if (request != null)
{
request.ContentType = "application/json";
request.Method = method;
}
if (method == "POST" && requestBody != null)
{
byte[] requestBodyBytes = ToByteArrayUsingJsonContractSer(requestBody);
request.ContentLength = requestBodyBytes.Length;
using (Stream postStream = request.GetRequestStream())
postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
}
if (request != null)
{
var response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
if (responseStream != null)
{
var reader = new StreamReader(responseStream);
responseMessage = reader.ReadToEnd();
}
}
else
{
responseMessage = response.StatusDescription;
}
}
return responseMessage;
}
private static byte[] ToByteArrayUsingJsonContractSer(string requestBody)
{
byte[] bytes = null;
var serializer1 = new DataContractJsonSerializer(typeof(string));
var ms1 = new MemoryStream();
serializer1.WriteObject(ms1, requestBody);
ms1.Position = 0;
var reader = new StreamReader(ms1);
bytes = ms1.ToArray();
return bytes;
}
调用WCF REST 方法和一般的POST方法有所不同,区别在于发送前对数据的编码,采用DataContractJsonSerializer 进行序列化。
一般的对POST方法的调用采用如何数据编码
1
2
3
4
5
6
7
private static byte[] ToByteArray(string requestBody)
{
byte[] bytes = null;
bytes = Encoding.UTF8.GetBytes(requestBody);
return bytes;
}
如果要建立通用的POST调用服务,不建议采用WCF REST 形式,如果只是针对.net 平台的调用到时没有关系。
1.1、 建立WCF REST 方法
1
2
3
4
5
6
7
8
[ServiceContract]
public interface IBookingBizService
{
[WebInvoke(UriTemplate = "setdeliver", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract()]
OperateResult SetDeliver(string args);
}
1.2、 POST调用WCF REST 方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
private string UseHttpWebApproach(string serviceUrl, string resourceUrl, string method, string requestBody)
{
string responseMessage = null;
var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
if (request != null)
{
request.ContentType = "application/json";
request.Method = method;
}
if (method == "POST" && requestBody != null)
{
byte[] requestBodyBytes = ToByteArrayUsingJsonContractSer(requestBody);
request.ContentLength = requestBodyBytes.Length;
using (Stream postStream = request.GetRequestStream())
postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
}
if (request != null)
{
var response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
if (responseStream != null)
{
var reader = new StreamReader(responseStream);
responseMessage = reader.ReadToEnd();
}
}
else
{
responseMessage = response.StatusDescription;
}
}
return responseMessage;
}
private static byte[] ToByteArrayUsingJsonContractSer(string requestBody)
{
byte[] bytes = null;
var serializer1 = new DataContractJsonSerializer(typeof(string));
var ms1 = new MemoryStream();
serializer1.WriteObject(ms1, requestBody);
ms1.Position = 0;
var reader = new StreamReader(ms1);
bytes = ms1.ToArray();
return bytes;
}
调用WCF REST 方法和一般的POST方法有所不同,区别在于发送前对数据的编码,采用DataContractJsonSerializer 进行序列化。
一般的对POST方法的调用采用如何数据编码
1
2
3
4
5
6
7
private static byte[] ToByteArray(string requestBody)
{
byte[] bytes = null;
bytes = Encoding.UTF8.GetBytes(requestBody);
return bytes;
}
如果要建立通用的POST调用服务,不建议采用WCF REST 形式,如果只是针对.net 平台的调用到时没有关系。
广东轻亿云软件
2024-05-14 广告
2024-05-14 广告
广东轻亿云软件科技有限公司在软件开发领域深耕多年,积累了丰富的经验和技术实力。我们深知API接口在现代软件开发中的重要性,因此,我们与多家业界领先的API接口提供商保持着紧密的合作关系,确保我们的产品和服务能够充分利用这些接口,为用户提供更...
点击进入详情页
本回答由广东轻亿云软件提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询