【问题标题】:call wcf service by HttpWebRequest通过 HttpWebRequest 调用 wcf 服务
【发布时间】:2011-11-21 12:04:26
【问题描述】:

当我获得该服务时:

[OperationContract]
ResponseMessage GetData(RequestMessage message);

在哪里

class RequestMessage
{
  public string data    
}

class ResponseMessage
{
  public string data
}

并调用此服务

string data2 = ""
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/Service.svc/GetData");
request.ContentType = "application/json";
request.Method = "POST";
request.KeepAlive = true;

using (Stream requestStream = request.GetRequestStream())
{
  var bytes = Encoding.UTF8.GetBytes(data2);
  requestStream.Write(bytes, 0, bytes.Length);
  requestStream.Close();
}

var response = (HttpWebResponse)request.GetResponse();
var abc = new StreamReader(response.GetResponseStream()).ReadToEnd();

作为data2,我应该发送字符串“mydata”还是应该将其包装为json格式:{“message”:{“data”:“mydata”}}

?? 我无法理解如何通过邮件在客户端发送数据以在服务端正确获取数据:/

【问题讨论】:

    标签: wcf post


    【解决方案1】:

    你没有提到服务是如何定义的。假设您的端点使用webHttpBinding,并且端点行为与<webHttp/> 具有默认值,那么主体样式的默认值为“Bare”,这意味着请求应仅包含参数的序列化版本。对于这种情况,您可以发送字符串{"data":"hello world"}

    如果您想快速找到 WCF 服务的预期格式,您可以使用 WCF 客户端,使用相同的合同/绑定/行为,并向服务器发送消息(并在提琴手上捕获它)。例如,下面的代码显示了一个类似于你的服务器,以及一个向它发送请求的客户端。

    public class StackOverflow_7492678
    {
        public class RequestMessage
        {
            public string data;
        }
        public class ResponseMessage
        {
            public string data;
        }
        [ServiceContract]
        public interface ITest
        {
            [OperationContract]
            ResponseMessage GetData(RequestMessage message);
        }
        public class Service : ITest
        {
            public ResponseMessage GetData(RequestMessage message)
            {
                return new ResponseMessage { data = message.data };
            }
        }
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
            var endpoint = host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "");
            endpoint.Behaviors.Add(new WebHttpBehavior());
            host.Open();
            Console.WriteLine("Host opened");
    
            ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new WebHttpBinding(), new EndpointAddress(baseAddress));
            factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
            ITest proxy = factory.CreateChannel();
    
            Console.WriteLine(proxy.GetData(new RequestMessage { data = "mydata" }).data);
    
            ((IClientChannel)proxy).Close();
            factory.Close();
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      相关资源
      最近更新 更多